flink的AggregateFunction,merge方法作用范围

背景

AggregateFunction接口是我们经常用的窗口聚合函数,其中有一个merge方法,我们一般情况下也是实现了的,但是你知道吗,其实这个方法只有在你使用会话窗口需要进行窗口合并的时候才需要实现

AggregateFunction.merge方法调用时机

AggregateFunction.merge方法其实只有在使用会话窗口进行窗口合并的时候才会用到,如下所示

对应的源码首先查看WindowOperator.processElement方法对要合并的窗口的状态进行合并

java 复制代码
public void processElement(StreamRecord<IN> element) throws Exception {
        final Collection<W> elementWindows =
                windowAssigner.assignWindows(
                        element.getValue(), element.getTimestamp(), windowAssignerContext);
 
        // if element is handled by none of assigned elementWindows
        boolean isSkippedElement = true;
 
        final K key = this.<K>getKeyedStateBackend().getCurrentKey();
 
        if (windowAssigner instanceof MergingWindowAssigner) {
            MergingWindowSet<W> mergingWindows = getMergingWindowSet();
 
            for (W window : elementWindows) {
 
                // adding the new window might result in a merge, in that case the actualWindow
                // is the merged window and we work with that. If we don't merge then
                // actualWindow == window
                W actualWindow =
                        mergingWindows.addWindow(
                                window,
                                new MergingWindowSet.MergeFunction<W>() {
                                    @Override
                                    public void merge(
                                            W mergeResult,
                                            Collection<W> mergedWindows,
                                            W stateWindowResult,
                                            Collection<W> mergedStateWindows)
                                            throws Exception {
 
                                        triggerContext.key = key;
                                        triggerContext.window = mergeResult;
 
                                        triggerContext.onMerge(mergedWindows);
 
                                        for (W m : mergedWindows) {
                                            triggerContext.window = m;
                                            triggerContext.clear();
                                            deleteCleanupTimer(m);
                                        }
 
                                        // 合并窗口的状态
                                        windowMergingState.mergeNamespaces(
                                                stateWindowResult, mergedStateWindows);
                                    }
                                });

继续查看AbstractHeapMergingState.mergeNamespaces方法,

java 复制代码
public void mergeNamespaces(N target, Collection<N> sources) throws Exception {
    if (sources == null || sources.isEmpty()) {
        return; // nothing to do
    }
 
    final StateTable<K, N, SV> map = stateTable;
 
    SV merged = null;
 
    // merge the sources
    for (N source : sources) {
 
        // get and remove the next source per namespace/key
        SV sourceState = map.removeAndGetOld(source);
 
        if (merged != null && sourceState != null) {
            //此处合并状态并调用AggregateFunction.merge方法
            merged = mergeState(merged, sourceState);
        } else if (merged == null) {
            merged = sourceState;
        }
    }
 
    // merge into the target, if needed
    if (merged != null) {
        map.transform(target, merged, mergeTransformation);
    }
}
 
//真正调用AggregateFunction.merge方法合并自定义的状态
@Override
protected ACC mergeState(ACC a, ACC b) {
    return aggregateTransformation.aggFunction.merge(a, b);
}

这样AggregateFunction.merge的调用过程就清楚了,实际应用中,我们只需要在使用会话窗口时才需要实现这个方法,其他的基于时间窗口的方式不需要实现这个方法,当然实现了也不会有错

相关推荐
无名-CODING4 分钟前
SpringMVC处理流程完全指南:从请求到响应的完整旅程
java·后端·spring
瑶山7 分钟前
Spring Cloud微服务搭建三、分布式任务调度XXL-JOB
java·spring cloud·微服务·xxljob
Guheyunyi7 分钟前
什么是安全监测预警系统?应用场景有哪些?
大数据·运维·人工智能·安全·音视频
清 晨8 分钟前
AI 代理购物把“流量”变成“答案”,而“可信交付”决定你能不能被选中
大数据·人工智能·跨境电商·跨境·营销策略
Re.不晚9 分钟前
深入底层理解HashMap——妙哉妙哉的结构!!
java·哈希算法
Serene_Dream13 分钟前
Java 内存区域
java·jvm
小邓睡不饱耶19 分钟前
深度实战:Spark GraphX构建用户信任网络,精准锁定高价值目标用户(含完整案例)
大数据·spark·php
BYSJMG25 分钟前
计算机毕设推荐:基于大数据的共享单车数据可视化分析
大数据·后端·python·信息可视化·数据分析·课程设计
爱吃山竹的大肚肚29 分钟前
文件上传大小超过服务器限制
java·数据库·spring boot·mysql·spring
jl486382129 分钟前
【选型指南】气密性检测仪显示屏如何兼顾IP65防护、-40℃~85℃宽温与快速交付?
大数据·人工智能·stm32·单片机·物联网