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


