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.
