0
0
Kafkadevops~10 mins

Message key and value 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 send a message with a key in Kafka producer.

Kafka
producer.send('my_topic', key=[1], value=b'My message')
Drag options to blanks, or click blank then click option'
Ab'key1'
B'key1'
C123
DNone
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without b prefix causes type errors.
Using None as key when a key is expected.
2fill in blank
medium

Complete the code to deserialize the message value as a UTF-8 string.

Kafka
message_value = record.value().[1]('utf-8')
Drag options to blanks, or click blank then click option'
AtoString
Bdecode
Cencode
Dparse
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode instead of decode reverses the operation.
Using parse or toString are not valid methods on bytes.
3fill in blank
hard

Fix the error in the code to correctly set the message key as a string converted to bytes.

Kafka
key_str = 'user123'
producer.send('topic', key=[1], value=b'data')
Drag options to blanks, or click blank then click option'
Abytes(key_str)
Bkey_str
Ckey_str.encode()
Dstr(key_str)
Attempts:
3 left
💡 Hint
Common Mistakes
Passing string directly causes type error.
Using bytes(string) without encoding causes error.
Using str() returns string, not bytes.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension mapping keys to values from a list of tuples.

Kafka
message_dict = { [1]: [2] for [1], [2] in records }
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Crecord
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable for key and value.
Using wrong variable names that don't match unpacking.
5fill in blank
hard

Fill all three blanks to filter messages with key length greater than 3 and create a dict of key-value pairs.

Kafka
filtered = { [1]: [2] for [1], [2] in messages if len([3]) > 3 }
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in comprehension.
Filtering by length of value instead of key.