I am new to the Apache Flink Api and I am trying to understand the different windows it offers.
I have a stream of events such as:
device_id,trigger_id,event_time,messageId
1,START,1520433909396,1
1,TRACKING,1520433914398,2
1,TRACKING,1520433919398,3
1,STOP,1520433924398,4
1,START,1520433929398,5
1,TRACKING,1520433934399,6
1,TRACKING,1520433939399,7
1,TRACKING,1520433944399,8
1,STOP,1520433949399,9
Where trigger_id can be an indicator such as: start,tracking,stop
What I would like to do is based on device_id group all incoming events and define a window based on the trigger_id. I.e group all events from start until stop and then do some calculations such as: average,max etc.
This could be defined as GlobalWindow and a Custom Trigger based on the trigger_id and use a Custom Evictor to evict the list of Events each time a stop trigger is reach.
Another option could be to use Flink CEP . I have defined the following pattern
DataStream<String> input = env.readTextFile("events.csv");
// create event stream
DataStream<Event> events = input.map(new LineToEvent());
DataStream<Event> waterMarkedStreams = events.assignTimestampsAndWatermarks(new EventAssigner());
Pattern<Event, Event> tripPattern =
Pattern.<Event>begin("start", AfterMatchSkipStrategy.noSkip())
.where(START_CONDITION)
.followedBy("middle").where(MIDDLE_CONDITION).oneOrMore()
.followedBy("end").where(END_CONDITION);
PatternStream<Event> patternStream = CEP.pattern(waterMarkedStreams, tripPattern);
DataStream<String> result = patternStream.select(
new PatternSelectFunction<Event, String>() {
@Override
public String select(Map<String, List<Event>> pattern) throws Exception {
StringBuilder builder = new StringBuilder();
builder.append(pattern.get("start").get(0).getMessageId()).append(",");
List<Event> vals = pattern.get("middle");
for (Event e: vals) {
builder .append(e.getMessageId()).append(",");
}
builder.append(pattern.get("end").get(0).getMessageId()).append(",");
return builder.toString();
}
});
result.print();
Where all conditions are static inner classes implementing SimpleCondition
However the pattern matches alll possible solution on the stream of events as so...
1> 1,2,3,4,
1> 1,2,3,6,9,
2> 1,2,4,
2> 5,6,7,8,9,
3> 1,2,3,6,7,8,9,
3> 5,6,7,9,
4> 1,2,3,6,7,9,
4> 5,6,9,
Does pattern have a notion of Evictor? How can you keep only the specific set of events. i.e.
1,2,3,4,
5,6,7,8,9,