liveBook · Manning

Peek

As explained in Kafka Streams in Action, Second Edition

KStream.peek is a Kafka Streams operation that performs a stateless action on each record without changing the record's key or value.

KStream.peek performs a stateless action on every record while leaving its incoming key and value unchanged. Use it to print, log, or collect information at an arbitrary point in a topology, then continue chaining stream operations. section 6.5

Use peek when you need visibility without terminating the stream:

  • Place it before and after a transformation such as mapValues to compare records as they move through the topology. section 6.5
  • Pass a ForeachAction, commonly a lambda receiving the record key and value, to perform the observation. section 6.5
  • Prefer peek when subsequent operations must remain chainable; print is purpose-built for output but is terminal and returns void. section 6.5 section 6.7
  • Convert a KTable to a KStream with toStream before using peek to inspect windowed aggregation results. section 9.1.6

Inspecting a transformation

ForeachAction<String, String> sysout = (key, value) -> System.out.println("key " + key + " value " + value); builder.stream("src-topic", Consumed.with(stringSerde, stringSerde)).peek(sysout).mapValues(value -> value.toUpperCase()).peek(sysout).to("out-topic", Produced.with(stringSerde, stringSerde)); The first peek observes the original records; the second observes the uppercased values, while the stream continues to out-topic. section 6.5

Listing 6.14 Printing records flowing through the Yelling app
ForeachAction<String, String> sysout =
  (key, value) ->
   System.out.println("key " + key
   + " value " + value);


builder.stream("src-topic",
  Consumed.with(stringSerde, stringSerde))
  .peek(sysout)       #1
  .mapValues(value -> value.toUpperCase())
  .peek(sysout)       #2
  .to( "out-topic",
  Produced.with(stringSerde, stringSerde));

Browse the complete chained example showing one peek before mapValues and another after it. section 6.5

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains callback behavior, unchanged records, chaining, and windowed-result inspection without descending into processor internals.
Examples
Uses the Yelling app and windowed aggregation logging to inspect records before transformations and analyze window boundaries.
What it assumes
Assumes familiarity with KStream chains, lambdas, transformations, and converting KTable results with toStream.
What you can do after
Apply it via Kafka Streams to print, log, or collect records while continuing a KStream chain.
How it connects
Connects peek with KStream, ForeachAction, mapValues, print, KTable, toStream, and windowed aggregations.

Also explained in

Compare how 3 books explain peek →

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.