• Flink Window那些事——ProcessWindowFunction/ProcessAllWindowFunction


    全量聚合: 窗口需要维护全部原始数据,窗口触发进行全量聚合。

    ProcessWindowFunction获得一个包含窗口所有元素的可迭代器,以及一个具有时间和状态信息访问权的上下文对象,这使得它比其他窗口函数提供更大的灵活性。这是以性能和资源消耗为代价的,因为元素不能增量地聚合,而是需要在内部缓冲,直到认为窗口可以处理为止。

    WindowFunction的升级版,可以跟ReduceFunction/AggregateFunction/FoldFunction结合使用(推荐用法)

    package com.lynch.stream.window;
    
    import org.apache.flink.api.java.tuple.Tuple;
    import org.apache.flink.api.java.tuple.Tuple3;
    import org.apache.flink.streaming.api.datastream.DataStream;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
    import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
    import org.apache.flink.util.Collector;
    
    /**
     * 测试ProcessWinFunction
     *
     * @author dajiangtai
     * @create 2019-06-11-18:37
     */
    public class TestProcessWinFunctionOnWindow {
    
        public static void main(String[] args) throws Exception{
            //获取执行环境
            StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    
            //读取数据
            DataStream<Tuple3<String,String,Long>> input = env.fromElements(ENGLISH);
    
            //求各班级英语成绩平均分
            DataStream<Double> avgScore = input.keyBy(0)
                    .countWindow(2)
                    .process(new MyProcessWindowFunction());
            avgScore.print();
            env.execute("TestProcessWinFunctionOnWindow");
    
        }
    
    
        public static class MyProcessWindowFunction extends ProcessWindowFunction<Tuple3<String,String,Long>,Double, Tuple, GlobalWindow>{
            
            //iterable 输入流中的元素类型集合
            @Override
            public void process(Tuple tuple, Context context, Iterable<Tuple3<String, String, Long>> iterable, Collector<Double> out) throws Exception {
                long sum = 0;
                long count = 0;
                for (Tuple3<String,String,Long> in :iterable){
                    sum+=in.f2;
                    count++;
                }
                out.collect((double)(sum/count));
            }
        }
    
        public static final Tuple3[] ENGLISH = new Tuple3[]{
                Tuple3.of("class1","张三",100L),
                Tuple3.of("class1","李四",78L),
                Tuple3.of("class1","王五",99L),
                Tuple3.of("class2","赵六",81L),
                Tuple3.of("class2","小七",59L),
                Tuple3.of("class2","小八",97L),
        };
    }
  • 相关阅读:
    二分查找小结
    FZU みねちゃんの修罗场(从一堆出现三次的数中找出出现两次的数)
    《C陷阱与缺陷》杂记
    HDU 1846 Brave Game(巴什博弈)
    【转载】并查集详解
    第七次作业——学末总结
    STL之vector
    fzuoop期中练习
    MFC 文件对话框
    第六次作业——利用MFC实现计算器图形界面以及简单四则运算表达式批处理
  • 原文地址:https://www.cnblogs.com/linjiqin/p/12591729.html
Copyright © 2020-2023  润新知