简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM