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();
    }
}
相关推荐
PcVue China2 小时前
PcVue + SQL Grid : 释放数据的无限潜力
大数据·服务器·数据库·sql·科技·安全·oracle
Mephisto.java3 小时前
【大数据学习 | HBASE】hbase的读数据流程与hbase读取数据
大数据·学习·hbase
SafePloy安策7 小时前
ES信息防泄漏:策略与实践
大数据·elasticsearch·开源
学术搬运工7 小时前
【珠海科技学院主办,暨南大学协办 | IEEE出版 | EI检索稳定 】2024年健康大数据与智能医疗国际会议(ICHIH 2024)
大数据·图像处理·人工智能·科技·机器学习·自然语言处理
Matrix708 小时前
HBase理论_背景特点及数据单元及与Hive对比
大数据·数据库·hbase
B站计算机毕业设计超人9 小时前
计算机毕业设计Python+大模型农产品价格预测 ARIMA自回归模型 农产品可视化 农产品爬虫 机器学习 深度学习 大数据毕业设计 Django Flask
大数据·爬虫·python·深度学习·机器学习·课程设计·数据可视化
好记性+烂笔头10 小时前
Flink_DataStreamAPI_输出算子Sink
flink
Carl_奕然10 小时前
【大数据算法】MapReduce算法概述之:MapReduce基础模型
大数据·算法·mapreduce
Elastic 中国社区官方博客10 小时前
Elasticsearch 8.16:适用于生产的混合对话搜索和创新的向量数据量化,其性能优于乘积量化 (PQ)
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
飞翔的佩奇10 小时前
ElasticSearch:使用dsl语句同时查询出最近2小时、最近1天、最近7天、最近30天的数量
大数据·elasticsearch·搜索引擎·dsl