Flink的Window窗口start和end时间

1,864 阅读1分钟

有网友提到这个问题:

比如我这是5S一个窗口 17:00:03启动 第一个窗口是 00-05?

翻看源码后,找到以下代码:

	/**
	 * 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;
	}

用以上网友的问题来解答:

我们将 17:00:03 理解为xxxxx3,按照源码公式来计算:

  1. timestamp时间戳为xxxxx3,offset不主动设置的情况下默认为0,windowSize为5
  2. xxxxx3 + 5 = xxxxx8
  3. xxxxx8 % 5 = 3
  4. xxxxx3 - 3 = xxxxx0

这里就得到getWindowStartWithOffset应该返回的时间为xxxxx0,这个时间作为window的start起始时间。

那么再给个情况: 如果windowSize为3,并且时间为xxxxx11,这个时候window的start起始时间应该为多少?

当前时间为`xxxxx1`,那么window.start-end = [0,3)
当前时间为`xxxxx11`,那么window.start-end = [9,12)