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.
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

