2017-10-17 87 views
0

我想获得多个字段的总和。我用这个代码来解释我的痛苦:如何在Flink中总结多个字段?

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

我想在我的时间窗口中的两个字段(计数和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") 

我不知道该怎么做。

+0

你怎么看待使用map函数来创建一个具有任意键的元组流,并在开始时使用两个字段值的总和,然后使用聚合? –

+1

使用@FabianHueske的解决方案它工作正常,我使用reduceFunction与自定义总和。 ''' 流 .MAP(X => transfom(X)) .keyBy( “场”) .timeWindow(Time.milliseconds(10000),Time.milliseconds(1000)) 。降低((X ,y)=> Custom.sum(x,y)) ''' – FlinkNoob

回答

1

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

有两种选择:

  1. 实现自定义ReduceFunction,总结这两个字段。
  2. 查看Flink的Table APISQL support。两者都可以在组窗口上执行多个聚合。
+0

谢谢你是完美的 – FlinkNoob

相关问题