Bird
Raised Fist0

Which code snippet correctly builds this?

hard🚀 Application Q8 of Q15
Kafka - Streams
You want to create a topology that reads from two topics, merges their streams, filters messages with key starting with 'A', and writes to a single output topic. Which code snippet correctly builds this?
AKStream<String,String> s1 = builder.stream("topic1"); KStream<String,String> s2 = builder.stream("topic2"); KStream<String,String> merged = s1.merge(s2); merged.filter((k,v) -> k.startsWith("A")).to("output");
BKStream<String,String> merged = builder.stream("topic1", "topic2"); merged.filter((k,v) -> v.startsWith("A")).to("output");
CKStream<String,String> s1 = builder.stream("topic1"); KStream<String,String> s2 = builder.stream("topic2"); s1.filter((k,v) -> k.startsWith("A")).to("output"); s2.to("output");
Dbuilder.stream("topic1").filter((k,v) -> k.startsWith("A")).to("output"); builder.stream("topic2").filter((k,v) -> k.startsWith("A")).to("output");
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct merging of two streams

    KStream s1 = builder.stream("topic1"); KStream s2 = builder.stream("topic2"); KStream merged = s1.merge(s2); merged.filter((k,v) -> k.startsWith("A")).to("output"); merges two streams explicitly using merge().
  2. Step 2: Check filtering and output

    Filter is applied on merged stream, then sent to one output topic.
  3. Step 3: Exclude incorrect options

    KStream merged = builder.stream("topic1", "topic2"); merged.filter((k,v) -> v.startsWith("A")).to("output"); incorrectly filters on value instead of key; C and D write separately, not merged.
  4. Final Answer:

    Explicitly merges two streams using merge(), filters keys starting with 'A', and writes to output -> Option A
  5. Quick Check:

    Merge streams then filter and output [OK]
Quick Trick: Use merge() to combine streams before filtering [OK]
Common Mistakes:
MISTAKES
  • Trying to stream multiple topics in one call
  • Writing streams separately instead of merged
  • Filtering before merging incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes