flink判断两个事件之间有没有超时(不使用CEP)

1.为啥不使用cep呢,cep的超时时间设置不好配置化,无法满足扩展要求

2.超时怎么界定。A事件发生后,过了N时间,还没有收到B事件,算超时。

代码如下:

java 复制代码
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

@Slf4j
public class AsyncModelTimeoutHandler extends KeyedProcessFunction<String, JSONObject, JSONObject> {

    private static final long serialVersionUID = -61608451659272532L;
    private transient ValueState<Long> firstDataTime;

    private transient ValueState<Long> secondDataTime;

    private transient ValueState<String> eventType;

    @Override
    public void open(Configuration parameters) throws Exception {
        ValueStateDescriptor<Long> firstDataDescriptor = new ValueStateDescriptor<>("firstDataTime", Long.class);
        firstDataTime = getRuntimeContext().getState(firstDataDescriptor);

        ValueStateDescriptor<Long> secondDataDescriptor = new ValueStateDescriptor<>("secondDataTime", Long.class);
        secondDataTime = getRuntimeContext().getState(secondDataDescriptor);

        ValueStateDescriptor<String> eventTypeDescriptor = new ValueStateDescriptor<>("eventType", String.class);
        eventType = getRuntimeContext().getState(eventTypeDescriptor);
    }


    @Override
    public void processElement(JSONObject value, KeyedProcessFunction<String, JSONObject, JSONObject>.Context ctx, Collector<JSONObject> out) throws Exception {
        Long currentTimestamp = value.getLong("ts");
        if (value.containsKey("timeout")) {
            //异步请求消息
            long timeout = value.getLong("timeout");
            firstDataTime.update(currentTimestamp + timeout);
            eventType.update(value.getString("event"));
            ctx.timerService().registerProcessingTimeTimer(currentTimestamp + timeout);
        } else {
            secondDataTime.update(currentTimestamp);
        }
    }

    @Override
    public void onTimer(long timestamp, KeyedProcessFunction<String, JSONObject, JSONObject>.OnTimerContext ctx, Collector<JSONObject> out) throws Exception {
        Long firstTime = firstDataTime.value();
        Long lastTime = secondDataTime.value();
        if (lastTime == null || (firstTime != null && lastTime >= firstTime)) {
            //超时了
            log.info("AsyncModelTimeoutHandler onTimer handle triggerTime={}, firstTime={}, secondTime={},key={}", timestamp, firstTime, lastTime, ctx.getCurrentKey());
            JSONObject r = new JSONObject();
            r.put("id", ctx.getCurrentKey());
            r.put("judgeTime", timestamp);
            r.put("event", eventType.value());
            out.collect(r);
        }
        firstDataTime.clear();
        secondDataTime.clear();
        eventType.clear();
    }
}
相关推荐
198******126347 小时前
2026企业AI办公工具选型指南:从评估框架到场景适配
大数据·运维·人工智能
2601_967212727 小时前
专利型电源轨道系统技术路线对比与选型框架
大数据·运维·网络·经验分享
数字化顾问8 小时前
(138页PPT)流程体系的建设优化与运营(附下载方式)
大数据·运维·人工智能
织码weavecodes9 小时前
培训平台部署备份与容灾:从三服务交付到可恢复运行
大数据·学习·开源软件
天涯明月19939 小时前
世界模型:原理、范式与工程实践
大数据·人工智能·大模型·具身智能·世界模型
梦想画家11 小时前
SQLMesh Python 模型入门(三):前后置语句、蓝图建模与避坑指南
大数据·python·sqlmesh
和裕11 小时前
年度框架直供 vs 零散按需采购:定制纸箱采购成本、交付与服务核心区别全对比
大数据·运维·网络·人工智能·算法
具身AGI12 小时前
ARR还是交付量,物理AI 国产 的三种收入口径
大数据·人工智能
计算机源码社12 小时前
基于大数据技术的城市空气质量时序趋势与站点特征分析系统 面向监测站点的城市空气污染时空异质性分析与可视化系统
大数据·机器学习·数据挖掘·数据分析·毕业设计·课程设计·数据可视化
AC赳赳老秦13 小时前
OpenClaw 数据引用规范自动生成:为公开数据构建可信来源标注与标准引用体系
大数据·开发语言·汇编·数据库·人工智能·deepseek·openclaw