liveBook · Manning

Data grouping

As explained in Kafka Streams in Action, Second Edition

GroupBy is a Kafka Streams DSL operation that remaps stream records to a selected key before grouping them for downstream processing.

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

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains key remapping, repartitioning, KStream aggregation, and KTable add/subtract update handling.
Examples
Uses stock transactions grouped by symbol and stock alerts grouped by market segment.
What it assumes
Assumes familiarity with stream setup and Serdes while clarifying grouping as an aggregation prerequisite.
What you can do after
Apply it via Kafka Streams to rekey records and build grouped KStream or KTable aggregates.
How it connects
Connects groupBy with groupByKey, aggregation, repartitioning, KTable, state stores, and joins.

Also explained in

Compare how 6 books explain data grouping →

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.