Flink窗口之CountWindowAll

Flink窗口之CountWindowAll

代碼

package Windows;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.AllWindowedStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;

public class CountWindowAllOp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> source = env.socketTextStream("t1", 9999);

        SingleOutputStreamOperator<Integer> map = source.map(new MapFunction<String, Integer>() {

            @Override
            public Integer map(String value) throws Exception {
                return Integer.parseInt(value);
            }
        });

        //不分組 將整體當做一個組 窗口大小爲3條數據一個窗口
        AllWindowedStream<Integer, GlobalWindow> windows = map.countWindowAll(3);

        SingleOutputStreamOperator<Integer> sum = windows.sum(0);

        SingleOutputStreamOperator<Integer> max = windows.max(0);

        SingleOutputStreamOperator<Integer> min = windows.min(0);


        sum.print();

        env.execute();
    }
}

輸入數據

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

運行結果

6> 6
7> 15

總結

  • 每3條數據進行一次統計 7 和 8 輸入後沒有達到窗口大小所以不統計
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章