Consider the following Kafka consumer code snippet that deserializes messages from a topic. What will be printed when a message with value {"name":"Alice","age":30} is consumed?
from kafka import KafkaConsumer import json consumer = KafkaConsumer( 'users', bootstrap_servers=['localhost:9092'], value_deserializer=lambda m: json.loads(m.decode('utf-8')) ) for message in consumer: print(message.value['name']) break
Think about what the value_deserializer does to the message bytes.
The value_deserializer converts the byte string into a Python dictionary. Accessing message.value['name'] prints the string Alice.
You receive Kafka messages encoded in Avro format. Which deserializer should you use to correctly convert the message bytes into usable data?
Avro data requires schema information to deserialize properly.
Avro data is binary and requires a deserializer that can use the schema registry to interpret the bytes correctly.
Examine the following Kafka consumer code. It raises a deserialization error when consuming messages. What is the cause?
from kafka import KafkaConsumer import json consumer = KafkaConsumer( 'events', bootstrap_servers=['localhost:9092'], value_deserializer=lambda m: m.decode('utf-8') ) for message in consumer: data = json.loads(message.value) print(data['event'])
Check the data type passed to json.loads.
The value_deserializer already decodes bytes to string. Calling json.loads again on a string causes a TypeError because json.loads expects a string, but the code tries to decode twice.
Which option contains a syntax error in the Kafka consumer deserializer configuration?
Look carefully at the parentheses.
Option D is missing a closing parenthesis for json.loads, causing a syntax error.
A Kafka message value is a JSON string: {"id":123,"tags":["red","blue"],"active":true}. After deserializing with json.loads, how many top-level keys does the resulting Python dictionary have?
import json message_value = b'{"id":123,"tags":["red","blue"],"active":true}' data = json.loads(message_value.decode('utf-8')) result = len(data)
Count the keys at the top level of the JSON object.
The JSON object has three keys: id, tags, and active. So the length of the dictionary is 3.