【问题标题】:How can I sum multiple fields in Flink?如何在 Flink 中对多个字段求和?
【发布时间】:2017-10-18 12:38:33
【问题描述】:

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

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

我不知道该怎么做。

【问题讨论】:

  • 那么您如何看待使用 map 函数创建具有任意键和两个字段值之和的元组流,然后使用聚合?
  • 使用@FabianHueske 的解决方案它工作正常,我使用reduceFunction 和自定义Sum。 ``` 流 .map(x => transfom(x)) .keyBy("field") .timeWindow(Time.milliseconds(10000), Time.milliseconds(1000)) .reduce((x, y) => Custom .sum(x, y)) ```

标签: apache-flink flink-streaming


【解决方案1】:

DataSteam API 不提供内置运算符来汇总多个字段。

有两种选择:

  1. 实现一个自定义 ReduceFunction 对两个字段求和。
  2. 看看 Flink 的Table APISQL support。两者都可以在一个组窗口上执行多个聚合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2018-06-14
    相关资源
    最近更新 更多