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, Integer, Tuple, Window>() {
    public void apply (Tuple tuple,
            Window window,
            Iterable> values,
            Collector 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, Integer, Window>() {
    public void apply (Window window,
            Iterable> values,
            Collector 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>() {
    public Tuple2 reduce(Tuple2 value1, Tuple2 value2) throws Exception {
        return new Tuple2(value1.f0, value1.f1 + value2.f1);
    }
});

相关