Complete the code to serialize a string message before sending it to Kafka.
producer.send(topic, value=[1].encode('utf-8'))
To send a string message to Kafka, you encode it to bytes using encode('utf-8').
Complete the code to serialize a Python dictionary to JSON string before sending.
producer.send(topic, value=[1].encode('utf-8'))
Use json.dumps() to convert a dictionary to a JSON string before encoding.
Fix the error in deserializing a JSON message received from Kafka.
data = json.loads(message.[1]('utf-8'))
Kafka messages are bytes, so decode to string before loading JSON.
Fill both blanks to create a dictionary comprehension that serializes only keys with length greater than 3 to JSON strings.
result = {key: json.dumps(value) for key, value in data.items() if len(key) [1] 3 and key [2] 'id'}We want keys longer than 3 and keys not equal to 'id'.
Fill all three blanks to create a dictionary comprehension that serializes keys in uppercase, values only if they are integers, and filters values greater than 10.
result = [1]: [2] for [3], v in data.items() if isinstance(v, int) and v > 10}
v directly without serialization.Keys are uppercased with k.upper(), values are serialized with json.dumps(v), and k is the variable for keys.