Skip to content

Why is there a “topic” parameter in the overridden serialize() method from Serializer interface in org.apache.kafka.common.serialization

I have observed that implementations of the method serialize() of the Serializer<> interface has two parameters:

byte[] serialize(String topic, T data)

but the method body does not require String topic parameter at all. So why does it exist?

Sample Implementation available in the package org.apache.kafka.common.serialization:

@Override
    public byte[] serialize(String topic, String data) {
        try {
            if (data == null)
                return null;
            else
                return data.getBytes(encoding);
        } catch (UnsupportedEncodingException e) {
            throw new SerializationException("Error when serializing string to byte[] due to unsupported encoding " + encoding);
        }
    }

Answer

In this specific implementation, indeed there’s no usage. Yet, this parameter may be used by different / advanced Serializers.