分享

Flink难点CEP1:什么是CEP以及量词的含义

pig2 2019-6-3 20:21:59 发表于 连载型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 2 11928
问题导读

1.Flink CEP解决什么问题?
2.如何使用Flink CEP
3.CEP有哪些模式?
4.CEP量词的作用是什么?
5.本文讲了哪些量词?


Flink CEP是Flink的一个难点,很多人都不明白CEP是干啥的,从英文的意思来说,它是复杂事件处理,但这个意思就会更加让我们迷惑了,什么是复杂事件。

FlinkCEP是在Flink之上实现的复杂事件处理(CEP)库。 它允许在无休止的事件流中检测事件模式,通过检测掌握数据中重要的事情。


1.CEP是什么

1.从多个数据流中发现复杂事件,它的目标是识别有意义的事件(例如机会或则威胁),并尽快的做出响应。
2.Flink一个复杂事件处理库

特点
查询是静态的,数据是动态的,满足实现和连续查询的需求

2.CEP实现



3.Flink CEP解决什么问题

以网络攻击为例:事件什么时候开始,在DDoS中该如何过滤攻击,该如何识别哪些事件是正常事件。


4.如何使用Flink CEP
[mw_shl_code=xml,true]<dependency>
  <groupId>org.apache.flink</groupId>
  <artifactId>flink-cep-scala_2.11</artifactId>
  <version>1.8.0</version>
</dependency>
[/mw_shl_code]

5.Flink CEP程序结构
[mw_shl_code=scala,true]val input: DataStream[Event] = ...

val pattern = Pattern.begin[Event]("start").where(_.getId == 42)
  .next("middle").subtype(classOf[SubEvent]).where(_.getVolume >= 10.0)
  .followedBy("end").where(_.getName == "end")

val patternStream = CEP.pattern(input, pattern)

val result: DataStream[Alert] = patternStream.process(
    new PatternProcessFunction[Event, Alert]() {
        override def processMatch(
              `match`: util.Map[String, util.List[Event]],
              ctx: PatternProcessFunction.Context,
              out: Collector[Alert]): Unit = {
            out.collect(createAlertFrom(pattern))
        }
    })[/mw_shl_code]

说明:
程序有多个模式组成,每个模式都有自己的名称,且每个模式都是独立的。
由一个模式到下一个模式,是由用户指定的条件。
其中start,middle,end为模式名称

6.CEP独立模式介绍
A模式可以是单例模式和循环模式。单例模式接受单个事件,循环模式接受多个事件。在模式匹配符号中,模式"a b+ c? d" ,(a跟着一个或多个B,可选的后跟c,跟d),a c?,和d是单例模式,可以通过Quantifiers转换为循环模式。每个模式基于一个或则多个条件接受事件

量词【难点,需要细看
这里需要对量词做一个解释,量即为次数,也就是说在cep中,通过量词来控制次数。那么量词都有哪些?
pattern.oneOrMore(),指事件发生一次或则多次

pattern.times(#ofTimes)ofTimes是指事件发生的次数,也就是期望发生几次,比如pattern.times(4),代表期望发生4次

pattern.times(#fromTimes, #toTimes),这里指定最少发生次数和最大发生次数。比如pattern.times(2, 4),也就是最少发生2次,最多发生4次,那么也可能发生3次

pattern.greedy() ,这样是模式进入循环模式

pattern.optional()代表这个事情可以发生,或则不发生。我们来看下面

这里重点摘出来
// expecting 4 occurrences
start.times(4)

// expecting 0 or 4 occurrences
start.times(4).optional()
//start.times(4)这里大表期望发生4次
//start.times(4).optional()加上optional()代表不发生4次,那么就是0次,也就是事件可以不发生

// expecting 0, 2, 3 or 4 occurrences
start.times(2, 4).optional()
//同理这里在解释下start.times(2, 4),可能发生2,3,4次
//start.times(2, 4).optional()加上optional(),代表可以不发生也就是0次,2,3,4次


[mw_shl_code=scala,true] // expecting 4 occurrences
start.times(4)

// expecting 0 or 4 occurrences
start.times(4).optional()
//start.times(4)这里大表期望发生4次
//start.times(4).optional()加上optional()代表不发生4次,那么就是0次,也就是事件可以不发生


// expecting 2, 3 or 4 occurrences
start.times(2, 4)

// expecting 2, 3 or 4 occurrences and repeating as many as possible
start.times(2, 4).greedy()

// expecting 0, 2, 3 or 4 occurrences
start.times(2, 4).optional()
//同理这里在解释下start.times(2, 4),可能发生2,3,4次
//start.times(2, 4).optional()加上optional(),代表可以不发生,2,3,4次


// expecting 0, 2, 3 or 4 occurrences and repeating as many as possible
start.times(2, 4).optional().greedy()

// expecting 1 or more occurrences
start.oneOrMore()

// expecting 1 or more occurrences and repeating as many as possible
start.oneOrMore().greedy()

// expecting 0 or more occurrences
start.oneOrMore().optional()

// expecting 0 or more occurrences and repeating as many as possible
start.oneOrMore().optional().greedy()

// expecting 2 or more occurrences
start.timesOrMore(2)

// expecting 2 or more occurrences and repeating as many as possible
start.timesOrMore(2).greedy()

// expecting 0, 2 or more occurrences
start.timesOrMore(2).optional()

// expecting 0, 2 or more occurrences and repeating as many as possible
start.timesOrMore(2).optional().greedy()
[/mw_shl_code]
以上明白了量词的作用,对于我们Flink CEP程序打下坚实的基础。

最新经典文章,欢迎关注公众号



下一篇:Flink难点:彻底明白CEP2:条件分类
http://www.aboutyun.com/forum.php?mod=viewthread&tid=27295


加入About云知识星球,获取更多实用资料

已有(2)人评论

跳转到指定楼层
金瞳 发表于 2019-12-9 14:08:14
了解,进入下一张
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条