Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set the consumer group ID for Kafka consumer.
Kafka
props.put("group.id", [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using server or topic names instead of a group ID.
Leaving the group.id property unset.
✗ Incorrect
The group.id property sets the consumer group ID, which is required for rebalancing behavior.
2fill in blank
mediumComplete the code to subscribe the consumer to a topic named "orders".
Kafka
consumer.subscribe(Collections.[1]("orders"));
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using emptyList which subscribes to no topics.
Using singleton which is not a method in Collections.
✗ Incorrect
The subscribe method expects a collection of topics. singletonList creates a list with one element.
3fill in blank
hardFix the error in the rebalance listener method signature.
Kafka
consumer.subscribe(topics, new ConsumerRebalanceListener() {
@Override
public void [1](Collection<TopicPartition> partitions) {
// handle partitions revoked
}
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
// handle partitions assigned
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like partitionsRevoked or onRebalance.
Confusing assigned and revoked methods.
✗ Incorrect
The correct method name is onPartitionsRevoked to handle revoked partitions during rebalance.
4fill in blank
hardFill both blanks to commit offsets synchronously after partitions are assigned.
Kafka
public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
consumer.[1]();
consumer.[2](offsets);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using poll instead of commit methods.
Calling seek instead of commit.
✗ Incorrect
commitSync() commits the current offsets synchronously; commitSync(offsets) commits specific offsets synchronously.
5fill in blank
hardFill all three blanks to implement a rebalance listener that commits offsets and seeks to correct positions.
Kafka
consumer.subscribe(topics, new ConsumerRebalanceListener() {
@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
consumer.[1]();
}
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
for (TopicPartition partition : partitions) {
consumer.[2](partition, offsets.get(partition));
}
consumer.[3]();
}
}); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using poll instead of commit or seek.
Not committing offsets before rebalance.
✗ Incorrect
On partitions revoked, commitSync is called to save offsets. On assigned, seek moves to saved offsets, then commitAsync commits asynchronously.