Challenge - 5 Problems
Kafka Java Consumer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kafka consumer poll loop?
Consider the following Java Kafka consumer code snippet. What will be printed to the console when the consumer polls messages from the topic?
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", "org.apache.kafka.common.serialization.StringDeserializer"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singletonList("my-topic")); ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000)); for (ConsumerRecord<String, String> record : records) { System.out.println("Received message: " + record.value()); } consumer.close();
Attempts:
2 left
💡 Hint
Assume the topic 'my-topic' has two messages: "Hello" and "World".
✗ Incorrect
The consumer subscribes to 'my-topic' and polls messages. Since the topic contains two messages, the loop prints each message's value. The group.id is set, so no runtime error occurs. Imports are assumed present.
🧠 Conceptual
intermediate1:30remaining
What does the 'auto.offset.reset' property control in a Kafka consumer?
In Kafka consumer configuration, what is the role of the 'auto.offset.reset' property?
Attempts:
2 left
💡 Hint
Think about what happens if the consumer group is new or the offset is lost.
✗ Incorrect
The 'auto.offset.reset' property tells the consumer where to start reading if no committed offset exists or the offset is out of range. Common values are 'earliest' or 'latest'.
🔧 Debug
advanced2:30remaining
Why does this Kafka consumer code throw a SerializationException?
Examine the code below. Why does it throw a SerializationException at runtime?
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "group1"); props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer"); // Missing value.deserializer property KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.subscribe(Collections.singletonList("topic1")); ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));
Attempts:
2 left
💡 Hint
Check the deserializer properties carefully.
✗ Incorrect
Kafka requires both key and value deserializers to convert bytes to objects. Missing value.deserializer causes SerializationException when reading message values.
📝 Syntax
advanced1:30remaining
Which option correctly creates a Kafka consumer with generic types for String keys and Integer values?
Select the correct Java code snippet that creates a KafkaConsumer instance with String keys and Integer values.
Attempts:
2 left
💡 Hint
Consider Java's diamond operator and generic type matching.
✗ Incorrect
Option C uses the diamond operator <> to infer types correctly. Option C is valid Java but verbose. Option C lacks generics on constructor, causing raw type warning. Option C swaps key and value types incorrectly.
🚀 Application
expert3:00remaining
How many partitions will the consumer group consume concurrently with this configuration?
Given a Kafka topic with 4 partitions and a consumer group with 3 consumers, how many partitions will each consumer consume concurrently?
Attempts:
2 left
💡 Hint
Partitions are divided among consumers in a group, but not always evenly.
✗ Incorrect
Kafka assigns partitions to consumers in a group as evenly as possible. With 4 partitions and 3 consumers, two consumers get 1 partition each, and one consumer gets 2 partitions.