简体   繁体   English

具有本地模式文件的 Kafka Avro 反序列化器

[英]Kafka Avro deserializer with local schema file

Background: I used SpringKafka to implement Avro based Consumer and Producer.背景:我使用SpringKafka实现了基于Avro的Consumer和Producer。 Other important components: Kafka-Broker, Zookeeper and Schema-registry runs in a docker container.其他重要组件:Kafka-Broker、Zookeeper 和 Schema-registry 在 docker 容器中运行。 And this works perfectly fine for me.这对我来说非常好。

What I Want: I want to have a Kafka Avro Deserializer(in Consumer) which should be independent of schema-registry.我想要什么:我想要一个 Kafka Avro Deserializer(在 Consumer 中),它应该独立于 schema-registry。 In my case, I have a avroSchema file which would not change.就我而言,我有一个不会更改的 avroSchema 文件。 So I want to get rid of this additional step of using schema-registry on Consumer side and Rather go for a local schema file所以我想摆脱在消费者端使用 schema-registry 的额外步骤,而 go 用于本地模式文件

If the Producer serializer uses the Schema Registry, then the Consumer should as well.如果生产者序列化程序使用模式注册表,那么消费者也应该使用。 Avro requires you to have a reader and writer schema. Avro要求你有一个读者作家模式。

If the consumer, for whatever reason cannot access the Registry over the.network, you would need to use ByteArrayDeserializer , then you would take the byte-slice after position 5 ( 0x0 + 4 byte schema integer ID) of the byte[] from the part of the record you want to parse.如果消费者出于某种原因无法通过0x0访问注册表,则需要byte[] ByteArrayDeserializer ,然后您将从您要解析的记录的一部分。

Then, from Avro API, you can use GenericDatumReader along with your local Schema reference to get a GenericRecord instance, but this would assume your reader and writer schema are the exact same (but this shouldn't be true, as the producer could change the schema at any time).然后,从 Avro API,您可以使用GenericDatumReader以及您的本地Schema引用来获取GenericRecord实例,但这将假设您的读取器和写入器模式完全相同(但这不应该是真的,因为生产者可以更改任何时候的模式)。


Or you can create a SpecificRecord from the schema you have, and configure the KafkaAvroDeserializer to use that.或者您可以从您拥有的模式创建一个 SpecificRecord,并配置KafkaAvroDeserializer以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM