使用java进行Apache Flink CEP模式检测[Apache Flink]

使用java进行Apache Flink CEP模式检测[Apache Flink],java,apache,apache-flink,complex-event-processing,Java,Apache,Apache Flink,Complex Event Processing,我想做什么; 从包含在映射结构中的任何arraylist元素的CEP开始,并继续使用我已经开始的其余arraylist元素。 地图和图案结构: final Map< Integer,ArrayList<String>> deger = new HashMap<Integer,ArrayList<String>>(); deger.put(1,new ArrayList<String>(Arrays.asList("h:1

我想做什么; 从包含在映射结构中的任何arraylist元素的CEP开始,并继续使用我已经开始的其余arraylist元素。 地图和图案结构:

final Map< Integer,ArrayList<String>> deger = new HashMap<Integer,ArrayList<String>>();
        deger.put(1,new ArrayList<String>(Arrays.asList("h:1","l:1","g:0")));
        deger.put(2,new ArrayList<String>(Arrays.asList("h:1","l:1","g:1")));
        deger.put(3,new ArrayList<String>(Arrays.asList("h:2","l:3","g:1")));
        deger.put(4,new ArrayList<String>(Arrays.asList("h:0","l:2","g:2")));

 for(int i=1;i<deger.size()+1;i++) {
            temp1.add(deger.get(i));
        }

Pattern<String,?> pattern = Pattern.<String>begin("start").where(
                new SimpleCondition<String>() {
//                    @Override
                    public boolean filter(String value) throws Exception {

                        for (ArrayList<String> aa: temp1){
                            for (String dd : aa)
                                if(value.equals(dd)){ 
                                    return true;
                                }
                        }
                        return false;
                    }
                }
        ).followedBy("middle").where(
                new SimpleCondition<String>() {
                    @Override
                    public boolean filter(String value) throws Exception {
                        return value.equals(temp1.get(1));
                    }
                }
        ).followedBy("end").where(
                new SimpleCondition<String>() {
                    @Override
                    public boolean filter(String value) throws Exception {
                        return value.equals(temp1.get(2));
                    }
                }
        );

因此,目前AFAIK Flink不支持开箱即用的非有序模式,因此基本上我看到了两种解决此问题的方法:

1) 您可以创建所有可能要搜索的模式,并简单地合并所有结果数据流

2) 正如这篇文章所建议的,您可以尝试使用
IterativeCondition
,这将允许您访问之前已经匹配的元素,因此基本上您必须定义匹配列表中所有可能元素的模式,然后简单地检查最后一个条件,如果这三个元素都属于同一个列表。如果是,则找到模式。

公共静态整数temp1;
 public static  Integer temp1;
    public static  Map<Integer,ArrayList<String>> temp2 = new HashMap<>();     
final Map< Integer,ArrayList<String>> deger = new HashMap<>();
            deger.put(1,new ArrayList<>(Arrays.asList("h:1","g:1","s:0")));
            deger.put(2,new ArrayList<>(Arrays.asList("h:1","g:1","g:0")));
            deger.put(3,new ArrayList<>(Arrays.asList("h:1","c:0","g:0")));
            deger.put(4,new ArrayList<>(Arrays.asList("h:1","s:1","g:0")));


            Pattern<String,?> pattern = Pattern.<String>begin("start").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String value) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryStart : deger.entrySet()) {
                                if(entryStart.getValue().contains(value) && !temp2.containsKey(entryStart.getKey())){
                                        ArrayList<String> newList = new ArrayList<String>();
                                        newList.addAll(entryStart.getValue());
                                        newList.remove(value);
                                        temp2.put(entryStart.getKey(),newList);
                                        flag = true;
                                }
                            }
                            return flag;
                        }
                    }
            ).followedBy("middle").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String middle) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryMiddle : temp2.entrySet()) {
                                if(entryMiddle.getValue().contains(middle) && entryMiddle.getValue().size() == 2){
                                    ArrayList<String> newListMiddle = new ArrayList<String>();
                                    newListMiddle.addAll(entryMiddle.getValue());
                                    newListMiddle.remove(middle);
                                    temp2.put(entryMiddle.getKey(),newListMiddle);
                                    flag = true;
                                }
                            }
                            return flag;
                        }
                    }
            ).followedBy("end").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String end) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryEnd : temp2.entrySet()) {
                                if(entryEnd.getValue().contains(end) && entryEnd.getValue().size() == 1){
                                    flag = true;
                                    temp1 = entryEnd.getKey();
                                }
                            }
                            if (flag)
                                temp2.remove(temp1);
                            return flag;
                        }
                    }
            );

            PatternStream<String> patternStream = CEP.pattern(stream_itemset_ham,pattern);

            DataStream<String> result = patternStream.select(
                    new PatternSelectFunction<String, String>() {
                        @Override
                        public String select(Map<String, List<String>> map) throws Exception {
                            ArrayList<String> NewList= new ArrayList<>();
                            NewList.addAll(deger.get(temp1));
                            String found = "Found";
                            for (String list_element : NewList)
                                found += " " + list_element ;
                            return found;
                        }
                    }
            );
            result.print();
