Bird
0
0

Identify the error in this Kafka consumer offset commit code snippet:

medium📝 Debug Q6 of 15
Kafka - Consumers
Identify the error in this Kafka consumer offset commit code snippet:
consumer.commitSync(Collections.singletonMap(
  new TopicPartition("topic1", 0),
  new OffsetAndMetadata(5)
));
AOffsetAndMetadata requires timestamp parameter
BTopicPartition is missing partition number
CcommitSync does not accept a map argument
DOffset should be 6 to commit next message
Step-by-Step Solution
Solution:
  1. Step 1: Understand offset commit semantics

    Offsets are zero-based; committing offset 5 means next message to read is offset 5, so to commit up to message 5, offset should be 6.
  2. Step 2: Check method usage and parameters

    commitSync accepts a map with TopicPartition and OffsetAndMetadata; TopicPartition is correctly specified; OffsetAndMetadata does not require timestamp.
  3. Final Answer:

    Offset should be 6 to commit next message -> Option D
  4. Quick Check:

    Commit offset = next message offset [OK]
Quick Trick: Commit offset = next message to read, not last processed [OK]
Common Mistakes:
  • Committing current message offset instead of next
  • Misunderstanding TopicPartition constructor
  • Adding unnecessary timestamp to OffsetAndMetadata

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kafka Quizzes