zoukankan      html  css  js  c++  java
  • FLINK基础(93): DS算子与窗口(7)单流算子(6) 窗口算子 Window/WindowAll/Window Apply/WindowReduce

    Window 

    KeyedStream → WindowedStream 

    Windows can be defined on already partitioned KeyedStreams. Windows group the data in each key according to some characteristic (e.g., the data that arrived within the last 5 seconds). See windows for a complete description of windows.

    java

    dataStream
      .keyBy(value -> value.f0)
      .window(TumblingEventTimeWindows.of(Time.seconds(5))); 

    WindowAll #

    DataStreamStream → AllWindowedStream #

    Windows can be defined on regular DataStreams. Windows group all the stream events according to some characteristic (e.g., the data that arrived within the last 5 seconds). See windows for a complete description of windows.

    This is in many cases a non-parallel transformation. All records will be gathered in one task for the windowAll operator.

    java

    dataStream
      .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)));

    Window Apply #

    WindowedStream → DataStream #

    AllWindowedStream → DataStream #

    Applies a general function to the window as a whole. Below is a function that manually sums the elements of a window.

    If you are using a windowAll transformation, you need to use an AllWindowFunction instead.
    windowedStream.apply(new WindowFunction<Tuple2<String,Integer>, Integer, Tuple, Window>() {
        public void apply (Tuple tuple,
                Window window,
                Iterable<Tuple2<String, Integer>> values,
                Collector<Integer> out) throws Exception {
            int sum = 0;
            for (value t: values) {
                sum += t.f1;
            }
            out.collect (new Integer(sum));
        }
    });
    
    // applying an AllWindowFunction on non-keyed window stream
    allWindowedStream.apply (new AllWindowFunction<Tuple2<String,Integer>, Integer, Window>() {
        public void apply (Window window,
                Iterable<Tuple2<String, Integer>> values,
                Collector<Integer> out) throws Exception {
            int sum = 0;
            for (value t: values) {
                sum += t.f1;
            }
            out.collect (new Integer(sum));
        }
    });

    WindowReduce #

    WindowedStream → DataStream #

    Applies a functional reduce function to the window and returns the reduced value.

    windowedStream.reduce (new ReduceFunction<Tuple2<String,Integer>>() {
        public Tuple2<String, Integer> reduce(Tuple2<String, Integer> value1, Tuple2<String, Integer> value2) throws Exception {
            return new Tuple2<String,Integer>(value1.f0, value1.f1 + value2.f1);
        }
    });

    本文来自博客园,作者:秋华,转载请注明原文链接:https://www.cnblogs.com/qiu-hua/p/15171020.html

  • 相关阅读:
    Mapreduce 工作机制图,MapReduce组合式,迭代式,链式
    win7安装 git软件,如何使用git上传本地代码
    新技术架起 Oracle、Hadoop、NoSQL数据存储之间的桥梁
    Commons-logging + Log4j 使用方法、常见问题
    数据挖掘_面试题一
    未来10年是大数据价值变现的阶段
    数据挖掘十大经典算法
    Java环境变量详细设置
    Hadoop中NameNode、DataNode和Client三者之间的通信方式是什么?怎样进行合作?
    在线图片无损压缩
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15171020.html
Copyright © 2011-2022 走看看