Search code examples
javaserializationapache-kafkadeserialization

What does configure function do in Kafka serializers / deserializers?


I am looking into implementing custom Serializers / Deserializers for Kafka. To implement a Kafka custom Serializer / Deserializer, we have to implement org.apache.kafka.common.serialization.Serializer and org.apache.kafka.common.serialization.Deserializer respectively.

What I don't understand is the configure(Map configs, boolean key) method. What does it do? What should we pass into it? What is the purpose of this?

Most of the examples I came into, have not implemented anything inside the method. But I want to know what exactly this does. Not just ignore it.


Solution

  • As you are trying to implement a custom serializer, the configure method will be used to configure the serializer at the start. If you want to read more about the configure method kindly go through this link: https://kafka.apache.org/20/javadoc/org/apache/kafka/common/serialization/Serializer.html

    void configure​(java.util.Map<java.lang.String,?> configs, boolean isKey)

    Configure this class.

    Parameters: configs - configs in key/value pairs, isKey - whether is for key or value

    Basically, the configure method accepts the Configurations Map as its first argument and second argument a boolean value, which sets true if it is for a Key or false if it is for value.

    From there, these configurations can be stored as fields within de/serializer implementations and used as part of the de/serialize methods. For example, StringSerializer has an encoding property.