首页 > 编程知识 正文

flink window 开始时间,flink window窗口

时间:2023-05-03 11:01:43 阅读:257858 作者:2970

一、滚动窗口(TumblingEventTimeWindows) // 引入滚动窗口val streamWindow = stream.window(TumblingEventTimeWindows.of(Time.seconds(10))) 二、滑动窗口(SlidingEventTimeWindows) // 引入滑动窗口,窗口10s,没5s滑动一次val streamWindow = stream.window(SlidingEventTimeWindows.of(Time.seconds(10), Time.seconds(5))) 三、会话窗口(EventTimeSessionWindows)

相邻两次数据的 EventTime 的时间差超过指定的时间间隔就会触发执行。如果
加入 Watermark,那么当触发执行时,所有满足时间间隔而还没有触发的 Window 会
同时触发执行

// 引入会话窗口val streamWindow = stream.window(EventTimeSessionWindows.withGap(Time.seconds(5))) 四、窗口的开始时间

以EventTime和东八区为例
一般情况下按小时、分钟、秒开窗时间都是对的,
比如按小时,eventTime:2020-2-15 21:57:40
窗口开始时间:2020-2-15 21:00:00
窗口结束时间:2020-2-15 22:00:00
但是按天开窗的时候由于国内时区问题可能会和设想的不一样,窗口默认开始时间是每天八点。
窗口的开始时间是按照 TimeWindow 类的getWindowStartWithOffset方法计算,参数单位都是ms,windowSize是窗口长度

public static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) { return timestamp - (timestamp - offset + windowSize) % windowSize; }

根据该计算公式,**如果想要让窗口按一天滚动,0点到24点,**需要使用如下方式,设置第二个参数offset为16小时。如果不设置的话窗口默认是每天八点到第二天八点
.window(TumblingEventTimeWindows.of(Time.days(1), Time.hours(16)))
这样设置之后窗口就是按0点到0点开的,之后的ProcessFunction里面就可以取window的start、end了

测试代码

public static void main(String[] args) { // 注意是毫秒为单位 long windowsize = 86400000L; // 注意是毫秒为单位,滚动窗口 offset = 0L long offset = 0L; SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); long a1 = 1577808000000L; long a2 = 1577822400000L; long a3 = 1577836799000L; long a4 = 1577836801000L; long b5 = 1577876400000L; long b6 = 1577890800000L; System.out.println(a1 + " -> " + format.format(a1) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(a1, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(a1, offset, windowsize))); System.out.println(a2 + " -> " + format.format(a2) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(a2, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(a2, offset, windowsize))); System.out.println(a3 + " -> " + format.format(a3) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(a3, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(a3, offset, windowsize))); System.out.println(a4 + " -> " + format.format(a4) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(a4, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(a4, offset, windowsize))); System.out.println(b5 + " -> " + format.format(b5) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(b5, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(b5, offset, windowsize))); System.out.println(b6 + " -> " + format.format(b6) + "t所属窗口的起始时间是: " + getWindowStartWithOffset(b6, offset, windowsize) + " -> " + format.format(getWindowStartWithOffset(b6, offset, windowsize))); } private static long getWindowStartWithOffset(long timestamp, long offset, long windowSize) { return timestamp - (timestamp - offset + windowSize) % windowSize; }

测试结果:

1577808000000 -> 2020-01-01 00:00:00.000所属窗口的起始时间是: 1577750400000 -> 2019-12-31 08:00:00.0001577822400000 -> 2020-01-01 04:00:00.000所属窗口的起始时间是: 1577750400000 -> 2019-12-31 08:00:00.0001577836799000 -> 2020-01-01 07:59:59.000所属窗口的起始时间是: 1577750400000 -> 2019-12-31 08:00:00.0001577836801000 -> 2020-01-01 08:00:01.000所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.0001577876400000 -> 2020-01-01 19:00:00.000所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.0001577890800000 -> 2020-01-01 23:00:00.000所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.000

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。