Complete the code to poll messages from the Kafka consumer.
ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100));
The poll method is used to fetch data from Kafka topics.
Complete the code to iterate over the polled records.
for (ConsumerRecord<String, String> record : [1]) { System.out.println(record.value()); }
The records variable holds the polled messages to iterate over.
Fix the error in committing offsets synchronously after processing records.
consumer.[1]();commitSync() commits the offsets synchronously to Kafka after processing.
Fill both blanks to create a consumer poll loop that processes messages and commits offsets.
while (true) { ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.value()); } consumer.[2](); }
The loop polls messages with poll() and commits offsets synchronously with commitSync().
Fill all three blanks to create a consumer poll loop that subscribes to a topic, polls messages, and commits offsets.
consumer.[1](Collections.singletonList("my-topic")); while (true) { ConsumerRecords<String, String> records = consumer.[2](Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.value()); } consumer.[3](); }
The consumer first subscribes to the topic with subscribe(), then polls messages with poll(), and finally commits offsets synchronously with commitSync().