Bird
Raised Fist0

Identify the error in this Kafka SDK consumer code snippet:

medium📝 Debug Q14 of Q15
Kafka - with Java/Python
Identify the error in this Kafka SDK consumer code snippet:
consumer = KafkaConsumer('my_topic', bootstrap_servers='localhost:9092')
for message in consumer:
    print(message.value.decode('utf-8'))
consumer.close()
AIncorrect method to decode message value
BTopic name should be a list, not a string
CConsumer.close() should be called before the loop
DMissing group_id parameter in consumer initialization
Step-by-Step Solution
Solution:
  1. Step 1: Check KafkaConsumer topic parameter type

    The KafkaConsumer expects a list of topics, not a single string.
  2. Step 2: Identify the correct topic argument format

    Passing 'my_topic' as a string causes an error; it should be ['my_topic'].
  3. Final Answer:

    Topic name should be a list, not a string -> Option B
  4. Quick Check:

    Topic parameter must be list = Topic name should be a list, not a string [OK]
Quick Trick: Pass topics as list, not string [OK]
Common Mistakes:
MISTAKES
  • Omitting group_id is allowed but not error here
  • Decoding message value with decode('utf-8') is correct
  • Calling close() after loop is correct

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes