Skip to content
Advertisement

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);
        }
    }

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement