2017-10-17 88 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

相關問題