liveBook · Manning

groupByKey

As explained in Kafka Streams in Action, Second Edition

groupByKey is a Kafka Streams stream operation that groups records by their existing keys for subsequent aggregation or other key-based processing.

groupByKey groups records by their existing keys, making each key’s records available to stateful operations such as reduce, count, or aggregate. It is the Kafka Streams counterpart of SQL GROUP BY for aggregation. section 7.2

Use groupByKey when downstream processing depends on the record key:

  • Group poker scores by player ID so each player’s running total is updated independently. section 7.2
  • Follow groupByKey with reduce when values share a type, such as summing Double scores, and materialize the result before writing it to an output topic. section 7.2
  • Expect repartitioning when a preceding operation changes keys and a later groupByKey, aggregation, or join depends on those keys. section 7.2.3

Running totals for poker scores

KStream<String, Double> pokerScoreStream = builder.stream("poker-game", Consumed.with(Serdes.String(), Serdes.Double())); pokerScoreStream.groupByKey().reduce(Double::sum, Materialized.with(Serdes.String(), Serdes.Double())).toStream().to("total-scores", Produced.with(Serdes.String(), Serdes.Double())); A record such as Neil, 650 represents a continually updated total for that player.

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains the operator through a reduce pipeline and details when key changes trigger automatic repartitioning.
Examples
Uses poker-game scores grouped by player ID, reduced with Double::sum, materialized, and emitted to total-scores.
What it assumes
Assumes familiarity with StreamBuilder and Serdes while establishing grouping as a prerequisite for stateful aggregation.
What you can do after
Apply it via Kafka Streams to build per-key reductions and recognize when preceding key changes require repartitioning.
How it connects
Connects groupByKey to reduce, aggregation, count, join, partitions, topology, and repartitioning.

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.