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 2022-08-18 16:14  嘣嘣嚓  阅读(611)  评论(0编辑  收藏  举报

导航