In Flink, the window time is not calculated as the window start time and the window end time according to the first element entering the window. Instead, it is calculated according to the built-in calculation formula timestamp – (timestamp – offset + windowSize) % windowSize of Flink.

 ​
 /**
   * 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. (偏移时间,默认为0)
   * @param windowSize The size of the generated windows. windowSize. (为窗口大小)
   * @return window start
 */
 public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) {
   return timestamp - (timestamp - offset + windowSize) % windowSize;
 }
Copy the code

Timestamp – (timestamp – offset + windowSize) % windowSize: Where the windowSize in brackets is used to prevent negative results after subtraction, offset defaults to 0. The whole formula can be changed to timestamp – (timestamp) % windowSize, the current timestamp minus the remainder, leaving exactly an integer multiple of the window. \