flink 窗口函數之增量聚合與全量聚合

flink 窗口函數後會跟進聚集函數,聚合函數又分爲增量聚合與全量聚合

增量聚合:本時間窗口範圍內的數據聚合

全量聚合:本次時間窗口聚合結果+ 歷史聚合結果 聚合後生成新的歷史聚合結果

public class InterctiveReduceFunction implements ReduceFunction<Tuple2<String, Long>> {
    private static final Logger logger = Logger.getLogger(InterctiveReduceFunction.class);

    @Override
    public Tuple2<String, Long> reduce(Tuple2<String, Long> value1, Tuple2<String, Long> value2) throws Exception {
        value1.setFields(value1.f0, value1.f1 + value2.f1);
        return value1;
    }

}


public class InterctiveWindowFunction implements WindowFunction<Tuple2<String, Long>, Tuple3<Integer, String, Long>, String, TimeWindow> {
    private static final Logger logger = Logger.getLogger(InterctiveWindowFunction.class);

    public void apply(String key, TimeWindow timeWindow, Iterable<Tuple2<String, Long>> iterable, Collector<Tuple3<Integer, String, Long>> collector) throws Exception {

        long count = 0;

        for (Tuple2<String, Long> value : iterable) {
            count += value.f1;
        }

        collector.collect(Tuple3.of(DealMidInteractive.BhvType, key, count));
    }

}

 

第一個函數是 增量聚合,第二個是全量聚合

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章