CC00060.flink——|Hadoop&Flink.V15|——|Flink.v15|Flink CEP|Flink CEP案例|超时未支付|

本文详细介绍了使用Flink的复杂事件处理(CEP)模块来检测超时未支付的场景。通过分析实现思路和编程代码,揭示了如何利用Flink CEP进行实时流数据检测。
摘要由CSDN通过智能技术生成
一、CEP案例:超时未支付
### --- CEP案例:超时未支付

~~~     案例3:超时未支付
~~~     需求:找出下单后10分钟没有支付的订单
二、实现思路:
### --- 1、数据源:

new PayEvent(1L, "create", 1597905234000L),
new PayEvent(1L, "pay", 1597905235000L),
new PayEvent(2L, "create", 1597905236000L),
new PayEvent(2L, "pay", 1597905237000L),
new PayEvent(3L, "create", 1597905239000L)
### --- 2、转化watermark
### --- 3、keyby转化
### --- 4、做出Pattern(下单以后10分钟内未支付)

~~~     注意:下单为create 支付为pay ,create和pay之间不需要是严格临近,所以选择followedBy
Pattern<PayEvent, PayEvent> pattern = Pattern.<PayEvent>begin("begin")
        .where(new IterativeCondition<PayEvent>() {
            @Override 
            public boolean filter(PayEvent payEvent, Context context)
        throws Exception {
        return payEvent.getName().equals("create");
        }
        })
        .followedBy("pay")
        .where(new IterativeCondition<PayEvent>() {
            
            @Override 
            public boolean filter(PayEvent payEvent, Context context)
        throws Exception {
        return payEvent.getName().equals("pay");
        }
        })
        .within(Time.seconds(600));
### --- 5、模式匹配
### --- 6、取出匹配成功的数据
~~~     采用测输出的方式

OutputTag<PayEvent> orderTimeoutOutput = new OutputTag<PayEvent>("orderTimeout"){};
~~~     # 采用select方法

SingleOutputStreamOperator<PayEvent> result =
patternStream.select(orderTimeoutOutput, new PatternTimeoutFunction<PayEvent,
PayEvent>() {
@Override
public PayEvent timeout(Map<String, List<PayEvent>> map, long l)
throws Exception {
return map.get("begin").get(0);
}
}, new PatternSelectFunction<PayEvent, PayEvent>() {
@Override
public PayEvent select(Map<String, List<PayEvent>> map) throws
Exception {
return map.get("pay").get(0);
}
});
//result.print();
DataStream<PayEvent> sideOutput =
result.getSideOutput(orderTimeoutOutput);
sideOutput.print();
三、编程代码实现
### --- 编程代码实现

package com.yanqi.mycep;

import org.apache.flink.api.common.eventtime.*;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.PatternTimeoutFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.IterativeCondition;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

public class PayDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        env.setParallelism(1);
        DataStreamSource<PayBean> data = env.fromElements(
                new PayBean(1L, "create", 1597905234000L),
                new PayBean(1L, "pay", 1597905235000L),
                new PayBean(2L, "create", 1597905236000L),
                new PayBean(2L, "pay", 1597905237000L),
                new PayBean(3L, "create", 1597905239000L)
        );
        SingleOutputStreamOperator<PayBean> watermarks = data.assignTimestampsAndWatermarks(new WatermarkStrategy<PayBean>() {
            @Override
            public WatermarkGenerator<PayBean> createWatermarkGenerator(WatermarkGeneratorSupplier.Context context) {
                return new WatermarkGenerator<PayBean>() {
                    long maxTimeStamp = Long.MIN_VALUE;
                    long maxOutOfOrderness = 500l;

                    @Override
                    public void onEvent(PayBean event, long eventTimestamp, WatermarkOutput output) {
                        maxTimeStamp = Math.max(maxTimeStamp, event.getTs());
                    }

                    @Override
                    public void onPeriodicEmit(WatermarkOutput output) {
                        output.emitWatermark(new Watermark(maxTimeStamp - maxOutOfOrderness));
                    }
                };
            }
        }.withTimestampAssigner((element, recordTimestamp) -> element.getTs()));
        KeyedS
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值