package com.sun;
import com.sun.pojo.LoginEvent;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternSelectFunction;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
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 java.util.List;
import java.util.Map;
/**
* 需求:检测用户行为,如果连续三次登录失败,就输出报警信息
*/
public class LoginFailDetect {
public static void main(String[] args) throws Exception {
//构建环境
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//获取事件流
KeyedStream<LoginEvent, String> stream = env
.fromElements(
new LoginEvent("user_1", "192.168.0.1", "fail", 2000L),
new LoginEvent("user_1", "192.168.0.2", "fail", 3000L),
new LoginEvent("user_2", "192.168.1.29", "fail", 4000L),
new LoginEvent("user_1", "171.56.23.10", "fail", 5000L),
new LoginEvent("user_2", "192.168.1.29", "success", 6000L),
new LoginEvent("user_2", "192.168.1.29", "fail", 7000L),
new LoginEvent("user_2", "192.168.1.29", "fail", 8000L)
)
.assignTimestampsAndWatermarks(
WatermarkStrategy.<LoginEvent>forMonotonousTimestamps()
.withTimestampAssigner(
new SerializableTimestampAssigner<LoginEvent>() {
public long extractTimestamp(LoginEvent element, long recordTimestamp) {
return element.timestamp;
}
}
)
)
.keyBy(new KeySelector<LoginEvent, String>() {
@Override
public String getKey(LoginEvent value) throws Exception {
return value.userId;
}
});
//CEP 的主要处理流程分为三步,对应到 Pattern API 中就是:
//(1)定义一个模式(Pattern);-->连续三次登录失败
Pattern<LoginEvent, LoginEvent> pattern = Pattern
.<LoginEvent>begin("first")//第一个登录失败事件
.where(new SimpleCondition<LoginEvent>() {
@Override
public boolean filter(LoginEvent value) throws Exception {
return value.eventType.equals("fail");
}
})
.next("second")//第二个登录失败事件
.where(new SimpleCondition<LoginEvent>() {
@Override
public boolean filter(LoginEvent value) throws Exception {
return value.eventType.equals("fail");
}
})
.next("third")//第三个登录失败事件
.where(new SimpleCondition<LoginEvent>() {
@Override
public boolean filter(LoginEvent value) throws Exception {
return value.eventType.equals("fail");
}
});
//(2)将Pattern应用到DataStream上,检测满足规则的复杂事件,得到一个PatternStream;
PatternStream<LoginEvent> patternStream = CEP.pattern(stream, pattern);
//(3)对 PatternStream 进行转换处理,将检测到的复杂事件提取出来,包装成报警信息输出。
SingleOutputStreamOperator<String> resultStream = patternStream
.select(new PatternSelectFunction<LoginEvent, String>() {
@Override
public String select(Map<String, List<LoginEvent>> map) throws Exception {
LoginEvent first = map.get("first").get(0);
LoginEvent second = map.get("second").get(0);
LoginEvent third = map.get("third").get(0);
System.out.println(map.get("first"));
System.out.println(map.get("first").get(0));
return first.userId + "连续三次登录失败,登录时间是: " + first.timestamp + "," + second.timestamp + "," + third.timestamp;
}
});
resultStream.print("LoginFailDetect");
env.execute("LoginFailDetect");
}
}
Flink-CEP入门案例
最新推荐文章于 2024-07-17 17:01:19 发布