com.github.thake.kafka.avro4k.serializer.KafkaAvro4kSerializer.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of avro4k-kafka-serializer Show documentation
Show all versions of avro4k-kafka-serializer Show documentation
Provides Kafka SerDes and Serializer / Deserializer implementations for avro4k
package com.github.thake.kafka.avro4k.serializer
import io.confluent.kafka.schemaregistry.avro.AvroSchema
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClient
import org.apache.kafka.common.serialization.Serializer
class KafkaAvro4kSerializer(
client : SchemaRegistryClient? = null,
props : Map? = null
) : AbstractKafkaAvro4kSerializer(), Serializer {
private var isKey = false
init {
props?.let { configure(this.serializerConfig(it)) }
//Set the registry client explicitly after configuration has been applied to override client from configuration
if (client != null) this.schemaRegistry = client
}
override fun configure(configs: Map, isKey: Boolean) {
this.isKey = isKey
this.configure(KafkaAvro4kSerializerConfig(configs))
}
override fun serialize(topic: String, record: Any?): ByteArray? {
return this.serializeImpl(
this.getSubjectName(
topic,
isKey,
record,
AvroSchema(avroSchemaUtils.getSchema(record))
), record
)
}
override fun close() {}
}