Complete the code to set the partition key when sending a message.
producer.send('my_topic', key=[1], value='Hello World')
The partition key must be bytes, so prefixing with b'' is required.
Complete the code to specify the partition explicitly when sending a message.
producer.send('my_topic', partition=[1], key=b'user1', value='Data')
The partition number must be an integer, not a string.
Fix the error in the code to correctly assign the partition key for routing.
producer.send('topic', key=[1], value='msg')
The key must be bytes for Kafka to route correctly, so b'user_id' is correct.
Fill both blanks to create a dictionary comprehension that maps keys to partitions based on key length.
partition_map = {key: len(key) [1] for key in keys if key [2] 'abc'}The modulo operator % 2 is used to assign partitions, and == filters keys equal to 'abc'.
Fill all three blanks to create a dictionary comprehension that routes messages to partitions based on key length and filters keys starting with 'k'.
routing = [1]: len(k) [2] for k in keys if k.startswith([3])
The dictionary keys are k, partition is len(k) % 3, and filter uses 'k' string.