Kafka Connect JSON格式

时间:2018-03-20 22:17:25

标签: apache-kafka apache-kafka-connect

我在Kafka有一个标题为newtest的主题,有三条消息:

Hello 
Is anybody out there
Can you hear me

...我有一个连接作业的配置:

{
    "name":"connect-test-9",
    "config":
    {
        "connector.class":"FileStreamSink",
        "file":"connector-test",
        "topics":"newtest",
        "name":"connect-test-9",
        "value.converter":"org.apache.kafka.connect.storage.StringConverter",
        "value.converter.schemas.enable":"false",
        "key.converter":"org.apache.kafka.connect.storage.StringConverter",
        "key.converter.schemas.enable":"false",
        "transforms":"Hoist, AddTimestamp",
        "transforms.Hoist.type":"org.apache.kafka.connect.transforms.HoistField$Value",
        "transforms.Hoist.field":"line",
        "transforms.AddTimestamp.type":"org.apache.kafka.connect.transforms.InsertField$Value",
        "transforms.AddTimestamp.timestamp.field":"Timestamp"
    }
}

我在文件connector-test中收到以下输出:

Struct{line=Hello,Timestamp=Mon Mar 12 14:50:34 PDT 2018}
Struct{line=Is anybody out there,Timestamp=Mon Mar 12 14:50:44 PDT 2018}
Struct{line=Can you hear me,Timestamp=Mon Mar 12 14:50:52 PDT 2018}

我想得到这个:

{"line":"Hello","Timestamp":"Mon Mar 12 14:50:34 PDT 2018"}
{"line":"Is anybody out there","Timestamp":"Mon Mar 12 14:50:44 PDT 2018"}
{"line":"Can you hear me","Timestamp":"Mon Mar 12 14:50:52 PDT 2018"}

我尝试过更改value.converter,没有好处(解析异常)。我还有另一个主题,其中消息已经是Json,并且解析在那里成功,我可以添加一个没有Hoist的Timestamp。但我的输出是非Json格式{key1=value1,key2=value2}

我能以任何方式获得正确的JSON输出吗?

这是我看到的解析异常:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Can': was expecting ('true', 'false', or 'null')

1 个答案:

答案 0 :(得分:1)

要获取JSON输出,您需要使用StringConverter而不是StringConveter enter image description here

您的数据已经在Kafka中,并且您使用了源连接器,可能需要Struct来提取,然后转换为内部{{1}},可以使用Sink连接器和不同的转换器类型