Branched defines how records are processed after a KStream is split. Each branch pairs a Predicate gate with branch processing logic, while preserving the parent stream’s key and value types. section 6.6.2
Use the branching sequence to route records:
- Call KStream.split() to obtain a BranchedKStream. section 6.6.2
- Add branch() calls with predicates and Branched handlers; matching records enter the corresponding processing logic. section 6.6.2
- Finish with defaultBranch() to route unmatched records or noDefaultBranch() to drop them. section 6.6.2
- Retrieve new streams through the returned map when branches use names or functions. section 6.6.2
Route purchase departments
purchaseKStream.split() .branch(isCoffee, Branched.withConsumer(s -> s.to("coffee-topic"))) .branch(isElectronics, Branched.withConsumer(s -> s.to("electronics"))) .defaultBranch(Branched.withConsumer(s -> s.to("purchases"))); Coffee and electronics purchases leave through dedicated topics; every other purchase follows the default branch.
