8 The KTable API
This chapter covers
This chapter will introduce you to a new API in Kafka Streams, the Ktable
. The KTable
is an update or changelog stream. You’ve already used a KTable
as any aggregation operations in Kafka Streams result in a KTable
. The KTable
is an essential abstraction for working with records with the same key. In a KStream
, records with the same key are independent events. But in the KTable
, a record updates the previous one with the same key.
Why is learning about an update stream necessary? Sometimes, you’ll only care about the latest entry for a given piece of data. For example, consider a user profile. When someone updates their profile, only the newest entry is correct. All previous versions of the profile don’t matter. Compared to a relational database, the event stream (a KStream
) could be considered a series of inserts where the primary key is an auto-incrementing number. Each insert of a new record has no relationship to previous ones. But with a KTable
, the key in the key-value pair is the primary key. So, instead of inserting a new row, an update to the row results.