Flink Complex Event Processing

走远了吗. 提交于 2019-12-14 02:34:44

问题


I have a flink cep code that reads from socket and detects for a pattern. Lets say the pattern(word) is 'alert'. If the word alert occurs five times or more, an alert should be created. But I am getting an input mismatch error. Flink version is 1.3.0. Thanks in advance !!

package pattern;

import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.IterativeCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

import java.util.List;
import java.util.Map;

    public class cep {

         public static void main(String[] args) throws Exception {


             StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

                DataStreamSource<String> dss = env.socketTextStream("localhost", 3005);

                dss.print();

                Pattern<String,String> pattern = Pattern.<String> begin("first")
                        .where(new IterativeCondition<String>() {
                            @Override
                            public boolean filter(String word, Context<String> context) throws Exception {
                                return word.equals("alert");
                            }
                        })
                        .times(5);


                PatternStream<String> patternstream = CEP.pattern(dss, pattern);

                DataStream<String> alerts = patternstream
                        .flatSelect((Map<String,List<String>> in, Collector<String> out) -> {

                            String first = in.get("first").get(0);

                            for (int i = 0; i < 6; i++ ) {

                                out.collect(first);

                            }


                        });

                alerts.print();

                env.execute();

            }

    }


回答1:


Just some clarification on the original problem. In 1.3.0 there was a bug that made using lambdas as arguments to select/flatSelect impossible.

It was fixed in 1.3.1, so your first version of the code would work with 1.3.1.


Besides I think you misinterpret the times quantifier. It matches exact number of times. So in your case it will return only when event will be matched exactly 3 times, not 3 or more.




回答2:


So I have got the code to work. Here is the working solution,

    package pattern;

    import org.apache.flink.cep.CEP;
    import org.apache.flink.cep.PatternSelectFunction;
    import org.apache.flink.cep.PatternStream;
    import org.apache.flink.cep.pattern.Pattern;
    import org.apache.flink.cep.pattern.conditions.IterativeCondition;
    import org.apache.flink.streaming.api.datastream.DataStream;
    import org.apache.flink.streaming.api.datastream.DataStreamSource;
    import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
    import org.apache.flink.util.Collector;

    import java.util.List;
    import java.util.Map;

    public class cep {

         public static void main(String[] args) throws Exception {


             StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

                DataStreamSource<String> dss = env.socketTextStream("localhost", 3005);

                dss.print();

                Pattern<String,String> pattern = Pattern.<String> begin("first")
                        .where(new IterativeCondition<String>() {
                            @Override
                            public boolean filter(String word, Context<String> context) throws Exception {
                                return word.equals("alert");
                            }
                        })
                        .times(5);

                PatternStream<String> patternstream = CEP.pattern(dss, pattern);

                DataStream<String> alerts = patternstream
                        .select(new PatternSelectFunction<String, String>() {
                            @Override
                            public String select(Map<String, List<String>> in) throws Exception {

                                String first = in.get("first").get(0);

                                if(first.equals("alert")){

                                    return ("5 or more alerts");
                                }
                                else{

                                    return (" ");
                                }
                            }
                        });

                alerts.print();

                env.execute();

            }

    }


来源:https://stackoverflow.com/questions/45033109/flink-complex-event-processing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!