liveBook · Manning

Key

As explained in Kafka Streams in Action, Second Edition

A key is a common identifier used to associate, store, and retrieve a value for a particular record or entity.

A key is a common identifier that associates a value with a particular record or entity. In stateful processing, it lets an operation look up, update, and store the value belonging to that entity. section 7.1

A stateful count uses the incoming object's identifier as the key, retrieves the current count, increments it, and stores the result. When no value exists, an initializing function supplies the first value. section 7.1

Counting widgets by identifier

The count function calls hashMap.compute(widget.id, ...) to associate each widget ID with its running count, returning 1 for the first occurrence and incrementing later occurrences. section 7.1

Preserving a Kafka record key

A Spring Kafka listener can capture an incoming record's key through the RECEIVED_MESSAGE_KEY header, then produce the processed loan with processedLoan.getCustomerId() as its key. section 12.2.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;
}

Inspect the compact Java implementation of key-based state: compute a value by identifier, initialize missing state, and increment existing state. section 7.1

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains lookup, initialization, update, and storage through a HashMap example, then applies keys to Spring Kafka records.
Examples
Uses widget counts keyed by ID and processed loans keyed by customer ID.
What it assumes
Assumes basic objects and identifiers, while introducing stateful processing through a concrete count operation.
What you can do after
Apply it via Java stateful functions and Spring Kafka listeners to associate, retrieve, update, and produce keyed values.
How it connects
Connects keys with stateful operations, aggregation, identifiers, Kafka records, and Spring Kafka message headers.

Also explained in

Compare how 10 books explain key →

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.