20、Flink实战:window和Time(三)TimeTumblingWindw时间滚动窗口

如下代码中,时间滚动窗口的大小是10秒,可知,1588490000至1588499999为一个窗口,本例测试数据可分为两个窗口,输出结果如下。

*

其中AscendingTimestampExtractor 指定字段为EventTime, 抽取timestamp
AscendingTimestampExtractor适用于elements的时间在每个parallel task里头是单调递增(timestamp monotony)的场景

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
/**
 * 业务场景:指定时间窗口内,统计事件/词汇的次数(热点更新等)
 */
public class TimeTumblingWindowReview {
    public static void main(String[] args) throws Exception{
        //1.创建一个 flink steam 程序的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);  // 设置使用EventTime划分窗口

        // 2. 创建数据源
        SingleOutputStreamOperator<Tuple3<Long, String, Long>> testElements = env.fromElements(
                Tuple3.of(1L, "浙江", 1588491228L),
                Tuple3.of(1L, "浙江", 1588499999L),
                Tuple3.of(1L, "上海", 1588490000L),
                Tuple3.of(1L, "上海", 1588491248L),
                Tuple3.of(2L, "上海", 1588491258L),
                Tuple3.of(2L, "上海", 1588500000L),
                Tuple3.of(2L, "浙江", 1588500000L));

        // 指定字段为EventTime, 抽取timestamp
        // AscendingTimestampExtractor适用于elements的时间在每个parallel task里头是单调递增(timestamp monotony)的场景
        SingleOutputStreamOperator<Tuple3<Long, String, Long>> input = testElements.assignTimestampsAndWatermarks(new AscendingTimestampExtractor<Tuple3<Long, String, Long>>() {
            @Override

            public long extractAscendingTimestamp(Tuple3<Long, String, Long> element) {
                return element.f2;
            }
        });

        // 3. Transformation
        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndOne = input.map(new MapFunction<Tuple3<Long, String, Long>, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(Tuple3<Long, String, Long> tp3) throws Exception {
                return Tuple2.of(tp3.f1, 1);
            }
    });

        SingleOutputStreamOperator<Tuple2<String, Integer>> summed =
        wordAndOne.keyBy(0)
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))   // 设置窗口大小为10秒
                .sum(1);

        //4. sink
        summed.print();

        //5.执行
        env.execute("TimeTumblingWindowReview");
    }
}

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: