首页 > 解决方案 > 为什么 kafka-avro-console-consumer 不显示缺失字段的默认值?

问题描述

好的,考虑到我已经问过一个相关问题,现在我有点沮丧:

为什么 kafka-avro-console-producer 不遵守该字段的默认值?

如果生产者使用旧模式而没有新字段 f2,则使用新模式的消费者应该接受该字段的默认值,但在 kafka-avro-console-consumer 的情况下并不明显:

$ 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"}]}'

{"f1": "value3"}

$ kafka-avro-console-consumer --bootstrap-server 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":"value3"}

我的意思是,好的,它确实不会因为缺少 f2 字段而引发异常并终止,这没关系,它显示了它收到的实际消息,但它不应该显示而不是显示该消息的表示形式它使用的架构?

这是模式的两个版本:

curl http://localhost:8081/subjects/test-avro-value/versions/1
{"subject":"test-avro-value","version":1,"id":5,"schema":"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}"}
curl http://localhost:8081/subjects/test-avro-value/versions/2
{"subject":"test-avro-value","version":2,"id":6,"schema":"{\"type\":\"record\",\"name\":\"myrecord1\",\"fields\":[{\"name\":\"f1\",\"type\":\"string\"},{\"name\":\"f2\",\"type\":\"int\",\"default\":0}]}"}

那么,这是否意味着无法使用 kafka-avro-console-consumer 测试这种情况?

在此处输入图像描述

标签: apache-kafkaavroconfluent-schema-registry

解决方案


您的生产者已注册value.schema您在注册表中指定的内容。

无法使用 Confluent 的消费者应用程序为消费者明确定义模式,因为它是根据嵌入在有效负载的字节数组中的模式 ID 直接从注册表中检索的。


推荐阅读