Flink CEP

		<dependency>
			<groupId>org.apache.flink</groupId>
			<artifactId>flink-cep_${scala.binary.version}</artifactId>
			<version>${flink.version}</version>
			<scope>${scp}</scope>
		</dependency>

CEPLoginEvent

package com.claroja;

public class CEPLoginEvent {
    public String userId;
    public String ipAddr;
    public String eventType;
    public Long eventTime;

    public CEPLoginEvent(String userId, String ipAddr, String eventType, Long eventTime) {
        this.userId = userId;
        this.ipAddr = ipAddr;
        this.eventType = eventType;
        this.eventTime = eventTime;
    }

    public CEPLoginEvent() {
    }

    @Override
    public String toString() {
        return "CEPLoginEvent{" +
                "userId='" + userId + '\'' +
                ", ipAddr='" + ipAddr + '\'' +
                ", eventType='" + eventType + '\'' +
                ", eventTime='" + eventTime + '\'' +
                '}';
    }
}

CEPLoginFailDetect

package com.claroja;

import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
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.TimeCharacteristic;
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 java.util.List;
import java.util.Map;

public class CEPLoginFailDetect {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        SingleOutputStreamOperator<CEPLoginEvent> stream = env
                .fromElements(
                        new CEPLoginEvent("user_1", "0.0.0.0", "fail", 2000L),
                        new CEPLoginEvent("user_1", "0.0.0.1", "fail", 3000L),
                        new CEPLoginEvent("user_1", "0.0.0.2", "fail", 4000L),
                        new CEPLoginEvent("user_1", "0.0.0.3", "fail", 4500L)
                )
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<CEPLoginEvent>forMonotonousTimestamps()
                                .withTimestampAssigner(new SerializableTimestampAssigner<CEPLoginEvent>() {
                                    @Override
                                    public long extractTimestamp(CEPLoginEvent element, long recordTimestamp) {
                                        return element.eventTime;
                                    }
                                })
                );

        // 定义恶意登录的模板
        Pattern<CEPLoginEvent, CEPLoginEvent> pattern1 = Pattern
                .<CEPLoginEvent>begin("first")
                .where(new SimpleCondition<CEPLoginEvent>() {
                    @Override
                    public boolean filter(CEPLoginEvent value) throws Exception {
                        return value.eventType.equals("fail");
                    }
                })
                .next("second")
                .where(new SimpleCondition<CEPLoginEvent>() {
                    @Override
                    public boolean filter(CEPLoginEvent value) throws Exception {
                        return value.eventType.equals("fail");
                    }
                })
                .next("third")
                .where(new SimpleCondition<CEPLoginEvent>() {
                    @Override
                    public boolean filter(CEPLoginEvent value) throws Exception {
                        return value.eventType.equals("fail");
                    }
                })
                // 5s以内连续三次登录失败
                .within(Time.seconds(5));

        // 连续三次登录失败模板的另一种定义方法
        Pattern<CEPLoginEvent, CEPLoginEvent> pattern2 = Pattern
                .<CEPLoginEvent>begin("first")
                .times(3)
                .where(new SimpleCondition<CEPLoginEvent>() {
                    @Override
                    public boolean filter(CEPLoginEvent value) throws Exception {
                        return value.eventType.equals("fail");
                    }
                })
                .within(Time.seconds(5));

        PatternStream<CEPLoginEvent> patternStream1 = CEP.pattern(stream.keyBy(r -> r.userId), pattern1);

        PatternStream<CEPLoginEvent> patternStream2 = CEP.pattern(stream.keyBy(r -> r.userId), pattern2);

        patternStream1
                .select(new PatternSelectFunction<CEPLoginEvent, String>() {
                    @Override
                    public String select(Map<String, List<CEPLoginEvent>> map) throws Exception {
                        CEPLoginEvent first = map.get("first").iterator().next();
                        CEPLoginEvent second = map.get("second").iterator().next();
                        CEPLoginEvent thrid = map.get("third").iterator().next();

                        return first.ipAddr + "; " + second.ipAddr + "; " + thrid.ipAddr;
                    }
                })
                .print();

        patternStream2
                .select(new PatternSelectFunction<CEPLoginEvent, String>() {
                    @Override
                    public String select(Map<String, List<CEPLoginEvent>> map) throws Exception {
                        for (CEPLoginEvent e : map.get("first")) {
                            System.out.println(e.ipAddr);
                        }
                        return "hello world";
                    }
                })
                .print();

        env.execute();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值