为JSON创建kafka流API

时间:2017-11-27 09:15:03

标签: apache-kafka apache-kafka-streams

我正在尝试编写一个kafka流代码,用于将JSON数组转换为JSON元素...因为我是kafka流的新手,任何人都可以帮我编写代码...就像kstream和ktable中应该有什么一样。 。 我的输入流ll采用以下格式

[
 {"timestamp":"2017-10-24T12:44:09.359126933+05:30","data":0,"unit":""},
 {"timestamp":"2017-10-24T12:44:09.359175426+05:30","data":1,"unit":""}
]

[
 {"timestamp":"2017-10-24T12:44:09.359126933+05:30","data":2,"unit":""},
 {"timestamp":"2017-10-24T12:44:09.359175426+05:30","data":3,"unit":""}
]

我的输出必须是

格式
{"timestamp":"2017-10-24T12:44:09.359126933+05:30","data":0,"unit":""}
{"timestamp":"2017-10-24T12:44:09.359175426+05:30","data":1,"unit":""}
{"timestamp":"2017-10-24T12:44:09.359126933+05:30","data":2,"unit":""}
{"timestamp":"2017-10-24T12:44:09.359175426+05:30","data":3,"unit":""}

任何人都可以帮我写代码吗?

2 个答案:

答案 0 :(得分:0)

如果您想使用Kafka Streams,可以使用flatMap()。像

这样的东西
// using new 1.0 API
StreamsBuilder builder = new StreamsBuilder();
builer.stream("topic").flatMap(...).to("output-topic");

查看示例和文档以获取更多详细信息:

答案 1 :(得分:-1)

Python中的

......

from kafka import KafkaConsumer
consumer = KafkaConsumer('topicName')
for message in consumer:
  print(message)

在KafkaConsumer中指定bootstrap_servers参数。

对于Java外观cloudkarafka,非常好:

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList(topic));
while (true) {
    ConsumerRecords<String, String> records = consumer.poll(100);
    for (ConsumerRecord<String, String> record : records)
        System.out.printf("msg = %s\n", record.value());
    }
}