0
0
Kafkadevops~10 mins

Consumer API basics 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.

Kafka
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", [1]);
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.LongDeserializer"
B"org.apache.kafka.common.serialization.StringDeserializer"
C"org.apache.kafka.common.serialization.IntegerDeserializer"
D"org.apache.kafka.common.serialization.ByteArrayDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a deserializer that does not match the message value type.
Forgetting to specify the value deserializer.
2fill in blank
medium

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

Kafka
consumer.[1](Collections.singletonList("my-topic"));
Drag options to blanks, or click blank then click option'
AcommitSync
Bassign
Cpoll
Dsubscribe
Attempts:
3 left
💡 Hint
Common Mistakes
Using assign instead of subscribe for topic subscription.
Calling poll before subscribing.
3fill in blank
hard

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

Kafka
ConsumerRecords<String, String> records = consumer.[1];
Drag options to blanks, or click blank then click option'
Apoll(100)
Bpoll()
Cpoll(Duration.ofMillis(100))
Dpoll(Duration.ofSeconds(1))
Attempts:
3 left
💡 Hint
Common Mistakes
Passing an integer directly to poll instead of a Duration.
Using poll() without arguments which is deprecated.
4fill in blank
hard

Fill both blanks to process each record's key and value inside the poll loop.

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
Coffset
Dpartition
Attempts:
3 left
💡 Hint
Common Mistakes
Using offset or partition instead of key or value to print message content.
Mixing up key and value fields.
5fill in blank
hard

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

Kafka
try {
    consumer.[1]();
} catch (Exception e) {
    System.out.println("Commit failed: ");
    e.[2]();
} finally {
    consumer.[3]();
}
Drag options to blanks, or click blank then click option'
AcommitSync
BgetMessage
Cclose
DprintStackTrace
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitAsync() instead of commitSync() here.
Calling getMessage() on exception instead of printStackTrace().
Forgetting to close the consumer.