public static Map temp2=new HashMap(); 最终Mapdeger=newhashmap(); deger.put(1,新的ArrayList(Arrays.asList(“h:1”、“g:1”、“s:0”)); deger.put(2,新的ArrayList(Arrays.asList(“h:1”、“g:1”、“g:0”)); deger.put(3,新的ArrayList(Arrays.asList(“h:1”、“c:0”、“g:0”)); deger.put(4,新的ArrayList(Arrays.asList(“h:1”、“s:1”、“g:0”)); 模式模式=模式。开始(“开始”)。其中( 新的SimpleCondition(){ @凌驾 公共布尔筛选器(字符串值)引发异常{ flag=false; 对于(Map.Entry entryStart:deger.entrySet()){ if(entryStart.getValue().contains(value)和&!temp2.containsKey(entryStart.getKey()){ ArrayList newList=新的ArrayList(); newList.addAll(entryStart.getValue()); newList.remove(值); temp2.put(entryStart.getKey(),newList); flag=true; } } 返回标志; } } ).后面跟着(“中间”)。其中( 新的SimpleCondition(){ @凌驾 公共布尔筛选器(字符串中间)引发异常{ flag=false; 对于(Map.Entry entryMiddle:temp2.entrySet()){ if(entryMiddle.getValue().contains(middle)&&entryMiddle.getValue().size()=2){ ArrayList newListMiddle=新的ArrayList(); newListMiddle.addAll(entryMiddle.getValue()); newlistmidle.remove(中间); temp2.put(entryMiddle.getKey(),newListMiddle); flag=true; } } 返回标志; } } )。以下简称“结束”)。其中( 新的SimpleCondition(){ @凌驾 公共布尔筛选器(字符串结尾)引发异常{ flag=false; 对于(Map.Entry entryEnd:temp2.entrySet()){ if(entryEnd.getValue().contains(end)&&entryEnd.getValue().size()=1){ flag=true; temp1=entryEnd.getKey(); } } 国际单项体育联合会(旗) temp2.移除(temp1); 返回标志; } } ); PatternStream PatternStream=CEP.pattern(stream\u itemset\u ham,pattern); 数据流结果=patternStream.select( 新的PatternSelectFunction(){ @凌驾 公共字符串选择(映射)引发异常{ ArrayList NewList=新的ArrayList(); NewList.addAll(deger.get(temp1)); String found=“found”; for(字符串列表\元素:NewList) 找到+=“”+列表元素; 发现退货; } } ); result.print();

我从你的问题中了解到可以提供这种解决方案。

嘿,这里的
temp1
到底是什么?CEP在某种程度上是基于时间序列的。如果你不在乎时间,为什么不直接使用循环来查找元素呢?我在模式函数的正上方添加了temp1 it,我实时工作,所以时间很重要,所以我只是想澄清一下。如果列表中有任何模式以任何可能的顺序排列,但所有元素必须属于同一个列表,是否要发出警报?因此,如果
“h:1”、“l:1”、“g:0”
以任何顺序出现,您希望发出警报,但如果“h:1”、“l:1”、“g:2”出现,则此处不会出现警报?此解决方案将基本上匹配所有模式元素的每个组合,无论它是否真的是一个模式。我的意思是,如果你有下面的输入流
“h:1”、“d:1”、“g:1”、“b:9”、“s:0”、“z:0”、“g:0”
,它仍然会找到两种模式:
h:1g:1g:0
h:1g:1s:0
,但是在输入中没有模式。实际上,你好,多米尼克,你说得对,在我的代码中找到Paten很重要,但找到元素并不重要。如果你有不同的解决方案,你能和我分享吗?嘿,我已经添加了一个答案:)
 public static  Integer temp1;
    public static  Map<Integer,ArrayList<String>> temp2 = new HashMap<>();     
final Map< Integer,ArrayList<String>> deger = new HashMap<>();
            deger.put(1,new ArrayList<>(Arrays.asList("h:1","g:1","s:0")));
            deger.put(2,new ArrayList<>(Arrays.asList("h:1","g:1","g:0")));
            deger.put(3,new ArrayList<>(Arrays.asList("h:1","c:0","g:0")));
            deger.put(4,new ArrayList<>(Arrays.asList("h:1","s:1","g:0")));


            Pattern<String,?> pattern = Pattern.<String>begin("start").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String value) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryStart : deger.entrySet()) {
                                if(entryStart.getValue().contains(value) && !temp2.containsKey(entryStart.getKey())){
                                        ArrayList<String> newList = new ArrayList<String>();
                                        newList.addAll(entryStart.getValue());
                                        newList.remove(value);
                                        temp2.put(entryStart.getKey(),newList);
                                        flag = true;
                                }
                            }
                            return flag;
                        }
                    }
            ).followedBy("middle").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String middle) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryMiddle : temp2.entrySet()) {
                                if(entryMiddle.getValue().contains(middle) && entryMiddle.getValue().size() == 2){
                                    ArrayList<String> newListMiddle = new ArrayList<String>();
                                    newListMiddle.addAll(entryMiddle.getValue());
                                    newListMiddle.remove(middle);
                                    temp2.put(entryMiddle.getKey(),newListMiddle);
                                    flag = true;
                                }
                            }
                            return flag;
                        }
                    }
            ).followedBy("end").where(
                    new SimpleCondition<String>() {
                        @Override
                        public boolean filter(String end) throws Exception {
                            flag = false;
                            for(Map.Entry<Integer, ArrayList<String>> entryEnd : temp2.entrySet()) {
                                if(entryEnd.getValue().contains(end) && entryEnd.getValue().size() == 1){
                                    flag = true;
                                    temp1 = entryEnd.getKey();
                                }
                            }
                            if (flag)
                                temp2.remove(temp1);
                            return flag;
                        }
                    }
            );

            PatternStream<String> patternStream = CEP.pattern(stream_itemset_ham,pattern);

            DataStream<String> result = patternStream.select(
                    new PatternSelectFunction<String, String>() {
                        @Override
                        public String select(Map<String, List<String>> map) throws Exception {
                            ArrayList<String> NewList= new ArrayList<>();
                            NewList.addAll(deger.get(temp1));
                            String found = "Found";
                            for (String list_element : NewList)
                                found += " " + list_element ;
                            return found;
                        }
                    }
            );
            result.print();