Flink学习20---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");
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值