liveBook · Manning

KStream.split

As explained in Kafka Streams in Action, Second Edition

KStream.split is a Kafka Streams DSL method that begins branching a KStream and returns a BranchedKStream.

KStream.split begins branching a KStream and returns a BranchedKStream. Predicates route records into separate processing paths while preserving the original key and value types. section 6.6 section 6.6.2

Build a branching topology in four steps:

  • Call split() on the source KStream. section 6.6.2
  • Add each branch with branch(), a Predicate, and a Branched processor. section 6.6.2
  • Finish with defaultBranch() to retain nonmatching records or noDefaultBranch() to drop them. section 6.6.2
  • Collect returned KStream objects through the resulting map when branches need further topology work. section 6.6.2

Routing ZMart purchases

Predicate<String, Purchase> isCoffee = (key, purchase) -> purchase.getDepartment().equalsIgnoreCase("coffee"); Predicate<String, Purchase> isElectronics = (key, purchase) -> purchase.getDepartment().equalsIgnoreCase("electronics"); 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

Figure 6.8 Creating branches for the two specific purchase types
figure

Visualizes a single purchase stream dividing into branches for specific purchase types and a default path. section 6.6.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the split-branch-termination sequence, predicate gating, returned stream maps, and unchanged key-value types.
Examples
Uses ZMart coffee, electronics, and retail purchases, plus temperature and proximity sensor streams.
What it assumes
Assumes a KStream and predicate-based record processing, while teaching default and nondefault handling from the branching operation.
What you can do after
Apply it via the Kafka Streams DSL to route matching records to topics or reusable downstream KStreams.
How it connects
Connects KStream.split to BranchedKStream, 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.