Bird
Raised Fist0

You want to consume messages from multiple topics ['topicA', 'topicB'] and process only messages from topicB. Which code snippet correctly implements this?

hard🚀 Application Q8 of Q15
Kafka - with Java/Python

You want to consume messages from multiple topics ['topicA', 'topicB'] and process only messages from topicB. Which code snippet correctly implements this?

Aconsumer.assign(['topicA', 'topicB']) for msg in consumer: if msg.topic == 'topicB': print(msg.value.decode())
Bconsumer.subscribe('topicB') for msg in consumer: print(msg.value.decode())
Cconsumer.subscribe(['topicA', 'topicB']) for msg in consumer: if msg.topic == 'topicB': print(msg.value.decode())
Dconsumer.subscribe(['topicA', 'topicB']) for msg in consumer: print(msg.value.decode())
Step-by-Step Solution
Solution:
  1. Step 1: Subscribe to both topics

    Use subscribe(['topicA', 'topicB']) to listen to both topics.
  2. Step 2: Filter messages by topic

    Inside the loop, check if msg.topic == 'topicB' before processing.
  3. Final Answer:

    Subscribe to both and filter inside loop -> Option C
  4. Quick Check:

    Subscribe multiple, filter by topic [OK]
Quick Trick: Subscribe multiple topics, filter messages by topic [OK]
Common Mistakes:
MISTAKES
  • Subscribing only to one topic when multiple needed
  • Using assign() incorrectly for topics
  • Not filtering messages by topic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes