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();
    }
}
相关推荐
君不见,青丝成雪2 分钟前
Hadoop技术栈(四)HIVE常用函数汇总
大数据·数据库·数据仓库·hive·sql
万邦科技Lafite10 分钟前
利用淘宝开放API接口监控商品状态,掌握第一信息
大数据·python·电商开放平台·开放api接口·淘宝开放平台
更深兼春远5 小时前
flink+clinkhouse安装部署
大数据·clickhouse·flink
专注API从业者8 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
媒体人8889 小时前
GEO 优化专家孟庆涛:技术破壁者重构 AI 时代搜索逻辑
大数据·人工智能
最初的↘那颗心10 小时前
Flink Stream API 源码走读 - print()
java·大数据·hadoop·flink·实时计算
君不见,青丝成雪11 小时前
hadoop技术栈(九)Hbase替代方案
大数据·hadoop·hbase
晴天彩虹雨11 小时前
存算分离与云原生:数据平台的新基石
大数据·hadoop·云原生·spark
朗迪锋11 小时前
数字孪生 :提高制造生产力的智能方法
大数据·人工智能·制造
杨荧12 小时前
基于Python的宠物服务管理系统 Python+Django+Vue.js
大数据·前端·vue.js·爬虫·python·信息可视化