Flink之Window与窗口开始时间

一、滚动窗口(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.000
1577822400000 -> 2020-01-01 04:00:00.000	所属窗口的起始时间是: 1577750400000 -> 2019-12-31 08:00:00.000
1577836799000 -> 2020-01-01 07:59:59.000	所属窗口的起始时间是: 1577750400000 -> 2019-12-31 08:00:00.000
1577836801000 -> 2020-01-01 08:00:01.000	所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.000
1577876400000 -> 2020-01-01 19:00:00.000	所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.000
1577890800000 -> 2020-01-01 23:00:00.000	所属窗口的起始时间是: 1577836800000 -> 2020-01-01 08:00:00.000
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Flink中的窗口window)是在流处理过程中对数据进行分组和聚合的一种机制。窗口将流数据划分为有限大小的数据块,然后对每个窗口中的数据进行处理和计算。 在Flink中,窗口有两种类型:时间窗口和计数窗口时间窗口根据事件发生的时间范围对数据进行划分,而计数窗口根据事件发生的次数对数据进行划分。 根据时间的划分方式,时间窗口可以分为滚动窗口和滑动窗口。滚动窗口将数据按照固定长度的时间间隔进行划分,比如每5分钟划分一个窗口。滑动窗口则以固定的时间间隔进行滑动,比如每隔1分钟滑动一次窗口。 对于计数窗口,可以定义固定数量的事件来进行划分,比如每10个事件划分一个窗口窗口操作可以包括聚合、计数、求和、最大值、最小值等操作。在窗口操作中,Flink提供了丰富的函数和操作符来实现不同的聚合和计算需求。 窗口操作可以通过窗口函数(window function)实现,窗口函数定义了对窗口中的数据进行聚合和处理的逻辑。 使用窗口操作可以提高流处理的性能和效率,通过将连续的数据划分为有限的窗口,可以减少计算的复杂性。同时,窗口操作也可以使得流处理应用更具可控性和灵活性。 在Flink中,窗口操作广泛应用于各种实时数据分析、实时计算和数据流处理的场景,如实时监测、实时查询、实时报警等。通过合理设置窗口大小和窗口滑动间隔,可以根据实际需求来进行数据处理和聚合,以满足不同的业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值