Flink处理函数(3)—— 窗口处理函数

窗口处理函数包括:ProcessWindowFunction 和 ProcessAllWindowFunction

基础用法
java 复制代码
stream.keyBy( t -> t.f0 )
 .window( TumblingEventTimeWindows.of(Time.seconds(10)) )
 .process(new MyProcessWindowFunction())

这里的MyProcessWindowFunction就是ProcessWindowFunction的一个实现类;

ProcessWindowFunction是一个典型的全窗口函数,把数据全部收集保存在窗口内,等到触发窗口计算时再统一处理

源码解析
java 复制代码
public abstract class ProcessWindowFunction<IN, OUT, KEY, W extends Window>
        extends AbstractRichFunction {

    private static final long serialVersionUID = 1L;

    /**
     * Evaluates the window and outputs none or several elements.
     *
     * @param key The key for which this window is evaluated.
     * @param context The context in which the window is being evaluated.
     * @param elements The elements in the window being evaluated.
     * @param out A collector for emitting elements.
     * @throws Exception The function may throw exceptions to fail the program and trigger recovery.
     */
    public abstract void process(
            KEY key, Context context, Iterable<IN> elements, Collector<OUT> out) throws Exception;

    /**
     * Deletes any state in the {@code Context} when the Window expires (the watermark passes its
     * {@code maxTimestamp} + {@code allowedLateness}).
     *
     * @param context The context to which the window is being evaluated
     * @throws Exception The function may throw exceptions to fail the program and trigger recovery.
     */
    public void clear(Context context) throws Exception {}

    /** The context holding window metadata. */
    public abstract class Context implements java.io.Serializable {
        /** Returns the window that is being evaluated. */
        public abstract W window();

        /** Returns the current processing time. */
        public abstract long currentProcessingTime();

        /** Returns the current event-time watermark. */
        public abstract long currentWatermark();

        /**
         * State accessor for per-key and per-window state.
         *
         * <p><b>NOTE:</b>If you use per-window state you have to ensure that you clean it up by
         * implementing {@link ProcessWindowFunction#clear(Context)}.
         */
        public abstract KeyedStateStore windowState();

        /** State accessor for per-key global state. */
        public abstract KeyedStateStore globalState();

        /**
         * Emits a record to the side output identified by the {@link OutputTag}.
         *
         * @param outputTag the {@code OutputTag} that identifies the side output to emit to.
         * @param value The record to emit.
         */
        public abstract <X> void output(OutputTag<X> outputTag, X value);
    }
}

类型参数如下:

  • IN:input,数据流中窗口任务的输入数据类型
  • OUT:output,窗口任务进行计算之后的输出数据类型
  • KEY:数据中键 key 的类型
  • W:窗口的类型,是 Window 的子类型。一般情况下我们定义时间窗口,W就是 TimeWindow

定义方法如下:

process(窗口处理函数不是逐个处理数据)

  • key:窗口做统计计算基于的键,也就是之前 keyBy 用来分区的字段
  • context:当前窗口进行计算的上下文
  • elements:窗口收集到用来计算的所有数据,这是一个可迭代的集合类型
  • out:用来发送数据输出计算结果的收集器,类型为 Collector

可以明显看出,这里的参数不再是一个输入数据,而是窗口中所有数据的集合。而上下文context 所包含的内容也跟其他处理函数有所差别:

①不再提供设置定时器的方法

②由于当前不是只处理一个数据,所以也不再提供.timestamp()方法

③可以通过.window()直接获取到当前的窗口对象

④可以通过.windowState().globalState()获取到当前自定义的窗口状态和全局状态

clear()

进行窗口的清理工作:如果我们自定义了窗口状态,那么必须在.clear()方法中进行显式地清除,避免内存溢出

学习课程链接:【尚硅谷】Flink1.13实战教程(涵盖所有flink-Java知识点)_哔哩哔哩_bilibili

相关推荐
安科瑞刘鸿鹏几秒前
双碳时代,能源调度的难题正从“发电侧”转向“企业侧”
大数据·运维·物联网·安全·能源
时序数据说14 分钟前
时序数据库IoTDB数据模型建模实例详解
大数据·数据库·开源·时序数据库·iotdb
水水沝淼㵘21 分钟前
嵌入式开发学习日志(数据库II && 网页制作)Day38
服务器·c语言·网络·数据结构·数据库·学习
时序数据说22 分钟前
时序数据库IoTDB结合SeaTunnel实现高效数据同步
大数据·数据库·开源·时序数据库·iotdb
守护者1701 小时前
JAVA学习-练习试用Java实现“一个词频统计工具 :读取文本文件,统计并输出每个单词的频率”
java·学习
代码搬运媛1 小时前
ES Modules 与 CommonJS 的核心区别详解
大数据·elasticsearch·搜索引擎
不太可爱的叶某人1 小时前
【学习笔记】深入理解Java虚拟机学习笔记——第3章 垃圾收集器与内存分配策略
java·笔记·学习
Chef_Chen2 小时前
从0开始学习R语言--Day21--Kruskal-Wallis检验与Friedman检验
学习
新中地GIS开发老师2 小时前
2025武汉考研形势分析,趋势、挑战与应对策略
学习·考研·arcgis·大学生·gis开发·webgis·地理信息科学
半导体守望者3 小时前
Kyosan K5BMC ELECTRONIC INTERLOCKING MANUAL 电子联锁
经验分享·笔记·功能测试·自动化·制造