Challenge - 5 Problems
Kafka Streams Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a KStream-KStream join with windowing
Given two KStreams joined with a 5-second tumbling window, what is the output when the following records arrive?
Stream1: (key=1, value=A) at t=1000ms
Stream2: (key=1, value=1) at t=4000ms
Stream2: (key=1, value=2) at t=7000ms
Assume the join concatenates values as value1+value2.
Stream1: (key=1, value=A) at t=1000ms
Stream2: (key=1, value=1) at t=4000ms
Stream2: (key=1, value=2) at t=7000ms
Assume the join concatenates values as value1+value2.
Kafka
KStream<String, String> stream1 = builder.stream("stream1"); KStream<String, String> stream2 = builder.stream("stream2"); KStream<String, String> joined = stream1.join( stream2, (v1, v2) -> v1 + v2, JoinWindows.ofTimeDifferenceWithNoGrace(Duration.ofSeconds(5)) ); joined.to("joined-output");
Attempts:
2 left
💡 Hint
Remember the join window allows matching records within 5 seconds of each other.
✗ Incorrect
The first record from stream1 at 1000ms joins with the stream2 record at 4000ms because they are within the 5-second window. The record at 7000ms is outside the 5-second window from 1000ms, so no join occurs for that.
❓ Predict Output
intermediate2:00remaining
Result of KStream-KTable join with missing key
What is the output when joining a KStream with a KTable if the KTable does not have the key present in the KStream record?
Stream record: (key=42, value=foo)
KTable contains keys: 1, 2, 3 only
Join function concatenates stream and table values with a dash.
Stream record: (key=42, value=foo)
KTable contains keys: 1, 2, 3 only
Join function concatenates stream and table values with a dash.
Kafka
KStream<Integer, String> stream = builder.stream("stream-topic"); KTable<Integer, String> table = builder.table("table-topic"); KStream<Integer, String> joined = stream.leftJoin( table, (streamVal, tableVal) -> streamVal + "-" + (tableVal == null ? "null" : tableVal) ); joined.to("joined-output");
Attempts:
2 left
💡 Hint
Left join emits stream records even if table key is missing, with null for table value.
✗ Incorrect
A left join emits all stream records. If the KTable key is missing, the table value is null, so the join function concatenates 'foo-null'.
🔧 Debug
advanced2:00remaining
Identify the error in KStream-KStream join code
What error will this Kafka Streams code produce?
Code snippet:
```java KStream stream1 = builder.stream("s1");
KStream stream2 = builder.stream("s2");
KStream joined = stream1.join(
stream2,
(v1, v2) -> v1 + v2,
JoinWindows.of(Duration.ofSeconds(10))
);
joined.to("out");
```
Code snippet:
```java KStream
Kafka
Same as promptAttempts:
2 left
💡 Hint
Check the Kafka Streams API for JoinWindows methods in recent versions.
✗ Incorrect
In Kafka Streams 3.0+, JoinWindows.of(Duration) is deprecated and replaced by ofTimeDifferenceWithNoGrace. Using the deprecated method causes a compilation warning or error depending on version.
📝 Syntax
advanced2:00remaining
Syntax error in KStream-KTable join code
Which option contains the syntax error in this KStream-KTable join snippet?
Kafka
KStream<String, String> stream = builder.stream("stream"); KTable<String, String> table = builder.table("table"); KStream<String, String> joined = stream.join( table, (streamVal, tableVal) -> streamVal + tableVal ); joined.to("output");
Attempts:
2 left
💡 Hint
Check Kafka Streams API for KStream-KTable join method names.
✗ Incorrect
KStream does not have a join method that takes a KTable directly. Instead, it has leftJoin and join (inner join) methods. The method join() without specifying join type does not exist, causing a syntax error.
🚀 Application
expert3:00remaining
Determine the number of output records from a complex join scenario
You have a KStream of orders and a KTable of customers. The KStream has 3 records with keys: 10, 20, 30. The KTable has customer data for keys: 10 and 30 only.
You perform a left join of the KStream with the KTable.
How many records will be emitted to the output topic?
You perform a left join of the KStream with the KTable.
How many records will be emitted to the output topic?
Kafka
KStream<Integer, String> orders = builder.stream("orders"); KTable<Integer, String> customers = builder.table("customers"); KStream<Integer, String> joined = orders.leftJoin( customers, (orderVal, customerVal) -> orderVal + ":" + (customerVal == null ? "unknown" : customerVal) ); joined.to("orders-with-customers");
Attempts:
2 left
💡 Hint
Left join emits all stream records regardless of table key presence.
✗ Incorrect
A left join emits all records from the stream. Even if the KTable lacks a key, the stream record is emitted with null for the table value. So all 3 stream records produce output.