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();
    }
}
相关推荐
档案宝档案管理5 小时前
档案宝自动化档案管理,从采集、整理到归档、利用,一步到位
大数据·数据库·人工智能·档案·档案管理
郑州光合科技余经理6 小时前
技术架构:上门服务APP海外版源码部署
java·大数据·开发语言·前端·架构·uni-app·php
云器科技6 小时前
告别Spark?大数据架构的十字路口与技术抉择
大数据·架构·spark·lakehouse·数据湖仓
zhongerzixunshi8 小时前
把握申报机遇 赋能高质量发展
大数据·人工智能
Jackeyzhe8 小时前
Flink源码阅读:双流操作
flink
AI数据皮皮侠9 小时前
中国乡村旅游重点村镇数据
大数据·人工智能·python·深度学习·机器学习
小北方城市网9 小时前
第 11 课:Python 全栈项目进阶与职业发展指南|从项目到职场的无缝衔接(课程终章・进阶篇)
大数据·开发语言·人工智能·python·数据库架构·geo
躺柒10 小时前
读共生:4.0时代的人机关系06人机合作关系
大数据·人工智能·人机协作·人机对话·人机合作
天远数科10 小时前
前端体验优化:用Node.js中间件无缝集成天远手机号码归属地核验服务
大数据·api
Hello.Reader11 小时前
Flink Table/SQL 自定义 Connector从 DDL 元数据到运行时 Source/Sink(含 Socket 全栈例子拆解)
大数据·sql·flink