Custom SerDes in Kafka - Commands & Configuration
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serializer;
import java.nio.charset.StandardCharsets;
public class CustomSerdesExample implements Serializer<String>, Deserializer<String> {
@Override
public byte[] serialize(String topic, String data) {
if (data == null) return null;
return data.toUpperCase().getBytes(StandardCharsets.UTF_8);
}
@Override
public String deserialize(String topic, byte[] data) {
if (data == null) return null;
return new String(data, StandardCharsets.UTF_8).toLowerCase();
}
@Override
public void close() {}
@Override
public void configure(java.util.Map<String, ?> configs, boolean isKey) {}
}
This Java class defines a custom serializer and deserializer for Kafka that changes strings to uppercase bytes when sending and back to lowercase strings when receiving.
serialize: converts the string to uppercase bytes.
deserialize: converts bytes back to lowercase string.
This example shows how you can control exactly how data is turned into bytes and back.
javac -cp kafka-clients-3.5.1.jar CustomSerdesExample.java
kafka-console-producer --broker-list localhost:9092 --topic example-topic --property parse.key=true --property key.serializer=org.apache.kafka.common.serialization.StringSerializer --property value.serializer=CustomSerdesExample--property value.serializer=CustomSerdesExample - Use the custom serializer for message valueskafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --from-beginning --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer --property value.deserializer=CustomSerdesExample--property value.deserializer=CustomSerdesExample - Use the custom deserializer for message valuesIf you remember nothing else from this pattern, remember: custom SerDes let you control exactly how Kafka turns your data into bytes and back, so you can use any format you want.
Practice
What is the main purpose of a custom SerDes in Kafka?
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 CQuick Check:
Custom SerDes = Data conversion control [OK]
- Confusing SerDes with Kafka broker settings
- Thinking SerDes manages partitions
- Assuming SerDes monitors consumer lag
Which interface must you implement to create a custom serializer in Kafka?
Solution
Step 1: Recall serializer interface
Kafka requires implementingSerializer<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 AQuick Check:
Serializer interface = Serializer<T> [OK]
- Choosing Deserializer for serialization
- Confusing Producer/Consumer with SerDes
- Using wrong generic interface
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);
}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].Final Answer:
[104, 101, 108, 108, 111] -> Option AQuick Check:
"hello" bytes = [104,101,108,108,111] [OK]
- Thinking it returns characters array
- Assuming null is returned for non-null input
- Expecting exception on valid input
Identify the error in this deserializer code snippet:
public String deserialize(String topic, byte[] data) {
return new String(data);
}Solution
Step 1: Check String constructor usage
Usingnew String(byte[])without charset uses platform default, which can cause bugs.Step 2: Identify best practice
Always specify charset likeStandardCharsets.UTF_8for consistent decoding.Final Answer:
Missing charset in String constructor -> Option DQuick Check:
Specify charset in String(byte[]) [OK]
- Ignoring charset causes wrong decoding
- Thinking unused parameter is an error
- Assuming return type must be byte[]
You want to send a complex object Person{name, age} through Kafka. Which approach best uses custom SerDes?
- Create a
PersonSerializerthat convertsPersonto JSON bytes. - Create a
PersonDeserializerthat converts JSON bytes back toPerson. - Register these SerDes in Kafka producer and consumer configs.
What is the main benefit of this approach?
Solution
Step 1: Understand custom SerDes for complex objects
Serializing to JSON bytes lets complex objects be sent as byte arrays.Step 2: Recognize benefit of JSON format
JSON is readable and widely supported, making data safe and easy to deserialize.Step 3: Confirm Kafka config usage
Registering SerDes ensures Kafka knows how to convert data on send and receive.Final Answer:
It allows sending complex objects as bytes safely and readably -> Option BQuick Check:
Custom SerDes + JSON = safe complex data transfer [OK]
- Thinking SerDes speeds up Kafka internals
- Assuming SerDes controls topic partitions
- Believing Kafka brokers are not needed
Ready to prove it?
Pass the test and this pattern moves unknown
