0

I followed this example and implemented with kafka json same sample data.

consumer sample data {"temperature" : 28,"machineName":"xyz"}

DataStream<Alert> patternStream = CEP.pattern(inputEventStream, warningPattern)
        .flatSelect(new PatternFlatSelectFunction<TemperatureEvent, Alert>() {
            private static final long serialVersionUID = 1L;


        @Override
        public void flatSelect(Map<String, List<TemperatureEvent>> event, Collector<Alert> out) throws Exception {
            new Alert("Temperature Rise Detected:" + ((TemperatureEvent) event.get("first")).getTemperature()
                    + " on machine name:" + ((MonitoringEvent) event.get("first")).getMachineName());

        }

Now i am getting issue with ArrayList cast

Exception in thread "main" org.apache.flink.runtime.client.JobExecutionException: Job execution failed.
at org.apache.flink.runtime.jobmaster.JobResult.toJobExecutionResult(JobResult.java:146)
at org.apache.flink.runtime.minicluster.MiniCluster.executeJobBlocking(MiniCluster.java:647)
at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.execute(LocalStreamEnvironment.java:123)
at Test.KafkaApp.main(KafkaApp.java:61)

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to Test.TemperatureEvent at Test.KafkaApp$2.flatSelect(KafkaApp.java:53) at org.apache.flink.cep.operator.FlatSelectCepOperator.processMatchedSequences(FlatSelectCepOperator.java:66) at org.apache.flink.cep.operator.AbstractKeyedCEPPatternOperator.processEvent(AbstractKeyedCEPPatternOperator.java:382) at org.apache.flink.cep.operator.AbstractKeyedCEPPatternOperator.processElement(AbstractKeyedCEPPatternOperator.java:198) at org.apache.flink.streaming.runtime.io.StreamInputProcessor.processInput(StreamInputProcessor.java:202) at org.apache.flink.streaming.runtime.tasks.OneInputStreamTask.run(OneInputStreamTask.java:105) at org.apache.flink.streaming.runtime.tasks.StreamTask.invoke(StreamTask.java:300) at org.apache.flink.runtime.taskmanager.Task.run(Task.java:704) at java.lang.Thread.run(Unknown Source)

Till Rohrmann
  • 13,148
  • 1
  • 25
  • 51
Nathon
  • 21
  • 7

1 Answers1

1

Your code contains two problems:

  • First of all flatSelect receives a Map<String, List<TemperatureEvent>>. This means that you get potentially multiple TemperatureEvents per pattern. Thus, you have to select which one you want.
  • You don't add any Alerts to the Collector<Alert>. A flat map function does not return values but outputs them via a Collector<Alert>

Without compiling, I think this should do the trick

DataStream<Alert> patternStream = CEP.pattern(inputEventStream, warningPattern)
    .flatSelect(
    new PatternFlatSelectFunction<TemperatureEvent, Alert>() {
            private static final long serialVersionUID = 1L;

        @Override
        public void flatSelect(Map<String, List<TemperatureEvent>> event, Collector<Alert> out) throws Exception {
            TemperatureEvent temperatureEvent = event.get("first").get(0);
            out.collect(new Alert("Temperature Rise Detected:" + temperatureEvent.getTemperature() + " on machine name:" + temperatureEvent.getMachineName()));
        }
       });

By the way, the linked code from the O'Reilly repository won't compile with Flink. The PatternSelectFunction has the wrong signature.

Till Rohrmann
  • 13,148
  • 1
  • 25
  • 51
  • but its getting issue with casting – Nathon Mar 01 '19 at 08:09
  • Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to Test.TemperatureEvent at Test.KafkaApp$2.flatSelect(KafkaApp.java:53) at org.apache.flink.cep.operator.FlatSelectCepOperator.processMatchedSequences(FlatSelectCepOperator.java:66) at org.apache.flink.cep.operator.AbstractKeyedCEPPatternOperator.processEvent(AbstractKeyedCEPPatternOperator.java:382) at org.apache.flink.cep.operator.Abs – Nathon Mar 01 '19 at 08:10
  • I'm not doing the cast in my code. Which code did you try out? – Till Rohrmann Mar 01 '19 at 08:14
  • DataStream patternStream = CEP.pattern(inputEventStream, warningPattern) .select(new PatternSelectFunction() { private static final long serialVersionUID = 1L; public Alert select(Map event) throws Exception { return new Alert("Temperature Rise Detected:" + ((TemperatureEvent) event.get("first")).getTemperature() + " on machine name:" + ((MonitoringEvent) event.get("first")).getMachineName()); } – Nathon Mar 01 '19 at 08:15
  • As I've written above the signature of the `PatternSelectFunction#select` is `Alert select(Map events)` and not `Alert select(Map event)`. Please also make sure that you use the same Flink version as dependency and for the cluster. – Till Rohrmann Mar 01 '19 at 08:22
  • Yes , same thing i have changed the signature what ever you suggested but its getting ArrayList can not be cast issue – Nathon Mar 01 '19 at 08:29
  • " @Override public Alert select(Map> event) throws Exception { return new Alert("Temperature Rise Detected:" + ((TemperatureEvent) event.get("first")).getTemperature() + " on machine name:" + ((MonitoringEvent) event.get("first")).getMachineName()); }" – Nathon Mar 01 '19 at 08:29
  • Could you share a link to a Github repo with your code so that I can take a look at the problem? – Till Rohrmann Mar 01 '19 at 08:31
  • Sure. Thank you – Nathon Mar 01 '19 at 08:36
  • The code you've shared with me compiles and seems to work. Not sure whether I understand what exactly your problem is. – Till Rohrmann Mar 01 '19 at 09:32
  • It's not getting any alert message – Nathon Mar 01 '19 at 10:39
  • Have you checked your input? Has the Kafka topic the required messages enqueued? – Till Rohrmann Mar 01 '19 at 10:54
  • data {"temperature" : 28,"machineName":"xyz"} – Nathon Mar 01 '19 at 11:08
  • Finally it works. Issue with Eventdeserialization getting array index out of bound – Nathon Mar 01 '19 at 11:42