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

相关推荐
武汉唯众智创1 小时前
高职院校大数据软件教学实训室建设方案
大数据·大数据实训室·大数据实验室·大数据软件教学实训室·大数据教学实训室
Elastic 中国社区官方博客1 小时前
Elasticsearch:如何为 Elastic Stack 部署 E5 模型 - 下载及隔离环境
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
qqxhb2 小时前
系统架构设计师备考第68天——大数据处理架构
大数据·hadoop·flink·spark·系统架构·lambda·kappa
思通数科多模态大模型2 小时前
扑灭斗殴的火苗:AI智能守护如何为校园安全保驾护航
大数据·人工智能·深度学习·安全·目标检测·计算机视觉·数据挖掘
high20112 小时前
【Git】-- Rebase 减少 Commit 次数指南
大数据·git·elasticsearch
Ace_31750887763 小时前
淘宝店铺全量商品接口实战:分类穿透采集与增量同步的技术方案
大数据·数据库·python
烤麻辣烫3 小时前
黑马程序员苍穹外卖(新手)Day1
java·数据库·spring boot·学习·mybatis
郭庆汝4 小时前
(二)自然语言处理笔记——Seq2Seq架构、注意力机制
人工智能·笔记·自然语言处理
提娜米苏4 小时前
Bash Shell脚本学习——唇读数据集验证脚本
开发语言·学习·bash
盈飞无限4 小时前
质量智能革命:SPC软件助力中国制造驶入高质量发展快车道
大数据·人工智能·制造