Challenge - 5 Problems
Kafka Consumer Mastery
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 configuration code?
Consider the following Kafka consumer configuration snippet in Java. What will be the value of
props.getProperty("enable.auto.commit") after execution?Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "false"); props.put("auto.commit.interval.ms", "1000"); String autoCommit = props.getProperty("enable.auto.commit");
Attempts:
2 left
💡 Hint
Check the value assigned to the key "enable.auto.commit" in the properties.
✗ Incorrect
The property "enable.auto.commit" is explicitly set to "false" in the properties. So, calling getProperty returns "false".
❓ Predict Output
intermediate2:00remaining
What will be the output of this Kafka consumer poll timeout code?
Given the following Kafka consumer poll call, what is the type and meaning of the returned value?
Kafka
Consumer<String, String> consumer = new KafkaConsumer<>(props);
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(500));
System.out.println(records.count());Attempts:
2 left
💡 Hint
The poll method returns records fetched within the timeout duration.
✗ Incorrect
The poll method waits up to the specified timeout to fetch records. The count() method returns how many records were fetched.
❓ Predict Output
advanced2:00remaining
What error does this Kafka consumer configuration cause?
Analyze the following Kafka consumer configuration snippet. What error will occur when creating the consumer?
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "maybe"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
Attempts:
2 left
💡 Hint
The enable.auto.commit property expects "true" or "false" only.
✗ Incorrect
The property "enable.auto.commit" must be "true" or "false". Setting it to "maybe" causes an IllegalArgumentException.
🧠 Conceptual
advanced2:00remaining
How many partitions will the consumer subscribe to after this code?
Given a Kafka topic with 4 partitions, what is the number of partitions assigned to the consumer after this subscription code?
Kafka
consumer.subscribe(Collections.singletonList("my-topic")); Set<TopicPartition> assigned = consumer.assignment(); int count = assigned.size(); System.out.println(count);
Attempts:
2 left
💡 Hint
Partitions are assigned only after poll() is called.
✗ Incorrect
The assignment set is empty until consumer.poll() is called to trigger partition assignment.
❓ Predict Output
expert3:00remaining
What is the output of this Kafka consumer manual commit code?
Consider the following code snippet for manual offset commit. What will be printed?
Kafka
Properties props = new Properties(); props.put("bootstrap.servers", "localhost:9092"); props.put("group.id", "test-group"); props.put("enable.auto.commit", "false"); props.put("auto.offset.reset", "earliest"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props, new StringDeserializer(), new StringDeserializer()); consumer.subscribe(List.of("my-topic")); ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.offset() + ":" + record.value()); } consumer.commitSync(); System.out.println("Committed offsets");
Attempts:
2 left
💡 Hint
Manual commit commits offsets after processing records.
✗ Incorrect
The code polls records, prints each offset and value, then commits offsets synchronously and prints confirmation.