flink的AggregateFunction,merge方法作用范围

背景

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

AggregateFunction.merge方法调用时机

AggregateFunction.merge方法其实只有在使用会话窗口进行窗口合并的时候才会用到,如下所示
在这里插入图片描述

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

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方法,

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Apache Flink 中,DataStream API 中的 aggregate 方法用于对流数据进行聚合操作。该方法的参数说明如下: ``` public <T, A, R> SingleOutputStreamOperator<R> aggregate(AggregateFunction<T, A, R> function, WindowAssigner<? super T, W> windowAssigner, Trigger<? super T, ? super W> trigger, Evictor<? super T, ? super W> evictor, AllowedLateness allowedLateness, OutputTag lateDataOutputTag) ``` 其中各参数的含义如下: - functionAggregateFunction 类型,表示聚合函数,用于定义聚合逻辑。 - windowAssigner:WindowAssigner 类型,表示窗口分配器,用于定义数据元素如何分配到不同的窗口中。 - trigger:Trigger 类型,表示触发器,用于定义窗口如何触发计算。 - evictor:Evictor 类型,表示清除器,用于定义窗口中哪些元素应该被清除。 - allowedLateness:AllowedLateness 类型,表示允许延迟的时间,用于定义窗口计算的最大允许延迟时间。 - lateDataOutputTag:OutputTag 类型,表示迟到的数据标记,用于标记迟到的数据。 其中前四个参数为必选参数,后两个参数为可选参数。 聚合函数 AggregateFunction 是必选参数,该函数用于定义聚合逻辑,需要实现 AggregateFunction 接口,并重写该接口中的四个方法:createAccumulator()、add()、getResult() 和 merge()。 窗口分配器 WindowAssigner、触发器 Trigger 和清除器 Evictor 也是必选参数,用于定义窗口如何分配、触发和清除。Flink 提供了多种窗口分配器、触发器和清除器的实现,也可以根据需求自定义实现。 允许延迟的时间 AllowedLateness 和迟到的数据标记 OutputTag 均为可选参数,用于定义窗口计算的最大允许延迟时间和标记迟到的数据。如果不指定这两个参数,则 Flink 默认不允许延迟,迟到的数据会被丢弃。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值