zoukankan      html  css  js  c++  java
  • Flink window Function

    package window

    import org.apache.flink.api.common.functions.{AggregateFunction, FoldFunction}
    import org.apache.flink.api.scala._
    import org.apache.flink.streaming.api.functions.source.SourceFunction
    import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment, WindowedStream}
    import org.apache.flink.streaming.api.windowing.time.Time
    import org.apache.flink.streaming.api.windowing.windows.TimeWindow

    /**
    * @author: create by maoxiangyi
    * @version: v1.0
    * @description: window
    * @date:2019 /6/4
    */
    object FoldWordCount {
    def main(args: Array[String]): Unit = {
    //设置环境
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.createLocalEnvironment()
    //设置数据源
    val window: WindowedStream[(String, Int), String, TimeWindow] = env.addSource(new SourceFunction[String] {
    override def run(ctx: SourceFunction.SourceContext[String]): Unit = {
    while (true) {
    ctx.collect("hello hadoop hello storm hello spark")
    Thread.sleep(1000)
    }
    }

    override def cancel(): Unit = {}
    })
    //计算逻辑
    .flatMap(_.split(" "))
    .map((_, 1))
    .keyBy(_._1)
    .timeWindow(Time.seconds(10), Time.seconds(10))


    window.fold(("", 0), new FoldFunction[(String, Int), (String, Int)] {
    override def fold(accumulator: (String, Int), value: (String, Int)): (String, Int) = {
    (value._1, value._2 + accumulator._2)
    }
    }).print()

    env.execute("word count")
    }
    }
  • 相关阅读:
    TX2 刷机教程
    ROS2 树莓派SBC镜像安装
    OP3 默认ID图
    OP3 镜像恢复
    ROS2 BringUp
    学习笔记3:Linux面试题
    学习笔记2:Linux简单指令
    学习笔记1:Git简单指令
    编程小白入门分享五:Vue的自定义组件
    编程小白入门分享四:Vue的安装及使用快速入门
  • 原文地址:https://www.cnblogs.com/maoxiangyi/p/10977910.html
Copyright © 2011-2022 走看看