Search code examples
javaapache-kafkaavroapache-kafka-streamsconfluent-schema-registry

Deserialization PRIMITIVE AVRO KEY with kafka lib


I'm currently incapable of deserialize an avro PRIMITIVE key in a KSTREAM APP

the key in encoded with an avro schema ( registered in the schema registry ) ,

when i use the kafka-avro-console-consumer, I can see that the key is correctly deserialize

But impossible to make it work in a KSTREAM app

the avro schema of the key is a PRIMITIVE:

{"type":"string"}

I already followed the documentation of confluent

final Serde<V> valueSpecificAvroSerde = new SpecificAvroSerde<>();
final Map<String, String> serdeConfig = Collections.singletonMap(SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
valueSpecificAvroSerde.configure(serdeConfig, false);

final Serdes.StringSerde keySpecificAvroSerde = new Serdes.StringSerde();
keySpecificAvroSerde.configure(serdeConfig, true);

Consumed<String, totoAvro> inputConf = Consumed.with(keySpecificAvroSerde, valueSpecificAvroSerde);

final KStream<String, totoAvro> mystream = builder.stream("name topic", inputConf);

mystream.peek((key, value) -> logger.info("topic KEY :" + key))

it's working well for the value, but the key is going to be a string containing the bytes from the schema registry and not only the "reel" key

https://docs.confluent.io/current/schema-registry/serializer-formatter.html#wire-format

So the string key is /§/./11016015201 , but I would like the reel value : 1016015201

if I print the bytes inside the String it's [ 0x00 0x00 0x00 0x02 0x31 0x14 0x31 0x30 0x31 0x36 0x30 0x31 0x35 0x32 0x30 0x31 ]


Solution

  • Update

    it's now working : https://stackoverflow.com/a/51957801/6227500

    Original answer

    The feature is not available currently in the schema registry project.

    But by implementing a custom SERDE you can manage the case ,

    Thiyaga Rajan proposed a working implementation

    Serde class for AVRO primitive type