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();
    }
}
相关推荐
培培说证6 分钟前
2026 大专大数据与财务管理专业考证选择哪个更实用?
大数据
sld1686 分钟前
2026 B2B电商存量时代破局:商联达以数据与生态重构增长逻辑
大数据·人工智能
hnult15 分钟前
考试云:智能防作弊功能体系,让招聘笔试更高效、公正
大数据·人工智能·笔记
小北方城市网15 分钟前
Python FastAPI 异步性能优化实战:从 1000 QPS 到 1 万 QPS 的踩坑之路
大数据·python·性能优化·架构·fastapi·数据库架构
成长之路51416 分钟前
【工具变量】国家级城市群政策DID数据集(2003-2024年)
大数据
电商API&Tina26 分钟前
电商数据采集 API:驱动选品、定价、运营的数据分析核心引擎
大数据·开发语言·人工智能·python·数据分析·json
Elastic 中国社区官方博客27 分钟前
在 ES|QL 中的混合搜索和多阶段检索
大数据·人工智能·sql·elasticsearch·搜索引擎·ai·全文检索
zgl_2005377928 分钟前
ZGLanguage 解析SQL数据血缘 之 Python提取SQL表级血缘树信息
大数据·数据库·数据仓库·hive·hadoop·python·sql
YangYang9YangYan29 分钟前
2026大专大数据与财务管理学数据分析指南
大数据·数据挖掘·数据分析
视界先声30 分钟前
中商旅游一卡通——打造国内惠民旅游领先平台
大数据·人工智能