liveBook · Manning

Data aggregation

As explained in Apache Kafka in Action

A metric aggregation combines metric observations over a time period or other grouping to produce a summarized value.

An event stream records what happened, but answering “what is the current balance?” by replaying every transaction is expensive. Aggregation maintains a summary as new data arrives. Grouping transactions by account ID and summing their amounts produces a separate current balance for each account. section 12.4 section 12.4.2

A streaming aggregation needs two pieces:

  • An initial state for a key with no previous result: 0 for a count or sum, 1 for multiplication, or an empty collection when collecting items. section 12.4.2 section 12.4.4
  • An adder that combines the previous state with each incoming value. A count adds 1; a sum adds the value. The updated result is retained per key. section 12.4.2

Count and sum by key

For the observations (K1, 2), (K2, 1), and (K2, 2), a count yields K1: 1 and K2: 2. A sum yields K1: 2 and K2: 3. An average needs more state than the displayed average alone: the example tracks sum and count so each new value can update the result. section 12.4.2

Figure 12.8 To count the number of messages per key, a state store with the current count per key is needed. The changes to the state store are stored in a compacted changelog topic.

The count diagram shows the operational detail behind the result: a state store holds each key’s current count, while a compacted changelog topic records changes so the state can survive a processor crash. section 12.4.2

The same pattern can maintain a list of active devices for each user. Start with an empty list, add active devices, remove inactive ones, then join the resulting table with incoming notifications. Kafka Streams provides groupByKey().aggregate() for this flow; Flink SQL can express the grouped device view with GROUP BY. section 12.4.4

Metric aggregation serves a related operational purpose. A CPU-utilization spike to 100% may be harmless if brief, so visualizing and smoothing fluctuations over a longer period can help choose a useful alert threshold. Begin conservatively and adjust thresholds if false alarms are frequent; not every useful diagnostic metric needs its own alert. section 15.4.1 section 15.4

How Apache Kafka in Action covers it

How deep it goes
Works through initial states, adders, stored results, and recovery; advises on metric smoothing without prescribing a particular time-window calculation.
Examples
Uses account balances, per-key counts and sums, active-device lists joined to notifications, and CPU-utilization spikes.
What it assumes
Assumes familiarity with streams and keys, while defining the initial state and adder needed to build an aggregation.
What you can do after
Apply it via Kafka Streams aggregate() or Flink SQL GROUP BY to maintain per-user results for a notification join.
How it connects
Connects stream processing with tables, state stores, changelog topics, joins, and alert thresholds.

Also explained in

Compare how 19 books explain data aggregation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.