Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to commit the offset after processing the message.
Kafka
consumer.commitSync([1]); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing true or false instead of offsets causes errors.
Not committing offsets can lead to message reprocessing.
✗ Incorrect
The commitSync method requires the offsets to commit, so 'offsets' is the correct argument.
2fill in blank
mediumComplete the code to enable auto-commit for at-least-once delivery.
Kafka
"enable.auto.commit": [1],
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using string 'true' instead of boolean true causes config issues.
Setting auto-commit to false disables automatic commits.
✗ Incorrect
Setting 'enable.auto.commit' to true enables automatic offset commits for at-least-once delivery.
3fill in blank
hardFix the error in the code to process messages and commit offsets synchronously.
Kafka
while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { process(record); } consumer.[1](); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using commitAsync() can cause message duplication.
commit() and commitOffset() are not valid Kafka consumer methods.
✗ Incorrect
commitSync() commits offsets synchronously, ensuring at-least-once delivery guarantees.
4fill in blank
hardFill both blanks to create a dictionary comprehension that stores offsets for partitions with offset greater than 5.
Kafka
Map<TopicPartition, OffsetAndMetadata> offsets = records.partitions().stream()
.collect(Collectors.toMap(
partition -> partition,
partition -> new OffsetAndMetadata([1])
));
if (offsets.entrySet().stream().anyMatch(entry -> entry.getValue().offset() [2] 5)) {
consumer.commitSync(offsets);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the first record's offset instead of the last.
Using '<' instead of '>' in the condition.
✗ Incorrect
The offset stored is the last record's offset plus one, and the condition checks if offset is greater than 5.
5fill in blank
hardFill all three blanks to create a Python dictionary comprehension that stores offsets for words longer than 3 characters.
Kafka
offsets = {word[1]: [2] for word in words if len(word) [3] 3} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>' in the condition.
Using the word itself instead of its uppercase form as key.
✗ Incorrect
The comprehension uses the uppercase word as key, length as value, and filters words longer than 3 characters.