Flink-窗口起始时间确定规则、闭窗时间计算规则

窗口何时开始

看下 TumblingEventTimeWindows 这个类
复制代码
 @Override
    public Collection<TimeWindow> assignWindows(
            Object element, long timestamp, WindowAssignerContext context) {
        if (timestamp > Long.MIN_VALUE) {
            if (staggerOffset == null) {
                staggerOffset =
                        windowStagger.getStaggerOffset(context.getCurrentProcessingTime(), size);
            }
            // Long.MIN_VALUE is currently assigned when no timestamp is present
            long start =
                    TimeWindow.getWindowStartWithOffset(
                            timestamp, (globalOffset + staggerOffset) % size, size);
            return Collections.singletonList(new TimeWindow(start, start + size));
        } else {
            throw new RuntimeException(
                    "Record has Long.MIN_VALUE timestamp (= no timestamp marker). "
                            + "Is the time characteristic set to 'ProcessingTime', or did you forget to call "
                            + "'DataStream.assignTimestampsAndWatermarks(...)'?");
        }
    }
 
 
    /**
     * Method to get the window start for a timestamp.
     *
     * @param timestamp epoch millisecond to get the window start.
     * @param offset The offset which window start would be shifted by.
     * @param windowSize The size of the generated windows.
     * @return window start
     */
    public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
        return timestamp - (timestamp - offset + windowSize) % windowSize;
    }
复制代码
 
减完offset再添加windowSize是为了防止负数出现
没有offset,则是以整数倍形式出现
如果windowSize为5s,则窗口起始为0s,5s,10s以此类推
如果offset为1s,则窗口起始为1s,6s,11s以此类推
 

watermark何时闭窗计算

举例:窗口时长(window_size)5s,watermark_size 3s,假设不设置offset。
会生成以下窗口:[0, 5),[5, 10),[10, 15)…以此类推
闭窗时间规则计算 close_time:window_start + window_size + watermark_size
[0, 5)窗口关闭:当出现数据时间 >= close_time时,即 0 + 5 + 3 = 8的时候
[5, 10)窗口关闭:当出现数据时间 >= close_time时,即 5 + 5 + 3 = 13的时候
以此类推
 
 

posted on   嘣嘣嚓  阅读(682)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示