Complete the code to enable automatic offset commits in a Kafka consumer.
props.put("enable.auto.commit", [1]);
Setting enable.auto.commit to true enables automatic offset commits.
Complete the code to commit offsets synchronously after processing records.
consumer.[1]();commitSync() commits offsets synchronously, ensuring the commit is complete before proceeding.
Fix the error in the code to commit offsets asynchronously with a callback.
consumer.[1](new OffsetCommitCallback() { public void onComplete(Map<TopicPartition, OffsetAndMetadata> offsets, Exception e) { if (e != null) { System.err.println("Commit failed"); } } });
commitAsync() accepts a callback to handle commit completion or failure asynchronously.
Fill both blanks to create a manual offset commit after processing each record.
for (ConsumerRecord<String, String> record : records) { process(record); consumer.[1](Collections.singletonMap( new TopicPartition(record.topic(), record.partition()), new OffsetAndMetadata(record.offset() + [2]) )); }
Using commitSync() with the offset incremented by 1 commits the next offset to process.
Fill all three blanks to configure a Kafka consumer for manual offset commits with a 5-second commit interval.
props.put("enable.auto.commit", [1]); props.put("auto.commit.interval.ms", [2]); consumer.[3]();
Disabling auto commit (false) and setting the interval to 5000 ms means commits are manual. Calling commitAsync() commits offsets asynchronously.