在kafka流中添加带有延迟的'for'循环

时间:2017-12-14 16:02:42

标签: apache-kafka apache-kafka-streams apache-kafka-connect

下面是为了获得输出而编写的代码。

我的输入将采用JSON数组的形式,将JSON数组分隔为Json对象,我编写了以下代码

KStreamBuilder builder = new KStreamBuilder();
KStream<String, String> textlines = builder.stream("INTOPIC");
KStream<String, String> mstream = textlines
                .mapValues(value -> value.replace("[","" ) )
                .mapValues(value -> value.replace("]","" ) )
                .mapValues(value -> value.replaceAll("\\},\\{" ,"\\}\\},\\{\\{"))
                .flatMapValues(value -> Arrays.asList(value.split("\\},\\{")));
mstream.to("OUTTOPIC");
KafkaStreams streams = new KafkaStreams(builder, config);
streams.start();

输入数组中所有JSON对象的输出都会立即出现,但我希望这些JSON对象一个接一个地出现。我是否需要增加延迟,如果是这样我该怎么做?你可以帮我编辑这段代码。

null    {"timestamp":"2017-12-05T13:52:43.560588466+05:30","namespace":"/intel/procfs/meminfo/active_anon","data":4183900160,"unit":"","tags":{"plugin_running_on":"AELAB110"},"version":4,"last_advertised_time":"2017-12-05T13:52:43.560658038+05:30"}
null    {"timestamp":"2017-12-05T13:52:43.560618652+05:30","namespace":"/intel/procfs/meminfo/mem_free","data":49385746432,"unit":"","tags":{"plugin_running_on":"AELAB110"},"version":4,"last_advertised_time":"2017-12-05T13:52:43.560660098+05:30"}
null    {"timestamp":"2017-12-05T13:52:43.560588466+05:30","namespace":"/intel/procfs/meminfo/active_anon","data":4183900160,"unit":"","tags":{"plugin_running_on":"AELAB110"},"version":4,"last_advertised_time":"2017-12-05T13:52:43.560658038+05:30"}
null    {"timestamp":"2017-12-05T13:52:43.560618652+05:30","namespace":"/intel/procfs/meminfo/mem_free","data":49385746432,"unit":"","tags":{"plugin_running_on":"AELAB110"},"version":4,"last_advertised_time":"2017-12-05T13:52:43.560660098+05:30"}

这些json对象应该一个接一个地出现,所以我的代码应该是什么

提前致谢。

0 个答案:

没有答案