0
0
Kafkadevops~10 mins

Partition key and routing 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 set the partition key when sending a message.

Kafka
producer.send('my_topic', key=[1], value='Hello World')
Drag options to blanks, or click blank then click option'
A123user
B'user123'
Cb'user123'
Duser123
Attempts:
3 left
💡 Hint
Common Mistakes
Using a plain string without bytes prefix causes an error.
Using an invalid key format.
2fill in blank
medium

Complete the code to specify the partition explicitly when sending a message.

Kafka
producer.send('my_topic', partition=[1], key=b'user1', value='Data')
Drag options to blanks, or click blank then click option'
ANone
B'partition0'
C'0'
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an integer for partition.
Passing None which means no explicit partition.
3fill in blank
hard

Fix the error in the code to correctly assign the partition key for routing.

Kafka
producer.send('topic', key=[1], value='msg')
Drag options to blanks, or click blank then click option'
Ab'user_id'
Buser_id
C'user_id'
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without bytes prefix.
Using a variable without encoding.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps keys to partitions based on key length.

Kafka
partition_map = {key: len(key) [1] for key in keys if key [2] 'abc'}
Drag options to blanks, or click blank then click option'
A% 2
B==
C!=
D// 2
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators.
Using floor division instead of modulo.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that routes messages to partitions based on key length and filters keys starting with 'k'.

Kafka
routing = [1]: len(k) [2] for k in keys if k.startswith([3])
Drag options to blanks, or click blank then click option'
Ak
B% 3
C'k'
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Using incorrect string for startswith.