CEP(Complex Event Processing)就是在无界事件流中检测事件模式
步骤分为四步
1、构建数据源
2、构建规则
3、Pattern上进行过滤
4、结果处理
多种模式:
1、严格连续模式
2、松散连续
3、不确定的松散连续
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-scala_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-cep-scala_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
需求1:用户点击完立刻购买的信息
import org.apache.flink.api.scala._
import org.apache.flink.cep.{CEP, PatternSelectFunction}
import org.apache.flink.cep.pattern.Pattern
import org.apache.flink.cep.pattern.conditions.SimpleCondition
import org.apache.flink.streaming.api.datastream.DataStream
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment
object Model02 {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
val input: DataStream[Event] = env.fromElements(
Event(1, "click", 1.0),
Event(1, "buy", 2.0),
Event(1, "show", 3.0),
Event(2, "click", 4.0),
Event(4, "show", 5.0),
Event(6, "middle", 6.0),
Event(2, "buy", 3.0),
Event(2, "buy", 42.0),
Event(3, "show", 1.0)
)
val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("click")
}
})
.next("end").subtype[Event](classOf[Event])
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("buy")
}
})
val patternStream = CEP.pattern(input, pattern)
patternStream.select(new PatternSelectFunction[Event, String] {
override def select(map: java.util.Map[String, java.util.List[Event]]): String = {
val click: Event = map.get("start").iterator().next()
val buy: Event = map.get("end").iterator().next()
s"uid: ${click.uid}, action: ${buy.action}, time: ${buy.time}"
}
}).print()
env.execute("cep demo")
}
case class Event(uid: Int, action: String, time: Double)
}
// 结果:
// name: 1, click: buy, time: 2.0
需求2:用户点击完后购买的信息(next 改为followedBy)
val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("click")
}
})
.followedBy("end").subtype[Event](classOf[Event])
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("buy")
}
})
// 结果:
// name: 1, click: buy, time: 2.0
// name: 2, click: buy, time: 3.0
需求2:用户点击完后所有的购买的信息(next 改为followedByAny)
val pattern: Pattern[Event, Event] = Pattern.begin[Event]("start")
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("click")
}
})
.followedByAny("end").subtype[Event](classOf[Event])
.where(new SimpleCondition[Event] {
override def filter(e: Event): Boolean = {
e.action.equals("buy")
}
})
// 结果:
// name: 1, click: buy, time: 2.0
// name: 1, click: buy, time: 3.0
// name: 2, click: buy, time: 3.0
// name: 1, click: buy, time: 42.0
// name: 2, click: buy, time: 42.0