Flink TimeWindow getStart和getEnd方法

使用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;
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容