Search code examples
javacomplex-event-processingesper

Advanced window configuration: order + group + length_batch


I need some help to create a window to manage events with the folowing rules:

  • Get events ordered by timestamp (arrival can be unordered)
  • Group these events by a key (customerId)
  • Finally do a length_batch per 2 events to create an edge from one event to another.

My problem is: what could be the good way to do that ?


I tried to create a window with groupwin(customerId).length_batch(2) but I failed to find a way to add the first rule: order

My EPL queries:

create window winEdge.std:groupwin(customerId).win:length_batch(2) as select customerId,type,ts from Stream

insert into winEdge customerId,type,ts from Stream

select customerId, 'edge' as type, concatstr(type) as path, count(type) as nb_events, sum(ts) as total_time, (last(ts)-first(ts)) as elapsed, first(ts) as fromTs, last(ts) as toTs from winEdge group by customerId

I tried to add an order by condition or to use the ex:time_order() but no success.

Is there someone to help/explain me what could be the good way to do that ?


Solution

  • The time-order view orders unordered events. See http://espertech.com/esper/release-5.3.0/esper-reference/html_single/index.html#view-time-order

    Two EPL statements, the first one produces a stream for the second one to consume:

    // produce ordered stream
    insert rstream into ArrivalTimeOrderedStream
    select rstream * from MyTimestampedEvent.ext:time_order(arrival_time, 10 sec);
    
    // aggregate
    select first(xxx), last(xxx), ... from ArrivalTimeOrderedStream
    .groupwin(customerId).length_batch(2) group by customerid