如何在flink中求多个字段的和?

agxfikkp  于 2021-06-25  发布在  Flink
关注(0)|答案(1)|浏览(550)

我想得到多个字段的和。我用这个代码来解释我的痛苦:

// parse the data, group it, window it, and aggregate the counts
 val windowCounts = text
      .flatMap { w => w.split("\\s") }
      .map { w => WordWithCount(w, 1, 2) }
      .keyBy("word")
      .timeWindow(Time.seconds(5), Time.seconds(1))
      .sum("count")

case class WordWithCount(word: String, count: Long, count2: Long)

我想要我的时间窗口中两个字段(count和count2)的总和。我不能像那样加多个和:

val windowCounts = text
        .flatMap { w => w.split("\\s") }
        .map { w => WordWithCount(w, 1, 2) }
        .keyBy("word")
        .timeWindow(Time.seconds(5), Time.seconds(1))
        .sum("count", "count2")

我不知道怎么做。

uqcuzwp8

uqcuzwp81#

datasteamapi不提供对多个字段求和的内置运算符。
有两种选择:
实现自定义 ReduceFunction 这两个领域的总和。
看看flink的表api或sql支持。两者都可以在组窗口上执行多个聚合。

相关问题