【问题标题】:how to get result of Kafka streams aggregate task and send the data to another service?如何获取 Kafka 流聚合任务的结果并将数据发送到另一个服务?
【发布时间】:2019-01-28 06:45:04
【问题描述】:

我使用Kafka流来处理实时数据,我需要对窗口时间的数据进行一些聚合操作。

我有两个关于聚合操作的问题。

  1. 如何获取汇总数据?我需要将它发送到第三个服务。
  2. 聚合操作后,我无法向第三个服务发送消息,代码无法运行。

这是我的代码:

stream = builder.stream("topic");
windowedKStream = stream.map(XXXXX).groupByKey().windowedBy("5mins");
ktable = windowedKStream.aggregate(()->"", new Aggregator(K,V,result));

// my data is stored in 'result' variable, but I can't get it at the end of the 5 mins window. 
// I need to send the 'result' to a 3rd service. But I don't know where to temporarily store it and then how to get it.

// below is the code the call a 3rd service, but the code can't be executed(reachable).
// I think it should be executed every 5 mins when thewindows is over. But it isn't.

result = httpclient.execute('result');

【问题讨论】:

  • “我的数据存储在‘结果’变量中”是什么意思?另请注意,Kafka Streams 窗口聚合为窗口的每个 update 发出一个结果记录——没有最终结果的概念。 Cf:confluent.io/blog/watermarks-tables-event-time-dataflow-model——还有几个关于“最终结果”的问题,您可能想看看。
  • @MatthiasJ.Sax 对不起,'result' 是 Aggregator() 函数的第三个参数。
  • 这不是 Kafka Streams 的工作方式...希望我的回答对您有所帮助。

标签: apache-kafka apache-kafka-streams


【解决方案1】:

我想可能想做这样的事情:

ktable.toStream().foreach((k,v) -> httpclient.execute(v));

每次更新KTable(禁用缓存),都会将更新记录发送到下游,并以v为当前聚合结果执行foreach

【讨论】:

  • 非常感谢,它对我们有用!但是正如您所提到的,每次更新 KTable 时都会执行 httpclient.execute,这不是我想要的。我想聚合加窗数据,例如,我想得到5分钟窗口的所有数字的总和,例如[4,5,6],我只想要最终的结果(sum) 15,但是现在它将输出 4、9 和 15,每次有新数据到来时计算总和。
  • 我可以使用 Kafka 流来做到这一点吗?
  • 目前没有“最终”结果。阅读博文:confluent.io/blog/watermarks-tables-event-time-dataflow-model -- 还可以比较:stackoverflow.com/questions/38935904/… -- 即将发布的 2.1 版本将包含您想要的内容。比较:cwiki.apache.org/confluence/display/KAFKA/…
  • 我最终使用KStream.transform()过滤非最终结果,再次使用transform()将restful结果转换为KStream,然后将结果保存到Kafka topic。它有效,但也许这不是我认为的最佳做法。
  • 目前,这是最佳实践。在即将发布的 2.1 中,我们计划添加 Suppress 配置,因此您不必编写自定义代码。
猜你喜欢
  • 2020-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
相关资源
最近更新 更多