41、Flink 的默认窗口触发器 EventTime 代码示例

1、ContinuousEventTimeTrigger

根据间隔时间周期性触发窗口或者当 Window 的结束时间小于当前的 watermark 时触发窗口计算。

bash 复制代码
ContinuousEventTimeTrigger.of(Duration.ofSeconds(3))

2、EventTimeTrigger

EventTimeWindows 默认使用,会在 watermark 越过窗口结束时间后直接触发。

3、完整代码示例

bash 复制代码
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.GlobalWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.triggers.ContinuousEventTimeTrigger;
import org.apache.flink.streaming.api.windowing.triggers.ContinuousProcessingTimeTrigger;
import org.apache.flink.streaming.api.windowing.triggers.CountTrigger;
import org.apache.flink.streaming.api.windowing.triggers.EventTimeTrigger;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;

public class _10_WindowTriggerDefaultEventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> input = env.socketTextStream("localhost", 8888);

        // 测试时限制了分区数,生产中需要设置空闲数据源
        env.setParallelism(2);

        // 事件时间需要设置水位线策略和时间戳
        SingleOutputStreamOperator<Tuple2<String, Long>> map = input.map(new MapFunction<String, Tuple2<String, Long>>() {
            @Override
            public Tuple2<String, Long> map(String input) throws Exception {
                String[] fields = input.split(",");
                return new Tuple2<>(fields[0], Long.parseLong(fields[1]));
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Long>> watermarks = map.assignTimestampsAndWatermarks(WatermarkStrategy.<Tuple2<String, Long>>forBoundedOutOfOrderness(Duration.ofSeconds(0))
                .withTimestampAssigner(new SerializableTimestampAssigner<Tuple2<String, Long>>() {
                    @Override
                    public long extractTimestamp(Tuple2<String, Long> input, long l) {
                        return input.f1;
                    }
                }));

        // ContinuousEventTimeTrigger(根据间隔时间周期性触发窗口或者当 Window 的结束时间小于当前的 watermark 时触发窗口计算)
        //a,1718089200000
        //b,1718089200000
        //c,1718089200000
        //
        //a,1718089203000
        //b,1718089203000
        //
        //1718089200000-1718089205000
        //2> a
        //2> a
        //1718089200000-1718089205000
        //1> b
        //1> b
        //1718089200000-1718089205000
        //1> c
        //
        //c,1718089203000
        //
        //a,1718089205000
        //b,1718089205000
        //
        //1718089200000-1718089205000
        //2> a
        //2> a
        //1718089200000-1718089205000
        //1> b
        //1> b
        //1718089200000-1718089205000
        //1> c
        //1> c
        //
        //1718089200000-1718089205000
        //2> a
        //2> a
        //1718089200000-1718089205000
        //1> b
        //1> b
        //1718089200000-1718089205000
        //1> c
        //1> c
        //
        //c,1718089205000
        // EventTimeTrigger(EventTimeWindows 默认使用,会在 watermark 越过窗口结束时间后直接触发)
        //a,1718089200000
        //b,1718089200000
        //c,1718089200000
        //
        //a,1718089203000
        //b,1718089203000
        //c,1718089203000
        //
        //a,1718089205000
        //b,1718089205000
        //
        //1718089200000-1718089205000
        //2> a
        //2> a
        //1718089200000-1718089205000
        //1> b
        //1> b
        //1718089200000-1718089205000
        //1> c
        //1> c
        //
        //c,1718089205000
        watermarks.keyBy(e -> e.f0)
                .window(TumblingEventTimeWindows.of(Duration.ofSeconds(5)))
                .trigger(ContinuousEventTimeTrigger.of(Duration.ofSeconds(3)))
//                .trigger(EventTimeTrigger.create())
                .apply(new WindowFunction<Tuple2<String, Long>, String, String, TimeWindow>() {
                    @Override
                    public void apply(String s, TimeWindow timeWindow, Iterable<Tuple2<String, Long>> iterable, Collector<String> collector) throws Exception {
                        System.out.println(timeWindow.getStart() + "-" + timeWindow.getEnd());

                        for (Tuple2<String, Long> tuple : iterable) {
                            collector.collect(tuple.f0);
                        }
                    }
                }).print();

        env.execute();
    }
}
相关推荐
weixin_5051544622 分钟前
数字孪生在建设智慧城市中可以起到哪些作用或帮助?
大数据·人工智能·智慧城市·数字孪生·数据可视化
打码人的日常分享25 分钟前
智慧城市建设方案
大数据·架构·智慧城市·制造
阿里云大数据AI技术3 小时前
ES Serverless 8.17王牌发布:向量检索「火力全开」,智能扩缩「秒级响应」!
大数据·运维·serverless
Mikhail_G3 小时前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
G皮T3 小时前
【Elasticsearch】映射:null_value 详解
大数据·elasticsearch·搜索引擎·映射·mappings·null_value
大霸王龙4 小时前
软件工程的软件生命周期通常分为以下主要阶段
大数据·人工智能·旅游
点赋科技5 小时前
沙市区举办资本市场赋能培训会 点赋科技分享智能消费新实践
大数据·人工智能
YSGZJJ5 小时前
股指期货技术分析与短线操作方法介绍
大数据·人工智能
Doker 多克5 小时前
Flink CDC —部署模式
大数据·flink
Guheyunyi6 小时前
监测预警系统重塑隧道安全新范式
大数据·运维·人工智能·科技·安全