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();
    }
}
相关推荐
CAIE研习社21 分钟前
CAIE一级有学历和专业门槛吗?报考条件、考试内容与适合人群解析
大数据·人工智能
磁场转动100万匹25 分钟前
Spark Core 核心原理:从 RDD 到分布式计算
大数据·分布式·spark
强哥聊算力26 分钟前
《算力江湖·DeepSeek》第四集:DeepSeek如何改变中国算力的采购逻辑?
大数据·运维·人工智能·ai·gpu算力
l1t36 分钟前
DeepSeek总结的DuckDB 2.0.0 中的隐藏亮点
大数据·数据库·duckdb
数据皮皮侠1 小时前
全国休闲农业与乡村旅游示范县政策DID(2010-2025)
大数据·人工智能·搜索引擎·数据挖掘·智慧城市·制造
keyipatience1 小时前
Git学习(3)合并冲突全解析:从原理到实战解决
大数据·elasticsearch·搜索引擎
Paper数据分析-经管学术1 小时前
2000-2025年国家级大数据综合试验区政策试点DID面板数据
大数据
Angeldream1231 小时前
第 9 章 医学影像项目实战
大数据·图像处理·人工智能·python·医学影像处理
wasee0011 小时前
三四线城市适合投资XR大空间吗?投入、客流与回本周期测算
大数据·人工智能
Raas1001 小时前
MAI Gateway选购攻略:开源AI网关有哪些?避坑指南与对比
大数据·人工智能·gateway·ai网关·企业级ai网关·企业级ai产品