cep优化
This commit is contained in:
parent
e6a9747e3d
commit
cf59892269
|
@ -0,0 +1,19 @@
|
|||
package com.blueegg.loginfail_detect.cep
|
||||
|
||||
import java.util
|
||||
|
||||
import com.blueegg.loginfail_detect.{LoginEvent, LoginFailWarning}
|
||||
import org.apache.flink.cep.PatternSelectFunction
|
||||
|
||||
// 实现自定义PatternSelectFunction
|
||||
class LoginFailEventMatch3() extends PatternSelectFunction[LoginEvent, LoginFailWarning]{
|
||||
// 这个map的key是前面pattern的名字
|
||||
override def select(pattern: util.Map[String, util.List[LoginEvent]]): LoginFailWarning = {
|
||||
// 当前匹配到的事件序列,就保存在Map里
|
||||
val iter = pattern.get("fail").iterator()
|
||||
val firstFailEvent = iter.next()
|
||||
val secondFailEvent = iter.next()
|
||||
val thirdFailEvent = iter.next()
|
||||
LoginFailWarning(firstFailEvent.userId, firstFailEvent.timestamp, thirdFailEvent.timestamp, "login fail")
|
||||
}
|
||||
}
|
|
@ -38,7 +38,7 @@ object LoginFailWithCep2 {
|
|||
val patternStream = CEP.pattern(loginEventStream.keyBy(_.userId), loginFailPattern)
|
||||
|
||||
// 3.检出符合模式的数据流,需要调用select
|
||||
val loginFailWarningStream = patternStream.select(new LoginFailEventMatch())
|
||||
val loginFailWarningStream = patternStream.select(new LoginFailEventMatch2())
|
||||
|
||||
loginFailWarningStream.print()
|
||||
env.execute("login fail with cep job")
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package com.blueegg.loginfail_detect.cep
|
||||
|
||||
import com.blueegg.loginfail_detect.LoginEvent
|
||||
import org.apache.flink.cep.scala.CEP
|
||||
import org.apache.flink.cep.scala.pattern.Pattern
|
||||
import org.apache.flink.streaming.api.TimeCharacteristic
|
||||
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor
|
||||
import org.apache.flink.streaming.api.scala._
|
||||
import org.apache.flink.streaming.api.windowing.time.Time
|
||||
|
||||
// 5秒之内有3次失败报警
|
||||
object LoginFailWithCep3 {
|
||||
def main(args: Array[String]): Unit = {
|
||||
val env = StreamExecutionEnvironment.getExecutionEnvironment
|
||||
env.setParallelism(1)
|
||||
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime)
|
||||
|
||||
val resource = getClass.getResource("/LoginLog.csv")
|
||||
val inputStream = env.readTextFile(resource.getPath)
|
||||
|
||||
val loginEventStream = inputStream
|
||||
.map(data => {
|
||||
val arr = data.split(",")
|
||||
LoginEvent(arr(0).toLong, arr(1), arr(2), arr(3).toLong)
|
||||
})
|
||||
.assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor[LoginEvent](Time.seconds(3)) {
|
||||
override def extractTimestamp(element: LoginEvent): Long = element.timestamp * 1000
|
||||
})
|
||||
|
||||
// 1. 定义匹配的模式,要求是一个登陆失败事件后,紧跟另一个登陆失败事件
|
||||
val loginFailPattern = Pattern
|
||||
.begin[LoginEvent]("fail").where(_.eventType == "fail").times(3).consecutive()
|
||||
.within(Time.seconds(5))
|
||||
|
||||
// 2.将模式应用到数据流上,得到一个PatternStream
|
||||
val patternStream = CEP.pattern(loginEventStream.keyBy(_.userId), loginFailPattern)
|
||||
|
||||
// 3.检出符合模式的数据流,需要调用select
|
||||
val loginFailWarningStream = patternStream.select(new LoginFailEventMatch3())
|
||||
|
||||
loginFailWarningStream.print()
|
||||
env.execute("login fail with cep job")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue