1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| package org.example.cep
import java.util import java.util.Random
import org.apache.flink.cep.{PatternSelectFunction, pattern} import org.apache.flink.cep.functions.InjectionPatternFunction import org.apache.flink.cep.scala.{CEP1} import org.apache.flink.cep.pattern.Pattern import org.apache.flink.cep.pattern.conditions.{IterativeCondition, RichIterativeCondition} import org.apache.flink.streaming.api.TimeCharacteristic import org.apache.flink.streaming.api.functions.source.SourceFunction import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment import org.apache.flink.streaming.api.windowing.time.Time import org.apache.flink.streaming.api.scala._ import org.apache.flink.streaming.api.scala.DataStream
import scala.io.{BufferedSource, Source}
object LoginFailWithCustomCEP { def main(args: Array[String]): Unit = { val env = StreamExecutionEnvironment.getExecutionEnvironment env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime) env.setParallelism(1)
val loginStream = env.addSource(new CustomGenerator).assignAscendingTimestamps(_.eventTime)
loginStream.print()
val patternStream = CEP1.injectionPattern(loginStream.keyBy(_.userId), new InjectionPatternFunction[LoginEvent] { var current: String = null
override def init(): Unit = { val source = Source.fromFile("/Users/xz/Local/Projects/LearnDemo/Flink_1_12/src/main/resources/rulestatus.txt", "UTF-8") val lines = source.getLines().toArray source.close() current = lines(0) }
override def inject(): pattern.Pattern[LoginEvent, LoginEvent] = { println(current) if (current == "a") { Pattern.begin[LoginEvent]("begin") .where(new RichIterativeCondition[LoginEvent]() { override def filter(value: LoginEvent, ctx: IterativeCondition.Context[LoginEvent]): Boolean = { value.eventTpye.equals("fail") } }) .next("next") .where(new RichIterativeCondition[LoginEvent]() { override def filter(value: LoginEvent, ctx: IterativeCondition.Context[LoginEvent]): Boolean = { value.eventTpye.equals("fail") } }) } else { Pattern.begin[LoginEvent]("begin") .where(new RichIterativeCondition[LoginEvent]() { override def filter(value: LoginEvent, ctx: IterativeCondition.Context[LoginEvent]): Boolean = { value.eventTpye.equals("success") } }) .next("next") .where(new RichIterativeCondition[LoginEvent]() { override def filter(value: LoginEvent, ctx: IterativeCondition.Context[LoginEvent]): Boolean = { value.eventTpye.equals("success") } }) } }
override def getPeriod: Long = 5000
override def isChanged: Boolean = { val source = Source.fromFile("/Users/xz/Local/Projects/LearnDemo/Flink_1_12/src/main/resources/rulestatus.txt", "UTF-8") val lines = source.getLines().toArray source.close() val tempStatus = current current = lines(0) !lines(0).equals(tempStatus) } })
val loginFailDataStream = patternStream.select(new MySelectFunction())
loginFailDataStream.print("warning")
env.execute("Login Fail Detect with CEP") } }
case class LoginEvent(userId: Long, ip: String, eventTpye: String, eventTime: Long)
case class Warning(userId: Long, firstFailTime: Long, lastFailTime: Long, warningMSG: String)
class MySelectFunction() extends PatternSelectFunction[LoginEvent, Warning] { override def select(patternEvents: util.Map[String, util.List[LoginEvent]]): Warning = { val firstFailEvent = patternEvents.getOrDefault("begin", null).iterator().next() val secondEvent = patternEvents.getOrDefault("next", null).iterator().next() Warning(firstFailEvent.userId, firstFailEvent.eventTime, secondEvent.eventTime, "login fail warning") } }
class CustomGenerator extends SourceFunction[LoginEvent] { private var running = true
override def run(ctx: SourceFunction.SourceContext[LoginEvent]): Unit = { val state = Array("fail", "success") while (running) { ctx.collect(LoginEvent(scala.util.Random.nextInt(10), "192.168.0.1", state(scala.util.Random.nextInt(2)), System.currentTimeMillis())) Thread.sleep(500) } }
override def cancel(): Unit = { running = false } }
|