0
0
Kafkadevops~10 mins

Consumer poll loop in Kafka - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to poll messages from the Kafka consumer.

Kafka
ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100));
Drag options to blanks, or click blank then click option'
AcommitSync
Bsubscribe
Cpoll
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribe instead of poll to fetch messages.
Calling commitSync before polling messages.
2fill in blank
medium

Complete the code to iterate over the polled records.

Kafka
for (ConsumerRecord<String, String> record : [1]) {
    System.out.println(record.value());
}
Drag options to blanks, or click blank then click option'
Arecords
Bconsumer
Ctopic
Dpartition
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to iterate over the consumer object instead of the records.
Using topic or partition which are not iterable collections.
3fill in blank
hard

Fix the error in committing offsets synchronously after processing records.

Kafka
consumer.[1]();
Drag options to blanks, or click blank then click option'
AcommitAsync
BcommitSync
Cpoll
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitAsync when synchronous commit is required.
Calling poll or subscribe instead of commit methods.
4fill in blank
hard

Fill both blanks to create a consumer poll loop that processes messages and commits offsets.

Kafka
while (true) {
    ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100));
    for (ConsumerRecord<String, String> record : records) {
        System.out.println(record.value());
    }
    consumer.[2]();
}
Drag options to blanks, or click blank then click option'
Apoll
Bsubscribe
CcommitSync
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribe instead of poll to fetch messages.
Using commitAsync instead of commitSync when synchronous commit is needed.
5fill in blank
hard

Fill all three blanks to create a consumer poll loop that subscribes to a topic, polls messages, and commits offsets.

Kafka
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]();
}
Drag options to blanks, or click blank then click option'
Asubscribe
Bpoll
CcommitSync
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Calling poll before subscribe.
Using commitAsync instead of commitSync.
Calling close inside the loop.