java之光 2022-08-10 21:17 采纳率: 0%
浏览 50

flink使用sum是怎么计算的

代码


object ReduceAgg {
  def main(args: Array[String]) {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment
    val tup: DataStream[(Int, Int)] = env.fromElements((1,2),(2,5),(2,3),(10,3),(2,8),(3,6))

    val gpT: KeyedStream[(Int, Int), Tuple] = tup.keyBy(1)
//    gpT.print()
    //gpT.print()
//    val reduce: DataStream[(Int, Int)] = gpT.reduce((before, after) => {
//      (before._1, before._2 + after._2)
//    })
    val sum: DataStream[(Int, Int)] = gpT.sum(1)
    sum.print()
    env.execute()
//    gpT.reduce((left,right)=>{
//      (left._1,right._1+right._2)
//    })
  }
}

结果


4> (1,2)
4> (2,5)
4> (2,3)
4> (2,6)
1> (2,8)
1> (3,6)

(2,3)与(2,5)不会做滚动求和吗,最不能理解的是(2,6)是怎么来的

  • 写回答

1条回答 默认 最新

  • 黄英镑 2022-08-11 16:36
    关注

    应该要keyBy(0)

    评论

报告相同问题?

问题事件

  • 创建了问题 8月10日