You need to enable JavaScript to run this app. 如何解决InfluxDB Sink连接器出现的JsonParseException异常
优惠活动
大模型
产品
解决方案
定价
更多

如何解决InfluxDB Sink连接器出现的JsonParseException异常

问题根因

从报错的JSON片段可以看到,你写入Kafka的消息中tags字段格式非法:你将JSON数组直接用双引号包裹作为字符串写入,数组内部的双引号没有做转义处理,导致JsonConverter解析整条JSON时直接抛出格式错误。这个错误和InfluxDB Sink本身的标签处理逻辑无关,是上游消息的JSON格式不合法导致的。
非法的tags字段示例:

"tags":"[{"tagId":0,"tagValueId":0,"tag":"미지정","value":"미지정"},...]"

JSON解析到第二个双引号就认为tags字段值已经结束,后续的[不符合JSON语法,直接抛出解析异常。

解决方案

根据你是否能调整上游生产逻辑,分为两种处理方案:

方案1:可调整上游Kafka消息生产逻辑(优先推荐)

直接修正写入Kafka的JSON格式,不要把tags数组转成字符串,直接作为JSON数组字段写入即可,修正后的tags字段格式如下:

"tags": [{"tagId":0,"tagValueId":0,"tag":"미지정","value":"미지정"},{"tagId":7,"tagValueId":2103044,"tag":"물리서버","value":"tcore-master-01"}]

修正后原连接器配置无需改动,即可正常解析消息并写入InfluxDB。

方案2:无法调整上游生产逻辑,仅能在连接器侧适配

需要新增Kafka Connect单消息转换(SMT)能力,处理转义的tags字符串,同时根据你的使用需求配置InfluxDB的标签映射规则,修改后的完整连接器配置如下:

curl -i -X PUT -H "Content-Type:application/json" \
    http://localhost:8089/connectors/influxdb-sink-test-20210615/config \
    -d '{
        "connector.class"               : "io.confluent.influxdb.InfluxDBSinkConnector",
        "value.converter"               : "org.apache.kafka.connect.json.JsonConverter",
        "value.converter.schemas.enable": "false",
        "key.converter"                 : "org.apache.kafka.connect.storage.StringConverter",
        "topics"                        : "test3",
        "influxdb.url"                  : "http://localhost:8086",
        "influxdb.db"                   : "exemDB",
        "measurement.name.format"       : "${topic}",
        "transforms"                          : "parseTags",
        "transforms.parseTags.type"           : "io.confluent.connect.transforms.FromJson$Value",
        "transforms.parseTags.field"          : "tags",
        "transforms.parseTags.schema"         : "[{\"type\":\"struct\",\"fields\":[{\"type\":\"int32\",\"optional\":false,\"field\":\"tagId\"},{\"type\":\"int32\",\"optional\":false,\"field\":\"tagValueId\"},{\"type\":\"string\",\"optional\":false,\"field\":\"tag\"},{\"type\":\"string\",\"optional\":false,\"field\":\"value\"}]}]",
        "influxdb.tag.mapping"                : "tag:value",
        "errors.tolerance"                    : "all",
        "errors.log.enable"                   : true
    }'

如果你不需要将tags数组内的内容作为InfluxDB的索引标签,仅需要将tags作为普通字段存储,也可以直接把value.converter替换为org.apache.kafka.connect.storage.StringConverter,将整条消息作为字符串处理,不需要额外的SMT配置,缺点是后续InfluxDB侧的查询灵活性会大幅降低。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 02:45:08