Flink之窗口函数之reducefunction

Flink之窗口函数之reducefunction

代码

package windows

import org.apache.flink.api.java.tuple.Tuple
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow


/**窗口函数之reduce*/
object WindowsFuncReduce {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val line: DataStream[String] = env.socketTextStream("t1",9999)

    val flatMaped: DataStream[String] = line.flatMap(_.split(","))

    //进行数字累加
    val numberAndOne: DataStream[(Int, Int)] = flatMaped.map(x=>{(1,x.toInt)})

    // key 都是1 所有的元素都进入同一个task
    val keyBy: KeyedStream[(Int, Int), Tuple] = numberAndOne.keyBy(0)

    //开启一个滚动窗口 时间间隔是5秒钟
    val windows: WindowedStream[(Int, Int), Tuple, TimeWindow] = keyBy.timeWindow(Time.seconds(5))

    val result: DataStream[(Int, Int)] = windows.reduce((v1, v2) =>{
      println(v1+"--------->"+v2)
      (v1._1,v1._2+v2._2)
    })
    result.print()

    env.execute("WindowsFuncReduce")

  }
}

输入数据

[root@t1 ~]# nc -lk 9999
1,1    
2,3
2,4
2,5

运行结果

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

总结

  • 使用窗口函数 数据进行两两相加 不再是按照窗口进行统计
发布了33 篇原创文章 · 获赞 12 · 访问量 3333

猜你喜欢

转载自blog.csdn.net/IT_BULL/article/details/104201157