为什么kafka-avro-console-producer不遵守该字段的默认值? [英] Why kafka-avro-console-producer doesn't honour the default value for the field?

查看:333
本文介绍了为什么kafka-avro-console-producer不遵守该字段的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管为字段定义了默认值,但kafka-avro-console-producer会完全忽略它:

Although default is defined for a field, kafka-avro-console-producer ignores it completely:

$ kafka-avro-console-producer --broker-list localhost:9092 --topic test-avro \
--property schema.registry.url=http://localhost:8081 --property \
value.schema='{"type":"record","name":"myrecord1","fields": \
[{"name":"f1","type":"string"},{"name": "f2", "type": "int", "default": 0}]}'

{"f1": "value1"}

org.apache.kafka.common.errors.SerializationException: Error 
deserializing json {"f1": "value1"} to Avro of schema 
{"type":"record","name":"myrecord1","fields": 
[{"name":"f1","type":"string"},{"name":"f2","type":"int","default":0}]}
Caused by: org.apache.avro.AvroTypeException: Expected int. Got END_OBJECT
    at org.apache.avro.io.JsonDecoder.error(JsonDecoder.java:698)
    at org.apache.avro.io.JsonDecoder.readInt(JsonDecoder.java:172)
    at org.apache.avro.io.ValidatingDecoder.readInt(ValidatingDecoder.java:83)
    at org.apache.avro.generic.GenericDatumReader.readInt(GenericDatumReader.java:511)
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:182)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
    at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:240)
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:230)
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:174)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144)
    at io.confluent.kafka.formatter.AvroMessageReader.jsonToAvro(AvroMessageReader.java:213)
    at io.confluent.kafka.formatter.AvroMessageReader.readMessage(AvroMessageReader.java:180)
    at kafka.tools.ConsoleProducer$.main(ConsoleProducer.scala:54)
    at kafka.tools.ConsoleProducer.main(ConsoleProducer.scala)

然后如何使用它才能接受默认值?顶级配置设置为"BACKWARD"兼容性级别检查,尽管我认为这与问题无关.该模式为版本2,版本1仅定义了f1字段,但正如我所说,我认为这无关紧要.

How to use it then in order to accept the default? Top level config is set to "BACKWARD" compatibility Level check, although I don't believe that has anything to do with the question. This schema is version 2, and version 1 was defined with only f1 field, but as I said, I don't think that matters.

推荐答案

default:此字段的默认值,在读取缺少此字段的实例时使用

default: A default value for this field, used when reading instances that lack this field

因此,生产者仍然需要提供该字段.

Therefore, the producer still needs to supply that field.

我不确定使用Avro控制台生成器时是否可以完全排除该字段,因为即使您将该字段设置为Giorgos所示的可空值,您仍然需要显式设置它.

I'm not sure it's possible to completely exclude a field when using the Avro console producer, because even if you make the field as nullable as Giorgos shows, you still need to explicitly set it.

这篇关于为什么kafka-avro-console-producer不遵守该字段的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