目录
1 CEP的概念
2 CEP特点
3 CEP的开发基本开发规则
4 CEP的匹配规则
4.1 条件匹配
4.2 模式匹配
4.3 量词
4.4 超时
1 CEP的概念
一个或多个简单事件构成的事件流满足通过一定的匹配规则,然后输出用户想得到的数据,满足规则的复杂事件。
2 CEP特点
具有低延迟,高吞吐的处理
3 CEP的开发基本开发规则
1)定义规则
//begin("规则的名字")
//.where()条件
//创建where()中的IterativeCondition
Pattern pattern= Pattern.begin("start").where(
new IterativeCondition() {
@Override
public boolean filter(WaterSensor value, Context ctx) throws Exception {
return "sensor_1".equals(value.getId());
}
}
)
2)应用规则
//sensor为来源数据 //pattern为定义的规则 PatternStreamsensorPM = CEP.pattern(sensorDS, pattern);
3)匹配结果
//匹配结果 SingleOutputStreamOperatorresultDS = sensorPM.select(new PatternSelectFunction () { @Override public String select(Map > pattern) throws Exception { return pattern.toString(); } });
4 CEP的匹配规则
4.1 条件匹配
1)简单匹配
Pattern
.begin("start")
.where(_._1 == "a")// 并且条件
2)组合条件
Pattern
.begin("start")
.where(_._1 == "a")
.or(_._1 == "b") // 或条件
4.2 模式匹配
1)严格近邻
严格的满足联合条件, 当且仅当数据为连续的a,b时,模式才会被命中。如果数据为a,c,b,由于a的后面跟了c,所以a会被直接丢弃,模式不会命中。如下图
Pattern
.begin("start")
.where(_._1 == "a")
.next("next")
.where(_._1 == "b")
2)宽松近邻
松散的满足联合条件, 当且仅当数据为a,b或者为a,c,b,模式均被命中,中间的c会被忽略掉。
Pattern
.begin("start")
.where(_._1 == "a")
.followedBy("followedBy")
.where(_._1 == "b")
3)非确定性宽松近邻
非确定的松散满足条件, 当且仅当数据为a,c,b,b时,对于followedBy模式而言命中的为{a,b},对于followedByAny而言会有两次命中{a,b},{a,b}
Pattern
.begin("start")
.where(_._1 == "a")
.followedByAny("followedByAny")
.where(_._1 == "b")
4.3 量词
1)固定次数(N)
times(n) 表示当前条件匹配n次,之后当作一个整体,再与其他事件产生关联。
2)多次数
time(m,n)表示当前条件匹配m次到n次都可以。
4.4 超时
whithin(TIme.seconds(5)) 表示在规定时间内进行规则匹配



