This is an implementation of Kafka Avro Serializer using Apache Jackson. In comparison to the official Kafka Avro Serializer it has the following advantages:
- No code generation. No need to use ugly generated classes. You can use nice POJOs, object oriented programming.
- Full power of Jackson
- First-class support for Kotlin
Of course, code generation has its advantages. It takes care of configuration. Without it, we need to solve the configuration in some other way.
When serializing, we know the topic we want to send the message to and we have the object to be serialized. From that, we need to get the schema to use for serialization and subject to verify the schema in the schema registry.
You can use DefaultJacksonKafkaAvroSerializer
which uses TopicNameStrategy to derive
the subject. Then it looks for avro_schemas/{subject}.avsc
file in your classpath. If you are sending messages to topic 'topic', it will try to load the schema from avro_schemas/topic-value.avsc
.
See the source for details.
Unfortunately, you can not use RecordNameStrategy
or TopicRecordNameStrategy
since we do not know the fully qualified name
of the record without finding the schema first.
For more complex scenarios extend AbstractJacksonKafkaAvroSerializer
and implement the
schema resolution by yourself, it's quite easy, just implement the getSchemaFor(String topic, Object value)
method.
When deserializing, we have the schema and the topic name for which we need to find the class to deserialize to.
DefaultJacksonKafkaAvroDeserializer
takes the fully qualified record name and tries to
deserialize to class with the same qualified name. If you need to change the class-name or package name, just override the
getClassName(String topic, Schema schema)
method.
If you need anything more complex, just implement AbstractJacksonKafkaAvroDeserializer
and itsgetClassFor(String topic, Schema schema)
method.
The package is available at Maven Central.
<dependencies>
<dependency>
<groupId>io.github.productboardlabs</groupId>
<artifactId>jackson-kafka-avro-serializer</artifactId>
<version>0.7.1</version>
</dependency>
</dependencies>
implementation("io.github.productboardlabs:jackson-kafka-avro-serializer:0.7.1")
TODO:
- Work without schema registry
- Support for use latest version flag
- Do not use deprecated methods from Kafka Avro serializer
To deploy a new release version to Maven Central, follow the following guide.