liveBook · Manning

KeyValueStore

As explained in Kafka Streams in Action, Second Edition

KeyValueStore is the Kafka Streams state-store interface for persistently storing and retrieving key-value records.

KeyValueStore is the local state-store interface used by Kafka Streams applications to retain keyed results. In a stateful topology, it lets tests inspect the latest value for each key alongside the output topic. section 14.1.4

Use a KeyValueStore in stateful Kafka Streams testing to:

  • Retrieve a named store through TopologyTestDriver.getKeyValueStore. section 14.1.4
  • Read the current value for a key with kvStore.get(key). section 14.1.4
  • Compare final state-store contents with the latest reduced output after processing records. section 14.1.4

Inspecting a reduced state store

After creating a TopologyTestDriver and processing scores, retrieve the named store and assert its contents: KeyValueStore<String, Double> kvStore = driver.getKeyValueStore("memory-poker-score-store"); assertThat(kvStore.get("Anna"), is(65.75)); Later inputs update the value for each key, allowing assertions against the final accumulated totals. section 14.1.4

Listing 14.17 Validating the state store contents match the latest output
KeyValueStore<String, Double> kvStore =
     driver.getKeyValueStore("memory-poker-score-store");    #1

assertThat(kvStore.get("Anna"), is(65.75));     #2
assertThat(kvStore.get("Matthias"), is(55.8));
assertThat(kvStore.get("Neil"), is(47.43));

A compact listing shows how to retrieve a named KeyValueStore and validate values for several keys. section 14.1.4

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Demonstrates named-store retrieval and key lookups, stopping short of implementing a store or detailing its persistence internals.
Examples
Uses reduced player scores, named store lookups, final per-key maps, and GlobalKTable materialization on local disk.
What it assumes
Assumes familiarity with Kafka Streams stateful processing, named stores, keys, and TopologyTestDriver-based tests.
What you can do after
Diagnose stateful topologies by retrieving a named store and comparing stored values with expected stream results.
How it connects
Connects Kafka Streams state stores with TopologyTestDriver, reduce operations, KTable, GlobalKTable, and changelog topics.

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.