gpt4 book ai didi

java - Avro union - 结果 json 中的类型信息

转载 作者:太空宇宙 更新时间:2023-11-04 10:02:31 25 4
gpt4 key购买 nike

我有一个 Java 应用程序的一部分,它将 AVRO 编码对象写入 Kafka 主题。我使用 org.apache.kafka.clients. Producer.Producer 将消息发送到主题中。 Producer 配置为使用 io.confluence.kafka.serializers.KafkaAvroSerializer。

AVRO 架构包含以下字段:

{
"name": "field1",
"type": [
"null",
"string"
],
"default": null
}

这基本上意味着它是一个可以为空的字符串字段。

将对象发送到主题后,我使用以下终端命令检查其内容:

./kafka-avro-console-consumer --bootstrap-server broker-host:9091 --topic test-topic-1
--property schema.registry.url=http://schema-host:8081 --from-beginning

结果我希望看到一个具有以下属性的 JSON:

{"field1": "something"}

然而,实际上我看到的是这样的:

{"field1": {"string": "something"}}

除此之外,如果我在实际将其发送到主题之前简单地在扩展 SpecificRecordBase 的对象上使用 toString() 方法,我会看到一个符合我预期的 JSON 格式。

有没有办法真正获得主题中的第一个选项,而无需将此额外的类型信息作为 JSON 的一部分?

最佳答案

Is there a way to actually get the first option in the topic, without this extra type information being a part of the JSON?

没有用于avro-console-consumer。它使用的是GenericRecord,而不是SpecificRecord

由于该字段是联合类型,Avro Record JSON 编码输出必须包含该值字段的类型

关于java - Avro union - 结果 json 中的类型信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53324806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com