Apache flink 并行处理Flink CEP中的多个图案

Apache flink 并行处理Flink CEP中的多个图案,apache-flink,flink-streaming,flink-cep,Apache Flink,Flink Streaming,Flink Cep,我有以下的案例场景 有2个虚拟机正在向Kafka发送流,CEP引擎正在接收这些流,当单个流满足特定条件时,会生成警告 目前,CEP正在检查两名患者的两条流是否存在相同的情况(当心率>65,呼吸率>68时),并同时发出警报,如下所示 // detecting pattern Pattern<joinEvent, ? > pattern = Pattern.<joinEvent>begin("start") .subtype

我有以下的案例场景

有2个虚拟机正在向Kafka发送流,CEP引擎正在接收这些流,当单个流满足特定条件时,会生成警告

目前,CEP正在检查两名患者的两条流是否存在相同的情况(当心率>65,呼吸率>68时),并同时发出警报,如下所示

 // detecting pattern
        Pattern<joinEvent, ? > pattern = Pattern.<joinEvent>begin("start")
                .subtype(joinEvent.class).where(new FilterFunction<joinEvent>() {
                    @Override
                    public boolean filter(joinEvent joinEvent) throws Exception {
                        return joinEvent.getHeartRate() > 65 ;
                    }
                })
                .subtype(joinEvent.class)
                .where(new FilterFunction<joinEvent>() {
                    @Override
                    public boolean filter(joinEvent joinEvent) throws Exception {
                        return joinEvent.getRespirationRate() > 68;
                    }
                }).within(Time.milliseconds(100));

我如何做到这一点?我是否需要在同一环境中创建多个流环境或多个模式

根据您的需求,如果需要,您可以创建两种不同的模式以实现清晰的分离

如果您希望使用相同的模式执行此操作,那么也可以这样做。要执行此操作,请在一个卡夫卡源中阅读所有卡夫卡主题:

    FlinkKafkaConsumer010<JoinEvent> kafkaSource = new FlinkKafkaConsumer010<>(
        Arrays.asList("topic1", "topic2"),
        new StringSerializerToEvent(),
        props);
FlinkKafkaConsumer010 kafkaSource=new FlinkKafkaConsumer010(
数组.asList(“topic1”、“topic2”),
新建StringSerializerToEvent(),
道具);
在这里,我假设两个主题中的事件结构是相同的,并且您有患者姓名以及提交的事件的一部分

一旦您这样做了,就变得很容易了,因为您只需要创建一个带有“或”的模式,如下所示:

    Pattern.<JoinEvent>begin("first")
        .where(new SimpleCondition<JoinEvent>() {

          @Override
          public boolean filter(JoinEvent event) throws Exception {
            return event.getPatientName().equals("A") && event.getHeartRate() > 65 && joinEvent.getRespirationRate() > 68;
          }
        })
        .or(new SimpleCondition<JoinEvent>() {

          @Override
          public boolean filter(JoinEvent event) throws Exception {
            return event.getPatientName().equals("B") && event.getHeartRate() > 75 && joinEvent.getRespirationRate() > 78;
          }
        });
Pattern.begin(“第一个”)
.where(新的SimpleCondition(){
@凌驾
公共布尔筛选器(JoinEvent事件)引发异常{
return event.getPatientName().equals(“A”)&&event.getHeartRate()>65&&joinEvent.getRespirationRate()>68;
}
})
.或(新的SimpleCondition(){
@凌驾
公共布尔筛选器(JoinEvent事件)引发异常{
返回event.getPatientName().equals(“B”)和&event.getHeartRate()>75和&joinEvent.getRespirationRate()>78;
}
});

这将在您的条件匹配时产生匹配。虽然,我不太确定你的例子中“.inthein(Time.millizes(100))”实现了什么。

嘿,我想知道你是否找到了问题的解决方案?是的,不同的患者针对不同的主题写信,flink有许多工作人员并行工作,每个人都在听一个主题,谢谢你的回答,我认为不同的患者写了相同的源/数据流,您希望根据不同的事件/患者T.T应用不同的CEP模式
    Pattern.<JoinEvent>begin("first")
        .where(new SimpleCondition<JoinEvent>() {

          @Override
          public boolean filter(JoinEvent event) throws Exception {
            return event.getPatientName().equals("A") && event.getHeartRate() > 65 && joinEvent.getRespirationRate() > 68;
          }
        })
        .or(new SimpleCondition<JoinEvent>() {

          @Override
          public boolean filter(JoinEvent event) throws Exception {
            return event.getPatientName().equals("B") && event.getHeartRate() > 75 && joinEvent.getRespirationRate() > 78;
          }
        });