0
0
Kafkadevops~20 mins

Deserialization in Kafka - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kafka Deserialization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kafka deserialization code?

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?

Kafka
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
A{'name': 'Alice', 'age': 30}
BAlice
Cb'{"name":"Alice","age":30}'
DNone
Attempts:
2 left
💡 Hint

Think about what the value_deserializer does to the message bytes.

🧠 Conceptual
intermediate
1:30remaining
Which deserializer is appropriate for Avro data in Kafka?

You receive Kafka messages encoded in Avro format. Which deserializer should you use to correctly convert the message bytes into usable data?

AA JSON deserializer that decodes UTF-8 strings
BA plain string deserializer that converts bytes to strings
CAn Avro deserializer that uses the schema registry to decode bytes
DA protobuf deserializer that parses protobuf messages
Attempts:
2 left
💡 Hint

Avro data requires schema information to deserialize properly.

🔧 Debug
advanced
2:30remaining
Why does this Kafka consumer raise a deserialization error?

Examine the following Kafka consumer code. It raises a deserialization error when consuming messages. What is the cause?

Kafka
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'])
AThe <code>value_deserializer</code> decodes bytes to string, so calling <code>json.loads</code> again on string causes error
BThe <code>value_deserializer</code> returns a string, but <code>json.loads</code> expects bytes
CThe topic name is incorrect, causing deserialization failure
DThe bootstrap server address is invalid
Attempts:
2 left
💡 Hint

Check the data type passed to json.loads.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this Kafka deserializer setup

Which option contains a syntax error in the Kafka consumer deserializer configuration?

Avalue_deserializer=lambda m: None if m is None else json.loads(m.decode('utf-8'))
Bvalue_deserializer=lambda m: json.loads(m.decode('utf-8'))
Cvalue_deserializer=lambda m: json.loads(m.decode('utf-8')) if m else None
Dvalue_deserializer=lambda m: json.loads(m.decode('utf-8')
Attempts:
2 left
💡 Hint

Look carefully at the parentheses.

🚀 Application
expert
2:00remaining
How many items are in the resulting dictionary after deserialization?

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?

Kafka
import json

message_value = b'{"id":123,"tags":["red","blue"],"active":true}'
data = json.loads(message_value.decode('utf-8'))

result = len(data)
A3
B1
C2
D4
Attempts:
2 left
💡 Hint

Count the keys at the top level of the JSON object.