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();
    }
}
相关推荐
m0_748241707 小时前
PostgreSQL 中进行数据导入和导出
大数据·数据库·postgresql
VeryReport-非常报表软件10 小时前
VeryReport和FastReport两款报表软件深度分析对比
大数据·信息可视化·数据分析
Sup星月★然10 小时前
Scala 语法入门
大数据·开发语言·scala
AutoMQ10 小时前
携手AWS,零成本在EKS上体验AutoMQ企业版
大数据·阿里云·云原生·kafka·云计算·腾讯云·aws·消息·gcp·计算·automq
B站计算机毕业设计超人11 小时前
计算机毕业设计SparkStreaming+Kafka广告推荐系统 广告预测 广告数据分析可视化 广告爬虫 大数据毕业设计 深度学习 机器学习
大数据·hadoop·python·spark·网络爬虫·课程设计·数据可视化
赶紧写完去睡觉13 小时前
尚硅谷课程【笔记】——大数据之Zookeeper【一】
大数据·linux·zookeeper
跨境一哥14 小时前
Chrome谷歌多开教程:实用方法与工具
大数据·前端·网络·chrome·tcp/ip·搜索引擎·ip
选择不变18 小时前
AI眼镜-推理成本降低将加速端侧硬件智能化-AI 眼镜、AI玩具、手机AI化
大数据·人工智能·通达信指标公式·炒股技巧·炒股指标·ai眼镜
拓端研究室19 小时前
【专题】2025年我国机器人产业发展形势展望:人形机器人量产及商业化关键挑战报告汇总PDF洞察(附原数据表)
大数据·人工智能
梦醒沉醉19 小时前
MapReduce简单应用(二)——去重、排序和平均
大数据·mapreduce