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的调用过程就清楚了,实际应用中,我们只需要在使用会话窗口时才需要实现这个方法,其他的基于时间窗口的方式不需要实现这个方法,当然实现了也不会有错

相关推荐
不想看见4042 分钟前
N-Queens -- 回溯法 -- 力扣101算法题解笔记
java·数据结构·算法
赵谨言5 分钟前
基于Python和ArcPy的不动产数据入库技术与运用
大数据·开发语言·经验分享·python
MX_93596 分钟前
Spring组件扫描原理解析
java·后端·spring
让我上个超影吧15 分钟前
天机学堂——领取优惠券优化
java
输出输入16 分钟前
Java Swing和JavaFX用哪个好
java·前端
星火开发设计18 分钟前
C++ 异常处理:try-catch-throw 的基本用法
java·开发语言·jvm·c++·学习·知识·对象
没有bug.的程序员20 分钟前
分布式配置深潜:Spring Cloud Config 与 Git 集成内核、版本回滚机制与多环境治理实战指南
java·分布式·git·spring cloud·分布式配置·版本回滚
好家伙VCC20 分钟前
# 发散创新:基于ARCore的实时3D物体识别与交互开发实战 在增强现实(
java·python·3d·ar·交互
清水白石00820 分钟前
函数签名内省实战:打造通用参数验证装饰器的完整指南
java·linux·数据库
一条咸鱼_SaltyFish27 分钟前
Elasticsearch索引规划:从字段类型到分片策略的实战思考
大数据·elasticsearch·搜索引擎·全文检索·后端开发·分片策略·索引规划