Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string without b prefix causes type errors.
Using None as key when a key is expected.
✗ Incorrect
Kafka message keys must be bytes, so the key should be a bytes object like b'key1'.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using encode instead of decode reverses the operation.
Using parse or toString are not valid methods on bytes.
✗ Incorrect
To convert bytes to string, use decode('utf-8').
3fill in blank
hardFix 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'
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.
✗ Incorrect
To convert a string to bytes, use encode(). bytes() without encoding argument won't work on string.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using same variable for key and value.
Using wrong variable names that don't match unpacking.
✗ Incorrect
In a dict comprehension, use key: value for key, value in records to unpack tuples.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in comprehension.
Filtering by length of value instead of key.
✗ Incorrect
Use key and value for dict comprehension and check length of key for filtering.