Bird
Raised Fist0
Kafkadevops~10 mins

Deserialization in Kafka - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to specify the deserializer for the Kafka consumer.

Kafka
props.put("value.deserializer", [1]);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.StringSerializer"
B"org.apache.kafka.common.serialization.IntegerSerializer"
C"org.apache.kafka.common.serialization.StringDeserializer"
D"org.apache.kafka.common.serialization.ByteArraySerializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a serializer class instead of a deserializer.
Confusing key and value deserializers.
2fill in blank
medium

Complete the code to create a Kafka consumer with the correct deserializer for keys.

Kafka
Consumer<String, String> consumer = new KafkaConsumer<>(props, new [1](), new StringDeserializer());
Drag options to blanks, or click blank then click option'
ALongDeserializer
BIntegerDeserializer
CByteArrayDeserializer
DStringDeserializer
Attempts:
3 left
💡 Hint
Common Mistakes
Using a deserializer that does not match the key type.
Using a serializer instead of a deserializer.
3fill in blank
hard

Fix the error in the deserializer class name to correctly deserialize JSON messages.

Kafka
props.put("value.deserializer", [1]);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.JsonDeserializer"
B"org.apache.kafka.common.serialization.JsonSerializer"
C"org.apache.kafka.common.serialization.ByteArraySerializer"
D"org.apache.kafka.common.serialization.StringDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Using serializer class names instead of deserializer.
Using StringDeserializer for JSON data.
4fill in blank
hard

Fill both blanks to configure the consumer to deserialize keys as strings and values as JSON.

Kafka
props.put("key.deserializer", [1]);
props.put("value.deserializer", [2]);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.StringDeserializer"
B"org.apache.kafka.common.serialization.IntegerDeserializer"
C"org.apache.kafka.common.serialization.JsonDeserializer"
D"org.apache.kafka.common.serialization.ByteArrayDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up key and value deserializers.
Using serializer classes instead of deserializers.
5fill in blank
hard

Fill all three blanks to create a Kafka consumer that deserializes keys as strings, values as JSON, and sets the group ID.

Kafka
props.put("key.deserializer", [1]);
props.put("value.deserializer", [2]);
props.put("group.id", [3]);
Drag options to blanks, or click blank then click option'
A"org.apache.kafka.common.serialization.StringDeserializer"
B"org.apache.kafka.common.serialization.JsonDeserializer"
C"my-group-id"
D"org.apache.kafka.common.serialization.IntegerDeserializer"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to set the group ID.
Using serializer classes instead of deserializers.

Practice

(1/5)
1.

What is the main purpose of deserialization in Kafka consumers?

easy
A. To send messages to Kafka topics
B. To compress data before sending to Kafka
C. To convert raw bytes from Kafka into usable data formats
D. To create Kafka topics automatically

Solution

  1. Step 1: Understand Kafka message flow

    Kafka stores messages as raw bytes in topics.
  2. Step 2: Role of deserialization in consumers

    Deserialization converts these raw bytes back into readable data for the consumer application.
  3. Final Answer:

    To convert raw bytes from Kafka into usable data formats -> Option C
  4. Quick Check:

    Deserialization = Convert bytes to data [OK]
Hint: Deserialization means reading bytes as data [OK]
Common Mistakes:
  • Confusing deserialization with serialization
  • Thinking deserialization sends data
  • Assuming deserialization compresses data
2.

Which of the following is the correct way to specify a String deserializer in a Kafka consumer configuration?

props.put("key.deserializer", ?);
easy
A. "org.apache.kafka.common.serialization.StringDeserializer"
B. "org.apache.kafka.common.serialization.IntegerSerializer"
C. "org.apache.kafka.common.serialization.ByteArrayDeserializer"
D. "org.apache.kafka.common.serialization.StringSerializer"

Solution

  1. Step 1: Identify deserializer for String keys

    The correct class for String deserialization is StringDeserializer.
  2. Step 2: Check option correctness

    "org.apache.kafka.common.serialization.StringDeserializer" uses StringDeserializer, which matches the key.deserializer property.
  3. Final Answer:

    "org.apache.kafka.common.serialization.StringDeserializer" -> Option A
  4. Quick Check:

    String deserializer = StringDeserializer [OK]
Hint: Deserializer ends with 'Deserializer', not 'Serializer' [OK]
Common Mistakes:
  • Using Serializer instead of Deserializer
  • Mixing IntegerSerializer with deserializer property
  • Choosing ByteArrayDeserializer for String data
3.

Given the following Kafka consumer code snippet, what will be the output if the message value is a JSON string but the deserializer is set to StringDeserializer?

props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
ConsumerRecord<String, String> record = consumer.poll(Duration.ofMillis(100)).iterator().next();
System.out.println(record.value());
medium
A. Binary data printed as garbage characters
B. The JSON string as plain text
C. Null value printed
D. A deserialization error exception

Solution

  1. Step 1: Understand StringDeserializer behavior

    StringDeserializer converts bytes to a UTF-8 string without parsing JSON.
  2. Step 2: Output of printing record.value()

    The JSON string is printed as plain text exactly as received.
  3. Final Answer:

    The JSON string as plain text -> Option B
  4. Quick Check:

    StringDeserializer outputs raw string [OK]
Hint: StringDeserializer prints raw string, no JSON parsing [OK]
Common Mistakes:
  • Expecting automatic JSON parsing
  • Thinking deserialization fails on JSON strings
  • Assuming binary garbage output
4.

What is the likely cause of this error in a Kafka consumer?

org.apache.kafka.common.errors.SerializationException: Error deserializing key/value for partition

Given the consumer uses IntegerDeserializer but the producer sends String keys.

medium
A. Mismatch between producer's String keys and consumer's IntegerDeserializer
B. Kafka topic does not exist
C. Consumer group ID is missing
D. Network connection failure

Solution

  1. Step 1: Analyze deserialization error cause

    The error occurs when deserializer cannot convert bytes to expected type.
  2. Step 2: Check key type mismatch

    Producer sends String keys, but consumer expects Integer keys, causing failure.
  3. Final Answer:

    Mismatch between producer's String keys and consumer's IntegerDeserializer -> Option A
  4. Quick Check:

    Deserializer mismatch = SerializationException [OK]
Hint: Producer and consumer deserializers must match data type [OK]
Common Mistakes:
  • Ignoring type mismatch between producer and consumer
  • Blaming unrelated network or topic errors
  • Confusing serialization with deserialization errors
5.

You want to consume messages where the value is a JSON object representing a user with fields id (int) and name (string). Which deserialization approach is best to convert the Kafka message value into a User Java object?

hard
A. Use ByteArrayDeserializer and manually convert bytes to User
B. Use StringDeserializer and cast the string to User
C. Use IntegerDeserializer for the value
D. Use a custom deserializer that parses JSON into a User object

Solution

  1. Step 1: Understand data format and target type

    The message value is JSON, and we want a User Java object.
  2. Step 2: Choose deserialization method

    A custom deserializer can parse JSON and create User instances directly.
  3. Step 3: Evaluate other options

    StringDeserializer returns raw JSON string, casting won't convert to User. IntegerDeserializer is wrong type. ByteArrayDeserializer requires manual parsing.
  4. Final Answer:

    Use a custom deserializer that parses JSON into a User object -> Option D
  5. Quick Check:

    Custom deserializer = JSON to object [OK]
Hint: Custom deserializer parses JSON to Java object directly [OK]
Common Mistakes:
  • Casting String to object without parsing
  • Using wrong deserializer type
  • Manually parsing bytes instead of using deserializer
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard