0
0
Kafkadevops~10 mins

Join operations (KStream-KStream, KStream-KTable) in Kafka - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Join operations (KStream-KStream, KStream-KTable)
Start with two streams or stream+table
Match keys from both sides
Apply join condition
Combine records into one output
Emit joined stream
End
Join operations combine records from two streams or a stream and a table by matching keys and merging data into a new stream.
Execution Sample
Kafka
KStream<String, String> stream1 = builder.stream("topic1");
KStream<String, String> stream2 = builder.stream("topic2");

KStream<String, String> joined = stream1.join(stream2,
  (v1, v2) -> v1 + ":" + v2,
  JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofSeconds(5)));

joined.to("joined-topic");
This code joins two streams on matching keys within a 5-second window and outputs combined records.
Process Table
StepInput Stream1 RecordInput Stream2 RecordJoin ConditionOutput Record
1key1: Akey1: XKeys match, within 5s windowkey1: A:X
2key2: Bkey3: YKeys do not matchNo output
3key1: Ckey1: ZKeys match, within 5s windowkey1: C:Z
4key4: Dkey1: WKeys do not matchNo output
5End of inputEnd of inputNo more recordsStream ends
💡 No more matching keys or records to join, stream processing ends.
Status Tracker
VariableStartAfter 1After 2After 3After 4Final
stream1 recordnonekey1:Akey2:Bkey1:Ckey4:Dend
stream2 recordnonekey1:Xkey3:Ykey1:Zkey1:Wend
joined outputnonekey1:A:Xnonekey1:C:Znoneend
Key Moments - 2 Insights
Why does no output appear when keys don't match?
Join only emits output when keys from both streams match (see rows 2 and 4 in execution_table). Non-matching keys produce no joined record.
How does the time window affect joining?
Records must arrive within the join window (5 seconds here) to be joined. If outside, no output is produced (implied in join condition column).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output record at step 3?
Akey1:A:X
Bkey1:C:Z
Ckey2:B:Y
DNo output
💡 Hint
Check the Output Record column at step 3 in execution_table.
At which step do the keys not match and no output is produced?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for 'No output' in the Output Record column in execution_table.
If the join window was removed, how would the output change?
AFewer joined records
BNo change in output
CMore joined records regardless of time
DStream would stop immediately
💡 Hint
Join window controls timing; removing it allows all matching keys to join anytime.
Concept Snapshot
Join operations combine two streams or a stream and a table by matching keys.
KStream-KStream join uses a time window to match records.
KStream-KTable join uses latest table state for each key.
Output is a new stream with combined records.
Only matching keys produce output.
Time window controls which records join in streams.
Full Transcript
Join operations in Kafka Streams combine data from two sources by matching keys. For KStream-KStream joins, records must arrive within a time window to be joined. For KStream-KTable joins, the stream record joins with the latest table value for the key. The process starts by reading records from both sources, matching keys, applying join conditions, and producing a combined output record. If keys don't match or records fall outside the time window, no output is produced. The joined results are emitted as a new stream for further processing or output. This visual trace shows step-by-step how records from two streams are joined based on keys and time window, tracking variables and outputs at each step.