Bird
Raised Fist0

You want to consume messages from two Kafka topics topicA and topicB using one Python KafkaConsumer instance. Which code snippet correctly sets this up?

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

You want to consume messages from two Kafka topics topicA and topicB using one Python KafkaConsumer instance. Which code snippet correctly sets this up?

from kafka import KafkaConsumer
consumer = KafkaConsumer(_____)
for msg in consumer:
    print(f"Topic: {msg.topic}, Message: {msg.value.decode()}")
A["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1'
B"topicA,topicB", bootstrap_servers='localhost:9092', group_id='group1'
C"topicA" and "topicB", bootstrap_servers='localhost:9092'
D"topicA"; "topicB", bootstrap_servers='localhost:9092', group_id='group1'
Step-by-Step Solution
Solution:
  1. Step 1: Check how to subscribe to multiple topics

    KafkaConsumer accepts a list of topic names to subscribe to multiple topics.
  2. Step 2: Verify correct syntax for multiple topics

    ["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1' correctly passes a list ["topicA", "topicB"] and required parameters.
  3. Final Answer:

    ["topicA", "topicB"], bootstrap_servers='localhost:9092', auto_offset_reset='earliest', group_id='group1' -> Option A
  4. Quick Check:

    List of topics subscribes to multiple topics [OK]
Quick Trick: Use list of topic names to consume multiple topics [OK]
Common Mistakes:
MISTAKES
  • Passing topics as comma-separated string
  • Using logical operators like and or ; between topics
  • Omitting group_id for consumer group

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes