【问题标题】:Why kafka-avro-console-consumer doesn't display the default value for the missing field?为什么 kafka-avro-console-consumer 不显示缺失字段的默认值?
【发布时间】:2019-08-25 06:52:59
【问题描述】:

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

Why kafka-avro-console-producer doesn't honour the default value for the field?

如果生产者使用旧架构而没有新字段 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 测试呢?

【问题讨论】:

  • kafka-avro-console-consumer 忽略 --property value.schema 选项中指定的架构。它使用消息中指定的模式。事实上,kafka-avro-console-consumer examples 都没有使用该选项。
  • 请告诉我们curl http://localhost:8081/subjects/test-avro-value/versions/1

标签: apache-kafka avro confluent-schema-registry


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2023-03-09
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2019-12-31
    • 2021-12-18
    相关资源
    最近更新 更多