State is retained information that lets a stream-processing computation recall earlier events and connect them to the current event. Stateless operations use only the current event; stateful operations retrieve, update, and store information from related events. section 7.0 section 7.1
A stateful operation commonly follows these steps:
- Use a shared key, such as an identifier, to locate the state being tracked. section 7.1
- Retrieve the current value associated with that key. section 7.1
- Update or merge the value with the current event. section 7.1
- Initialize the value when the key has no prior state. section 7.1
Accumulating poker scores
A player’s score resets after each round, but the application must retain earlier scores to calculate a total. Kafka Streams groups records by player ID, reduces each player’s scores with addition, and writes the resulting totals to a topic. section 7.2 section 7.2
Listing 7.2 Stateful function example
public int count(Widget widget) {
int widgetCount = hashMap.compute(widget.id,
(key, value) -> (value == null) ? 1 : value + 1)
return widgetCount;
}
Shows a minimal Java stateful function that retrieves a count by widget ID, initializes missing values, and increments existing values. section 7.1
Listing 7.3 Performing a reduce in Kafka Streams
KStream<String, Double> pokerScoreStream = builder.stream("poker-game",
Consumed.with(Serdes.String(), Serdes.Double()));
pokerScoreStream
.groupByKey() #1
.reduce(Double::sum, #2
Materialized.with(Serdes.String(), Serdes.Double()))
.toStream() #3
.to("total-scores",
Produced.with(Serdes.String(), Serdes.Double())); #4
Shows Kafka Streams code that groups poker scores by key, reduces them with Double::sum, materializes the result, and writes totals. section 7.2






































