【发布时间】: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