📖 Scenario: You are working on a Kafka application that sends and receives messages containing simple user data. To handle this data properly, you need to create a custom serializer and deserializer (SerDes) that convert user objects to bytes and back.
🎯 Goal: Build a custom SerDes for Kafka that serializes and deserializes a User object with name and age fields.
📋 What You'll Learn
Create a User class with name and age fields
Create a custom serializer class called UserSerializer
Create a custom deserializer class called UserDeserializer
Create a UserSerDe class that implements Kafka's Serializer and Deserializer interfaces
Test the serialization and deserialization by converting a User object to bytes and back
💡 Why This Matters
🌍 Real World
Custom SerDes are used in Kafka applications to efficiently convert complex objects to bytes for transmission and back to objects for processing.
💼 Career
Understanding how to create custom SerDes is important for developers working with Kafka to handle domain-specific data formats and optimize performance.
Progress0 / 4 steps
1
Create the User class
Create a User class with two fields: name of type String and age of type int. Include a constructor to set these fields and override toString() to display the user details.
Kafka
Hint
Define a class with private fields, a constructor, getters, and override toString() to show name and age.
2
Create the UserSerializer class
Create a class called UserSerializer that implements Kafka's Serializer<User> interface. Implement the serialize method to convert a User object into a byte array by encoding the name as UTF-8 bytes, followed by the age as 4 bytes in big-endian order.
Kafka
Hint
Use ByteBuffer to combine the length of the name, the name bytes, and the age as bytes.
3
Create the UserDeserializer class
Create a class called UserDeserializer that implements Kafka's Deserializer<User> interface. Implement the deserialize method to read the byte array, extract the length of the name, read the name bytes as UTF-8 string, and then read the age as an integer to create a new User object.
Kafka
Hint
Use ByteBuffer to read the name length, then the name bytes, then the age integer.
4
Test the UserSerDe serialization and deserialization
Create a User object with name "Alice" and age 30. Use UserSerializer to serialize it to bytes, then use UserDeserializer to deserialize it back to a User object. Finally, print the deserialized User object using System.out.println.
Kafka
Hint
Create a User object, serialize it, then deserialize it, and print the result.
Practice
(1/5)
1.
What is the main purpose of a custom SerDes in Kafka?
easy
A. To manage Kafka topic partitions
B. To speed up Kafka brokers
C. To control how data is converted to bytes and back
D. To monitor Kafka consumer lag
Solution
Step 1: Understand SerDes role
SerDes stands for serializer-deserializer, which converts data to bytes and back.
Step 2: Identify main purpose
Custom SerDes lets you control this conversion for special or complex data.
Final Answer:
To control how data is converted to bytes and back -> Option C
Quick Check:
Custom SerDes = Data conversion control [OK]
Hint: SerDes = Serializer + Deserializer for data [OK]
Common Mistakes:
Confusing SerDes with Kafka broker settings
Thinking SerDes manages partitions
Assuming SerDes monitors consumer lag
2.
Which interface must you implement to create a custom serializer in Kafka?
easy
A. Serializer<T>
B. Consumer<T>
C. Producer<T>
D. Deserializer<T>
Solution
Step 1: Recall serializer interface
Kafka requires implementing Serializer<T> to convert objects to bytes.
Step 2: Match interface to purpose
Deserializer<T> is for reading bytes back, not serialization.
Final Answer:
Serializer<T> -> Option A
Quick Check:
Serializer interface = Serializer<T> [OK]
Hint: Serializer interface handles object to bytes [OK]
Common Mistakes:
Choosing Deserializer for serialization
Confusing Producer/Consumer with SerDes
Using wrong generic interface
3.
Given this serializer code snippet, what will serialize() return for input "hello"?
public byte[] serialize(String topic, String data) {
return data == null ? null : data.getBytes(StandardCharsets.UTF_8);
}
medium
A. [104, 101, 108, 108, 111]
B. null
C. ["h", "e", "l", "l", "o"]
D. Throws NullPointerException
Solution
Step 1: Understand serialize method
If data is not null, it returns UTF-8 bytes of the string.
Step 2: Convert "hello" to bytes
"hello" in UTF-8 bytes is [104, 101, 108, 108, 111].