liveBook · Manning

BranchedKStream

As explained in Kafka Streams in Action, Second Edition

BranchedKStream is a Kafka Streams object that defines and terminates branches created from a KStream.

BranchedKStream is the branching stage returned by KStream.split(). It creates predicate-gated branches and terminates them with defaultBranch() or noDefaultBranch(). section 6.6.2

A branching topology follows this sequence:

  • Call split() on the original KStream. section 6.6.2
  • Call branch() with a Predicate and Branched handler; matching records flow through that branch. section 6.6.2
  • Finish with defaultBranch() for unmatched records or noDefaultBranch() to drop them. section 6.6.2
  • Use the returned Map<String, KStream<K, V>> when Branched objects expose newly created streams; branch key and value types remain unchanged. section 6.6.2

Route purchases by department

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"))); section 6.6.2

Figure 6.8 Creating branches for the two specific purchase types
figure

Visualizes how specific purchase types become separate branches while unmatched events continue through the default path. section 6.6.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the split, branch, and termination API, including returned streams, but stops short of broker-level internals.
Examples
Uses coffee, electronics, and default purchase topics plus temperature and proximity sensor streams.
What it assumes
Assumes an existing KStream and teaches predicates as logical gates for branch processing.
What you can do after
Apply via the Kafka Streams API to route records into topic-writing or reusable branch streams.
How it connects
Connects KStream, split(), branch(), Predicate, Branched, defaultBranch(), noDefaultBranch(), and Kafka topics.

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.