0
0
Kafkadevops~10 mins

Java consumer client 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 create a Kafka consumer with the correct group ID.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "[1]");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
Drag options to blanks, or click blank then click option'
Amy-group
Bgroup-123
Cdefault
Dconsumer1
Attempts:
3 left
💡 Hint
Common Mistakes
Using an empty string as group ID
Using an invalid property key
2fill in blank
medium

Complete the code to subscribe the consumer to a topic named 'test-topic'.

Kafka
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.[1](Collections.singletonList("test-topic"));
Drag options to blanks, or click blank then click option'
Asubscribe
Bconnect
Clisten
Dassign
Attempts:
3 left
💡 Hint
Common Mistakes
Using assign() instead of subscribe()
Using a non-existent method like listen()
3fill in blank
hard

Fix the error in the code to correctly poll messages from Kafka.

Kafka
ConsumerRecords<String, String> records = consumer.[1](100);
Drag options to blanks, or click blank then click option'
Apoll
Bfetch
Cread
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using fetch() or read() instead of poll()
Passing wrong argument types to poll()
4fill in blank
hard

Fill both blanks to process each record and print its key and value.

Kafka
for (ConsumerRecord<String, String> record : records) {
    System.out.println("Key: " + record.[1]() + ", Value: " + record.[2]());
}
Drag options to blanks, or click blank then click option'
Akey
Bvalue
CgetKey
DgetValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using key() or value() methods which do not exist
Trying to access key or value as public fields
5fill in blank
hard

Fill all three blanks to commit offsets synchronously after processing records.

Kafka
try {
    consumer.[1]();
} catch ([2] e) {
    e.[3]();
}
Drag options to blanks, or click blank then click option'
AcommitSync
BcommitAsync
CprintStackTrace
DInterruptedException
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitAsync() instead of commitSync()
Catching wrong exception types
Not printing the stack trace in catch block