I am trying to catch pattern with Apache Flink CEP (Complex Event Processing) Library. I started with the following structure, and I expect to see 2 matches for ids [1,2] and [3,4]. However I see no result.
public class StreamingJob {
private static Logger logger = LoggerFactory.getLogger(StreamingJob.class);
public static void main(String[] args) throws Exception {
// set up the streaming execution environment
final StreamExecutionEnvironment env = StreamExecutionEnvironment
.getExecutionEnvironment();
ArrayList <Event> strings = new ArrayList <>();
strings.add(new Event(0L, "room", 9));
strings.add(new Event(1L, "room", 10));
strings.add(new Event(2L, "garden", 11));
strings.add(new Event(3L, "room", 12));
strings.add(new Event(4L, "garden", 13));
strings.add(new Event(5L, "room", 14));
strings.add(new Event(6L, "room", 15));
KeyedStream <Event, String> source = env.fromCollection(strings).keyBy(Event::getName);
source.print("###-source");
Pattern <Event, ?> pattern = Pattern. <Event>begin("room")
.where(new SimpleCondition <Event>() {
@Override
public boolean filter(Event value) {
logger.info("### value: {}", value);
return value.getName().equals("room");
}
})
.next("garden")
.where(new SimpleCondition <Event>() {
@Override
public boolean filter(Event value) {
logger.info("### value: {}", value);
return value.getName().equals("garden");
}
});
PatternStream <Event> patternStream = CEP.pattern(source, pattern);
// process
DataStream <Alarm> result = patternStream.process(
new PatternProcessFunction <Event, Alarm>() {
@Override
public void processMatch(
Map <String, List <Event>> pattern,
Context ctx,
Collector <Alarm> out) throws Exception {
logger.info("### pattern: {}", pattern);
logger.info("### ctx: {}", ctx);
out.collect(new Alarm(pattern.toString()));
}
});
result.print("###");
// or select function
patternStream.select(new PatternSelectFunction <Event, Alarm>() {
@Override
public Alarm select(Map <String, List <Event>> pattern) throws Exception {
logger.info("###");
return new Alarm(pattern.toString());
}
}).print("###");
// execute program
env.execute("Flink Streaming Java API Skeleton");
}
}
The source.print()
method is printing the source stream, and for the sink I tried both process
and select
methods but neither of them can print the result. Also my logs from the filter or match methods are not printed at all. I have the impression that filter functions are not even used. The Event
and Alarm
objects are simple pojos as follows:
public class Event implements Serializable {
Long id;
String name;
Integer temperature;
//...
}
public class Alarm implements Serializable {
String text;
// ...
}
I also tried to change it to filter other fields, for example, using the temperature field, I wanted to catch odd
-even
number sequence but still, nothing was printed as a result.