Apache flink 如何对Flink中的多个字段求和?

Apache flink 如何对Flink中的多个字段求和?,apache-flink,flink-streaming,Apache Flink,Flink Streaming,我想得到多个字段的总和。我用这段代码来解释我的痛苦: // 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.

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

 // 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")

我不知道该怎么做。

DataSteam API不提供用于对多个字段求和的内置运算符

有两种选择:

  • 实现对两个字段求和的自定义
  • 看看弗林克的手术室。两者都可以在组窗口上执行多个聚合

  • 使用map函数创建一个元组流,在开始时使用任意键和两个字段值的和,然后使用聚合,你觉得怎么样?使用@FabianHueske的解决方案,我使用reduceFunction和自定义和stream.map(x=>transfom(x)).keyBy(“字段”).timeWindow(Time.millizes(10000),Time.millizes(1000)).reduce((x,y)=>Custom.sum(x,y))```