flink心得体会

保存点与检查点

1.保存点由提交任务时指定,恢复时手动指定保存点路径来恢复存储的状态值

2.检查点在程序中指定,设置时间间隔,自动保存存储状态值

Watermark是用于处理乱序事件的,而正确的处理乱序事件,通常用Watermark机制结合window来实现

数据流中的Watermark用于表示timestamp小于Watermark的数据,都已经到达了,因此,window的执行也是由Watermark触发的

Watermark可以理解成一个延迟触发机制,我们可以设置Watermark的延时时长t,每次系统会校验已经到达的数据中最大的maxEventTime,然后认定eventTime小于maxEventTime - t的所有数据都已经到达,如果有窗口的停止时间等于maxEventTime – t,那么这个窗口被触发执行

 

Flink接收到每一条数据时,都会产生一条Watermark,这条Watermark就等于当前所有到达数据中的maxEventTime - 延迟时长,也就是说,Watermark是由数据携带的,一旦数据携带的Watermark比当前未触发的窗口的停止时间要晚,那么就会触发相应窗口的执行。由于Watermark是由数据携带的,因此,如果运行过程中无法获取新的数据,那么没有被触发的窗口将永远都不被触发

Flink之Window与窗口开始时间

窗口的开始时间
以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

猜你喜欢

转载自blog.csdn.net/qq_35240226/article/details/105122766