liveBook · Manning

Stream-table join

As explained in Kafka Streams in Action, Second Edition

A stream-table join combines records from a KStream with corresponding records in a KTable according to matching keys.

A stream-table join enriches each newly arriving KStream record with matching information looked up by key in a KTable. A clickstream containing user IDs and visited pages can gain current user details in real time. section 8.6

Keep these operational rules in mind:

  • Use co-partitioned source topics for KStream–KTable joins: matching keys must be assigned to corresponding partitions, and the topics must have the same partition count. section 8.6
  • Place the stream on the left and the table on the right; the join is not reciprocal or windowed. section 8.6.1
  • Trigger a lookup only when a new stream record arrives. A table update changes stored state but emits no new join result. section 8.6.1
  • Choose an inner equi-join when a match is required, or a left-outer join when unmatched stream records must remain available. section 8.6.1 section 11.6
  • Repartition incompatible KStream and KTable inputs before joining; GlobalKTable inputs avoid the co-partitioning requirement because every record is copied. section 8.6

Enriching click events with Kafka Streams

KStream<String, ClickEvent> clickEventKStream = builder.stream("click-events", Consumed.with(stringSerde, clickEventSerde)); KTable<String, User> userTable = builder.table("users", Consumed.with(stringSerde, userSerde)); clickEventKStream.join(userTable, clickEventJoiner) .peek(printKV("stream-table-join")) .to("stream-table-join", Produced.with(stringSerde, stringSerde)); The ValueJoiner receives both values and creates the enriched output, which may use either input type or a new type. section 8.6 section 8.6.1

Enriching rewards events with ksqlDB

Create a rewards_members table from the rewards-members topic, then LEFT OUTER JOIN customer-rewards-stream to retain purchases from nonmembers. Select customer identity, membership details, purchase total, and reward points into an enriched stream. section 11.6

Figure 8.15 Co-partitioned topics have the same number of partitions, so Kstream and KTable instances will have tasks working with the same partition.
figure

Use the partition diagram to trace why matching keys can join only when source topics assign corresponding partitions to the same processing tasks. section 8.6

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains partitioning, join direction, triggers, join types, and implementation APIs, while deferring temporal semantics to versioned state stores.
Examples
Uses clickstream-user enrichment and rewards-member enrichment through Kafka Streams code and ksqlDB statements.
What it assumes
Assumes familiarity with stream-stream joins and introduces KStream, KTable, partition alignment, and lookup-trigger semantics.
What you can do after
Build it from scratch with Kafka Streams or ksqlDB, selecting partitioning and outer-join behavior for an enrichment pipeline.
How it connects
Connects KStream, KTable, GlobalKTable, co-partitioning, repartitioning, ValueJoiner, stream-stream joins, and versioned state stores.

Also explained in

Compare how 2 books explain stream-table join →

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.