• 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

  • 相关阅读:
    51nod-1420-贪心
    51nod-1455-dp/缩小范围
    51nod-1574-排列转换
    简单的鼠标滚轮事件
    数组去重
    模仿jq里的选择器和color样式
    在页面里写个动态本地时间
    使用css中的flex布局弹性手风琴效果
    bootstrap中如何多次使用一个摸态框
    使用css让文字两端对齐
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/15171020.html
Copyright © 2020-2023  润新知