liveBook · Manning

KStream

As explained in Kafka Streams in Action, Second Edition

KStream is a Kafka Streams abstraction representing an unbounded stream of key-value records and supporting record-by-record processing operations.

KStream is the Kafka Streams DSL abstraction for flowing key-value records. It supports fluent, record-by-record processing: most operations return another KStream, so transformations can be chained with lambda-friendly interfaces. A topology uses source nodes to consume topic records and sink nodes to write results back to Kafka. section 6.2 section 6.7

Use KStream operations to shape, route, inspect, and publish event records:

  • Read a topic with StreamsBuilder.stream(), supplying Serdes through Consumed or relying on configured defaults. section 6.4.1
  • Transform values with mapValues when the key stays unchanged; use map when producing a new key and value. section 6.4.1
  • Split records with split(), branch predicates, and a default or no-default branch; branches retain the parent key and value types. section 6.6.2
  • Inspect records with peek() for stateless logging or collection; use print(), foreach(), and to() as terminal operations. section 6.5
  • Merge compatible streams, select keys, and filter records when building independent processing paths. section 6.7

Mask purchases before further routing

Create a KStream from the transactions topic and immediately call mapValues with a credit-card mapper. The mapper returns a RetailPurchase with its card number masked while preserving the record key. If the transformation needs the customer-ID key, use ValueMapperWithKey instead; if it must create a new key, use map with KeyValueMapper. section 6.4.1

Listing 6.7 Building the source node and first processor
KStream<String, RetailPurchase> retailPurchaseKStream =
     streamsBuilder.stream("transactions",
     Consumed.with(stringSerde, retailPurchaseSerde))
    .mapValues(creditCardMapper);

This minimal pipeline shows a typed source stream followed by a value-only transformation: read transaction records, apply the masking mapper, and retain a KStream for the next operation. section 6.4.1

A KStream treats every input record as an individual event. In the stock-ticker comparison, it printed all nine produced quotes, whereas a KTable represented updates and printed only the final values for each key. section 8.1.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains source creation, transformations, branching, terminal operations, and KTable contrast; defers lower-level Processor API control.
Examples
Uses masked retail purchases, branching coffee and electronics purchases, sensor streams, yelling text, and stock-ticker quotes.
What you can do after
Apply it via Kafka Streams DSL to construct chained topic-processing pipelines and inspect or route their records.
How it connects
Connects Kafka Streams DSL with topologies, topics, Serdes, mapValues, map, split, merge, KTable, and Processor API.

Also explained in

Compare how 3 books explain KStream →

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.