liveBook · Manning

Branched

As explained in Kafka Streams in Action, Second Edition

Branched is a Kafka Streams definition that specifies naming and processing behavior for a stream branch.

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.

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the split, predicate gates, termination choices, returned stream map, and unchanged key-value types.
Examples
Demonstrates coffee, electronics, retail, temperature-sensor, and proximity-sensor branches.
What it assumes
Assumes KStream processing and predicates, while introducing split, branch, default, and no-default control flow.
What you can do after
Builds Java Kafka Streams branches that route matching and unmatched records to topics or reusable streams.
How it connects
Connects KStream, BranchedKStream, Predicate, topics, merging, and stateful stream topology design.

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.