Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to assign a message to a specific partition.
Kafka
producer.send('my_topic', value=message, partition=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the topic name instead of a partition number.
Using a variable name that is not defined.
✗ Incorrect
The partition number must be an integer indicating which partition to send the message to. Here, 0 is a valid partition index.
2fill in blank
mediumComplete the code to get the partition count for a topic.
Kafka
partitions = [tp.partition for tp in client.partitions_for_topic('[1]')]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a string literal.
Passing the partition number instead of the topic name.
✗ Incorrect
You must provide the topic name as a string to get its partitions.
3fill in blank
hardFix the error in the code to assign partitions in a round-robin fashion.
Kafka
partition = partitions[[1] % len(partitions)]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the partition variable instead of the index.
Using the topic name as an index.
✗ Incorrect
The variable 'i' is used as the index to cycle through partitions using modulo.
4fill in blank
hardFill both blanks to create a dictionary mapping partitions to messages.
Kafka
partition_map = {partition: messages[1] for partition, messages in data[2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .keys() instead of .items() for iteration.
Not copying the list of messages.
✗ Incorrect
The slice [:] copies the messages list, and .items() iterates over dictionary key-value pairs.
5fill in blank
hardFill both blanks to filter partitions with more than 5 messages.
Kafka
filtered = {p: m for p, m in partition_map.items() if len(m) [1] 5 and p [2] 0} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' or '!=' in conditions.
Using ':' incorrectly in dictionary comprehension.
✗ Incorrect
Use ':' to separate key and value in dict comprehension, '>' to check message count, and '!=' to exclude partition 0.