Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a Kafka consumer with a group coordinator.
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"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the broker address instead of the group id
Using the topic name as group id
Leaving group.id empty
✗ Incorrect
The group coordinator is identified by the group.id property, which should be a string like "my-group".
2fill in blank
mediumComplete the code to subscribe the consumer to a topic.
Kafka
consumer.[1](Collections.singletonList("my-topic"));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using assign instead of subscribe
Calling poll before subscribing
Using commitSync to subscribe
✗ Incorrect
The subscribe method is used to subscribe the consumer to a list of topics.
3fill in blank
hardFix the error in the code to poll messages from the consumer.
Kafka
ConsumerRecords<String, String> records = consumer.[1](Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println(record.value()); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subscribe instead of poll to get messages
Calling commitSync before polling
Using assign instead of poll
✗ Incorrect
The poll method retrieves messages from the broker within the specified timeout.
4fill in blank
hardFill both blanks to commit offsets synchronously after processing.
Kafka
consumer.[1](Duration.ofMillis(100)); try { consumer.[2](); } catch (Exception e) { e.printStackTrace(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Committing before polling
Using close instead of commitSync
Not handling exceptions during commit
✗ Incorrect
First, poll to get messages, then commitSync to commit offsets synchronously.
5fill in blank
hardFill all three blanks to create a consumer, subscribe, and poll messages.
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"); KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); consumer.[2](Collections.singletonList("my-topic")); ConsumerRecords<String, String> records = consumer.[3](Duration.ofMillis(100));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using topic name as group.id
Calling poll before subscribe
Using assign instead of subscribe
✗ Incorrect
Set group.id to "group1", subscribe to the topic, then poll messages.