liveBook · Manning

BranchedKStream.branch

As explained in Kafka Streams in Action, Second Edition

BranchedKStream.branch is a Kafka Streams method that creates a branch by applying a predicate and branch-processing definition to records.

BranchedKStream.branch() creates one stream branch by pairing a boolean Predicate with a Branched processing definition. A matching record enters that branch’s processor, while the parent stream retains its original key and value types. section 6.6.2

Build a split topology in this sequence:

  • Call KStream.split() to obtain a BranchedKStream. section 6.6.2
  • Call branch(predicate, branched) for each condition and destination or transformation. section 6.6.2
  • Finish with defaultBranch() to process nonmatching records, or noDefaultBranch() to drop them. section 6.6.2
  • Use the returned map of KStream objects when Branched definitions expose newly created branches. section 6.6.2

Route purchase departments

Predicate<String, Purchase> isCoffee = (key, purchase) -> purchase.getDepartment().equalsIgnoreCase("coffee"); purchaseKStream.split() .branch(isCoffee, Branched.withConsumer(stream -> stream.to("coffee-topic"))) .branch(isElectronics, Branched.withConsumer(stream -> stream.to("electronics"))) .defaultBranch(Branched.withConsumer(stream -> stream.to("purchases"))); Coffee and electronics purchases leave through dedicated topics; all other purchases continue through the default topic. section 6.6.2

Listing 6.17 Splitting the stream
Predicate<String, Purchase> isCoffee =
  (key, purchase) ->
   purchase.getDepartment().equalsIgnoreCase("coffee");       #1

Predicate<String, Purchase> isElectronics =
  (key, purchase) ->
  purchase.getDepartment().equalsIgnoreCase("electronics");  

purchaseKStream.split()   #2
.branch(isCoffee,
 Branched.withConsumer(coffeeStream -> coffeeStream.to("coffee-topic")))  #3
.branch(isElectronics,
  Branched.withConsumer(electronicStream ->
  electronicStream.to("electronics"))   #4
.defaultBranch(Branched.withConsumer(retailStream ->
                retailStream.to("purchases"));  #5

Inspect the complete purchase-routing listing showing predicates, Branched.withConsumer(), and defaultBranch(). section 6.6.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains predicates, processing definitions, termination choices, returned stream maps, and concrete Java topology code.
Examples
Uses coffee, electronics, legacy sensor, temperature sensor, and proximity sensor branches with topic outputs and value conversion.
What it assumes
Assumes an existing KStream and records while defining Predicate and Branched roles through concrete examples.
What you can do after
Apply it via Kafka Streams to route matching records, handle unmatched records, or retrieve branches for later merging.
How it connects
Connects KStream, Predicate, Branched, default branches, topic processing, stream merging, and Kafka Streams topologies.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.