liveBook · Manning

KTable

As explained in Apache Kafka in Action

KTable is the Kafka Streams abstraction representing the latest value for each key as a continuously updated table.

A KTable represents the current intermediate state for each key while changes continue to arrive. Unlike a traditional relational table, it contains key-value pairs and is usually partitioned, so each application instance holds only part of the state. section 12.4.1

A KTable is created and maintained through a stateful stream-processing flow:

  • Create one from a topic with builder.table("topic"); Kafka Streams reads the topic from the beginning and updates the stored value for each key. section 12.4.1
  • Store the state locally, commonly in a fast key-value store such as RocksDB; protect recoverability with a usually compacted changelog topic. section 12.4.1
  • Create one from a KStream through aggregation, and convert it back to a KStream with toStream when downstream processing needs the changes. section 12.4.1

Active devices for notification delivery

A notifications pipeline can aggregate the user-devices stream into a KTable containing each user’s active devices. The aggregator adds active devices and removes inactive ones; the notifications stream then joins that table to attach the current device list before writing device notifications. section 12.4.4

Figure 12.7 Processors such as map, filter, and flatMap work on both KStreams and KTables. We use aggregations to create KTables from KStreams and toStream to produce a KStream from a KTable.
A close-up of two ovals

AI-generated content may be incorrect.

Use the stream-table flow to see how aggregations produce KTables and toStream produces a KStream. section 12.4.1

How Apache Kafka in Action covers it

How deep it goes
Explains creation, state reconstruction, partitioning, local storage, changelog recovery, aggregation, and conversion, but not storage internals.
Examples
Uses active-user-device aggregation and notification joins to demonstrate a continuously updated keyed table.
What it assumes
Assumes familiarity with topics and stream processing, while introducing tables, keyed state, aggregation, and recovery progressively.
What you can do after
Apply it via Kafka Streams to aggregate keyed state and join incoming events with current values.
How it connects
Connects KStream, topics, aggregation, joins, topic compaction, local state, RocksDB, and changelog topics.

Also explained in

Compare how 3 books explain KTable →

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.