Java 使用ApacheFlink进行有状态复杂事件处理
我想根据具有相同标识符的两个事件检测两个事件是否在定义的时间范围内发生。例如,Java 使用ApacheFlink进行有状态复杂事件处理,java,apache-flink,flink-cep,Java,Apache Flink,Flink Cep,我想根据具有相同标识符的两个事件检测两个事件是否在定义的时间范围内发生。例如,DoorEvent如下所示: <doorevent> <door> <id>1</id> <status>open</status> </door> <timestamp>12345679</timestamp> </doorevent> <doorevent&g
DoorEvent
如下所示:
<doorevent>
<door>
<id>1</id>
<status>open</status>
</door>
<timestamp>12345679</timestamp>
</doorevent>
<doorevent>
<door>
<id>1</id>
<status>close</status>
</door>
<timestamp>23456790</timestamp>
</doorevent>
如何在
door\u open
中将door 1的状态保存为open,以便在door\u close
步骤中,我知道door 1是正在关闭的门,而不是其他的门?如果您有Flink 1.3.0及其以上版本,则它与您想要做的事情非常直接
您的模式如下所示:
Pattern.<DoorEvent>begin("first")
.where(new SimpleCondition<DoorEvent>() {
private static final long serialVersionUID = 1390448281048961616L;
@Override
public boolean filter(DoorEvent event) throws Exception {
return event.getDoor().getStatus().equals("open");
}
})
.followedBy("second")
.where(new IterativeCondition<DoorEvent>() {
private static final long serialVersionUID = -9216505110246259082L;
@Override
public boolean filter(DoorEvent secondEvent, Context<DoorEvent> ctx) throws Exception {
if (!secondEvent.getDoor().getStatus().equals("close")) {
return false;
}
for (DoorEvent firstEvent : ctx.getEventsForPattern("first")) {
if (secondEvent.getDoor().getEventID().equals(firstEvent.getDoor().getEventId())) {
return true;
}
}
return false;
}
})
.within(Time.minutes(5));
Pattern.begin(“第一个”)
.where(新的SimpleCondition(){
私有静态最终长serialVersionUID=1390448281048961616L;
@凌驾
公共布尔筛选器(门事件)引发异常{
返回事件.getDoor().getStatus().equals(“打开”);
}
})
.“第二次”)
.where(新的迭代条件(){
私有静态最终长serialVersionUID=-92165051110246259082L;
@凌驾
公共布尔筛选器(DoorEvent secondEvent,Context ctx)引发异常{
如果(!secondEvent.getDoor().getStatus().equals(“close”)){
返回false;
}
for(DoorEvent-firstEvent:ctx.getEventsForPattern(“first”)){
if(secondEvent.getDoor().getEventID().equals(firstEvent.getDoor().getEventID())){
返回true;
}
}
返回false;
}
})
.在(时间.分钟(5))内;
因此,基本上,您可以使用IterativeConditions,获取匹配的第一个模式的上下文,并在该列表上迭代,同时比较所需的模式,并根据需要继续
迭代条件非常昂贵,应该相应地进行处理
有关条件的更多信息,请访问
Pattern.<DoorEvent>begin("first")
.where(new SimpleCondition<DoorEvent>() {
private static final long serialVersionUID = 1390448281048961616L;
@Override
public boolean filter(DoorEvent event) throws Exception {
return event.getDoor().getStatus().equals("open");
}
})
.followedBy("second")
.where(new IterativeCondition<DoorEvent>() {
private static final long serialVersionUID = -9216505110246259082L;
@Override
public boolean filter(DoorEvent secondEvent, Context<DoorEvent> ctx) throws Exception {
if (!secondEvent.getDoor().getStatus().equals("close")) {
return false;
}
for (DoorEvent firstEvent : ctx.getEventsForPattern("first")) {
if (secondEvent.getDoor().getEventID().equals(firstEvent.getDoor().getEventId())) {
return true;
}
}
return false;
}
})
.within(Time.minutes(5));