0
0
Kafkadevops~10 mins

Partition assignment in Kafka - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A0
B1
Cpartition
Dtopic
Attempts:
3 left
💡 Hint
Common Mistakes
Using the topic name instead of a partition number.
Using a variable name that is not defined.
2fill in blank
medium

Complete 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'
Apartition
Bmy_topic
Ctopic
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a string literal.
Passing the partition number instead of the topic name.
3fill in blank
hard

Fix 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'
Atopic
Bpartition
Ci
Dmessage
Attempts:
3 left
💡 Hint
Common Mistakes
Using the partition variable instead of the index.
Using the topic name as an index.
4fill in blank
hard

Fill 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'
A[:]
B.keys()
C.items()
D.values()
Attempts:
3 left
💡 Hint
Common Mistakes
Using .keys() instead of .items() for iteration.
Not copying the list of messages.
5fill in blank
hard

Fill 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'
A:
B>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' or '!=' in conditions.
Using ':' incorrectly in dictionary comprehension.