0
0
Kafkadevops~10 mins

Custom SerDes in Kafka - Commands & Configuration

Choose your learning style9 modes available
Introduction
When sending data through Kafka, it needs to be turned into bytes and back again. Custom SerDes let you control exactly how your data is changed to bytes and back, so you can use your own formats or special data types.
When you want to send complex objects like JSON or Avro with your own rules.
When the default Kafka serializers don't match your data format needs.
When you want to optimize data size or speed by customizing serialization.
When you need to ensure compatibility between different versions of your data.
When integrating Kafka with systems that require specific data encoding.
Config File - CustomSerdesExample.java
CustomSerdesExample.java
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.

Commands
Compile the custom serializer/deserializer Java class with Kafka client library in the classpath.
Terminal
javac -cp kafka-clients-3.5.1.jar CustomSerdesExample.java
Expected OutputExpected
No output (command runs silently)
Start a Kafka producer using the custom serializer for message values to send data to 'example-topic'.
Terminal
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
Expected OutputExpected
No output (command runs silently)
--property value.serializer=CustomSerdesExample - Use the custom serializer for message values
Start a Kafka consumer using the custom deserializer for message values to read data from 'example-topic'.
Terminal
kafka-console-consumer --bootstrap-server localhost:9092 --topic example-topic --from-beginning --property key.deserializer=org.apache.kafka.common.serialization.StringDeserializer --property value.deserializer=CustomSerdesExample
Expected OutputExpected
hello world
--property value.deserializer=CustomSerdesExample - Use the custom deserializer for message values
Key Concept

If 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.

Common Mistakes
Not including the custom SerDes class in the classpath when running producer or consumer.
Kafka won't find your custom serializer/deserializer and will fail to start or throw errors.
Always compile your custom SerDes and include its jar or class files in the classpath when running Kafka clients.
Using mismatched serializer and deserializer classes between producer and consumer.
Data will not deserialize correctly, causing errors or corrupted data.
Ensure the producer and consumer use matching custom SerDes classes.
Not handling null values in serialize or deserialize methods.
Null pointer exceptions can crash your Kafka clients.
Always check for null and handle it gracefully in your custom SerDes methods.
Summary
Write a Java class implementing Kafka Serializer and Deserializer interfaces to define custom data conversion.
Compile the class and include it in the Kafka client classpath when running producers and consumers.
Use the --property flags to specify your custom serializer and deserializer classes for Kafka clients.