split() routes each record into one of several branches when different conditions require different processing. It offers an alternative to multiple filter() calls, especially when conditions represent mutually exclusive categories. section 12.2.1
Routing products by price
A prices stream can send products below 10 to prices-cheap, products below 100 to prices-midrange, and all remaining products to prices-expensive. Branches can continue processing through Branched.withConsumer(), such as writing each branch to its own topic. Branches are evaluated from top to bottom; the first true predicate handles the record, while defaultBranch() catches records that match no earlier predicate. section 12.2.1
prices.split() #1
.branch((key, value) -> value.price < 10, #2
Branched.withConsumer( #3
(stream) -> stream.to("prices-cheap"))) #4
.branch((key, value) -> value.price < 100, #5
Branched.withConsumer(
(stream) -> stream.to("prices-midrange")))
.defaultBranch(Branched.withConsumer( #6
(stream) -> stream.to("prices-expensive")))
Inspect the complete Kafka Streams topology for price-based branching, including predicates, branch consumers, output topics, and the default branch. section 12.2.1
