[英]Consuming a key/value from KAFKA using kafka-json-schema-console-consumer only returns value but not key
[英]Using kafka-json-schema-console-producer to produce message with a key schema and a value schema
我正在尝试使用 kafka-json-schema-console-producer 发布包含键(带模式)和值(带模式)的消息。 不幸的是,我无法找到一个可以满足我要求的示例。
我可以按照文档发送简单的消息:
kafka-json-schema-console-producer \
--broker-list localhost:9092 \
--topic some-topic \
--property value.schema='
{
"definitions" : {
"record:myrecord" : {
"type" : "object",
"required" : [ "name", "calories" ],
"additionalProperties" : false,
"properties" : {
"name" : {"type" : "string"},
"calories" : {"type" : "number"},
"colour" : {"type" : "string"}
}
}
},
"$ref" : "#/definitions/record:myrecord"
}' < snacks.txt
两个问题:
是的,添加--property key.schema
。 对于磁盘上的key.schema.file
文件,还有 key.schema.file 选项或注册表中已有 ID 的key.schema.id
选项。
摘录(自 v6.2.0 起)
* bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic t1 \
* --line-reader io.confluent.kafka.formatter.JsonSchemaMessageReader \
* --property schema.registry.url=http://localhost:8081 \
* --property parse.key=true \
* --property key.schema='{"type":"string"}' \
* --property value.schema='{"type":"object","properties":{"f1":{"type":"string"}}}'
* "type":"string"}]}'
kafka-json-schema-console-producer
是
kafka-console-producer --line-reader io.confluent.kafka.formatter.JsonSchemaMessageReader
多亏@OneCricketeer 的帮助,我才能够完成这项工作。 下面是一个完整的示例,它将发送具有 JSON 架构的键和值的消息。 (顺便说一句:我展示了一个使用 Docker 的解决方案,但将其修改为不使用 Docker 应该很简单)
请注意,有几件事让我感到困惑:
docker exec -it schema-registry \
/usr/bin/kafka-json-schema-console-producer \
--broker-list http://kafka:9092 \
--topic source-1 \
--property key.separator="|" \
--property value.schema='
{
"definitions" : {
"record:myrecord" : {
"type" : "object",
"required" : [ "name", "calories" ],
"properties" : {
"name" : {"type" : "string"},
"calories" : {"type" : "number"},
"colour" : {"type" : "string"}
}
}
},
"$ref" : "#/definitions/record:myrecord"
}' \
--property parse.key=true \
--property key.schema='
{
"definitions" : {
"record:mykey" : {
"type" : "object",
"required" : [ "id" ],
"additionalProperties" : false,
"properties" : {
"id" : {"type" : "integer"}
}
}
},
"$ref" : "#/definitions/record:mykey"
和数据:
{"id":1} | {"timestamp":"foo", "data":"bar"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.