flink streaming-windows的开始和结束时间


0

用什么方法显示窗口的开始和结束时间?比如实现用户定义的窗口?

想知道windows开始和计算的时间,以便输出

quantity(WindowAll Sum), window_start_time, window_end_time
12, 1:13:21, 1:13:41
6, 1:13:41, 1:15:01

1 答案


0

找到了答案。时间窗口.class有getStart()和getEnd()

用法示例:

public static class SumAllWindow implements AllWindowFunction<Tuple2<String,Integer>,
        Tuple3<Integer, String, String>, TimeWindow> {

private static transient DateTimeFormatter timeFormatter =
        DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SS").withLocale(Locale.GERMAN).
                withZone(DateTimeZone.forID("Europe/Berlin"));

@Override
public void apply (TimeWindow window, Iterable&lt;Tuple2&lt;String, Integer&gt;&gt; values,
                   Collector&lt;Tuple3&lt;Integer, String, String&gt;&gt; out) throws Exception {

    DateTime startTs = new DateTime(window.getStart(), DateTimeZone.forID("Europe/Berlin"));
    DateTime endTs = new DateTime(window.getEnd(), DateTimeZone.forID("Europe/Berlin"));

    int sum = 0;
    for (Tuple2&lt;String, Integer&gt; value : values) {
        sum += value.f1;
    }
    out.collect(new Tuple3&lt;&gt;(sum, startTs.toString(timeFormatter), endTs.toString(timeFormatter)));
}

}

在main()中

msgStream.timeWindowAll(Time.of(6, TimeUnit.SECONDS)).apply(new SumAllWindow()).print();

我来回答

写文章

提问题

面试题