You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用kafka-json-schema-console-consumer消费JSON消息时无法打印key怎么办

问题解决方法

你当前的命令缺少两个核心配置项:开启key打印开关、指定key的JSON Schema反序列化器,按以下步骤修改即可:

  • 新增--property print.key=true:该配置默认值为false,是控制控制台是否打印消息key的核心开关,开启后才会输出key内容
  • 新增--property key.deserializer=io.confluent.kafka.serializers.json.KafkaJsonSchemaDeserializer:指定key使用JSON Schema专用反序列化器,否则默认的key反序列化器不会调用你配置的key.schema做解析,无法正常输出结构化的key内容
  • 可选新增--property print.separator=" | ":自定义key和value的输出分隔符,方便区分两部分内容,可根据自己的阅读习惯修改分隔符内容

修改后的完整命令如下:

kafka-json-schema-console-consumer \
--bootstrap-server http://kafka:9092 \
--topic source-1 \
--property value.schema='
{
  "definitions" : {
    "record:myrecord" : {
      "type" : "object",
      "required" : [ "timestamp", "data" ],
      "properties" : {
        "timestamp" : {"type" : "string"},
        "data" : {"type" : "string"}
      }
    }
  },
  "$ref" : "#/definitions/record:myrecord"
}' \
--property key.schema='
{
  "definitions" : {
    "record:mykey" : {
      "type" : "object",
      "required" : [ "id" ],
      "additionalProperties" : false,
      "properties" : {
        "id" : {"type" : "integer"}
      }
    }
  },
  "$ref" : "#/definitions/record:mykey"
}' \
--property print.key=true \
--property key.deserializer=io.confluent.kafka.serializers.json.KafkaJsonSchemaDeserializer \
--property print.separator=" | "

执行后输出示例:

{"id":123} | {"timestamp":"10/05/2021 4:41:53 PM","data":"hello world"}

内容的提问来源于stack exchange,提问作者Eric Broda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 04:57:04