Flink TimeWindow getStart和getEnd

2020-04-12  本文已影响0人  lsyarn

使用TimeWindow时,窗口算子(如aggregate, reduce等)允许传入一个ProcessWindowFunction参数。通过重写ProcessWindowFunctionprocess(KEY key, Context context, Iterable<IN> elements, Collector<OUT> out)方法,可以对窗口计算结果进行再加工,其中context提供了对当前window的访问。使用context.window().getStart()context.window().getEnd()可以分别得到窗口打开时间和关口关闭时间。

context.window().getStart()得到的是属于窗口的第一条数据的时间。

context.window().getEnd()得到的是不属于窗口的第一条数据的时间。

这里需要注意的是,getEnd()得到的时间不输入窗口,当使用事件时间时需要尤为注意,如果你需要的是这个窗口里面最后一个事件的发生时间,不能用getEnd()的结果。

附上源码:

//来自 org/apache/flink/streaming/api/windowing/windows/TimeWindow.java
    /**
     * Gets the starting timestamp of the window. This is the first timestamp that belongs
     * to this window.
     *
     * @return The starting timestamp of this window.
     */
    public long getStart() {
        return start;
    }

    /**
     * Gets the end timestamp of this window. The end timestamp is exclusive, meaning it
     * is the first timestamp that does not belong to this window any more.
     *
     * @return The exclusive end timestamp of this window.
     */
    public long getEnd() {
        return end;
    }
上一篇下一篇

猜你喜欢

热点阅读