If you want to use Avro in your Kafka project, but aren't using Confluent Schema Registry, you can use this Avro Serdes instead. Note that the record schema will be serialized with each message.
dependencies {
implementation 'com.mitchseymour:kafka-registryless-avro-serdes:0.1.0'
}
Once you've generated your classes from an Avro schema file, for example, with the gradle-avro-plugin, you can use the AvroSerdes#get
method to generate an Avro Serdes for a generated class. For example, if you generated a class named Tweet
from the following definition:
{
"namespace": "com.mitchseymour.model",
"name": "Tweet",
"type": "record",
"fields": [
{
"name": "id",
"type": "long"
},
{
"name": "text",
"type": "string"
}
]
}
You could then create an Avro Serde for that class using this code:
Serde<Tweet> serde = AvroSerdes.get(Tweet.class);
The resulting Serde can be used anywhere you would normally use one of Kafka's built-in Serdes. For example, in a Kafka Streams app, you could do this:
stream.to("tweets", Produced.with(Serdes.String(), AvroSerdes.get(Tweet.class)));