Bird
Raised Fist0

Identify the error in this Kafka consumer code snippet:

medium📝 Debug Q14 of Q15
Kafka - with Java/Python

Identify the error in this Kafka consumer code snippet:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "group1");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Collections.singletonList("my-topic"));
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
    System.out.println(record.value());
}
Agroup.id property is missing
Bsubscribe() should use assign()
CDeserializers are incorrectly set
Dpoll() argument should be Duration, not int
Step-by-Step Solution
Solution:
  1. Step 1: Check poll() method signature

    In modern Kafka clients, poll requires a Duration argument, not an int.
  2. Step 2: Identify incorrect argument type

    The code uses poll(100) (int), should be poll(Duration.ofMillis(100)).
  3. Final Answer:

    poll() argument should be Duration, not int -> Option D
  4. Quick Check:

    poll() needs Duration argument [OK]
Quick Trick: Use Duration.ofMillis() with poll(), not plain int [OK]
Common Mistakes:
MISTAKES
  • Passing int to poll() instead of Duration
  • Missing group.id property
  • Using assign() instead of subscribe()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes