代码
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)是怎么来的
应该要keyBy(0)