Data grouping changes or selects the key that records share before a stateful operation. In Kafka Streams, `groupBy` uses a mapper to choose that key, producing a grouped stream or table that can feed an aggregation. Use `groupByKey` when the existing key is already the grouping key. section 7.2 section 7.2.2
For a transaction stream keyed by client ID, grouping by stock symbol puts all transactions for a ticker into the same aggregate. The key change requires repartitioning so records for a group can be processed together; Kafka Streams handles that repartition automatically when a downstream aggregation, `groupByKey`, or join depends on the new key. section 7.2.2 section 7.2.3
Aggregate stock transactions by symbol
Read transactions, map each record to its symbol with `groupBy`, then call `aggregate`. Supply grouped-key and value Serdes, an initial aggregate, aggregation logic, and state-store Serdes. Convert the resulting `KTable` to a stream before writing aggregate updates to an output topic. section 7.2.2 section 7.2.2
A `KTable` groups differently because its incoming key is a primary key: grouping by that key alone yields one record. Group by another field, such as market segment, to combine rows for aggregation. A `KTable.groupBy` returns a `KGroupedTable`; its aggregate uses an initializer plus add and subtract functions so updates can remove the prior value before adding the new one. section 8.4 section 8.4
Listing 8.2 Aggregates with a KTable
KTable<String, StockAlert> stockTable =
builder.table("stock-alert",
Consumed.with(stringSerde, stockAlertSerde)); #1
stockTable.groupBy((key, value) ->
KeyValue.pair(value.getMarketSegment(), value),
Grouped.with(stringSerde, stockAlertSerde)) #2
.aggregate(segmentInitializer, #3
adderAggregator, #4
subtractorAggregator, #5
Materialized.with(stringSerde, segmentSerde))
.toStream()
.to("stock-alert-aggregate",
Produced.with(stringSerde, segmentSerde));
Inspect a complete `KTable.groupBy` pipeline that maps stock alerts to market segments and maintains an aggregate with initializer, adder, subtractor, and materialization. section 8.4





