Session

Provides examples on aggregating events over continuous activity sessions in a sliding manner.

To aggregate events in batches, or based on events, refer other the examples in Data Summarization section. For more information on windows refer the Siddhi query guide.

define stream PurchaseStream
        (userId string, item string, price double);

@info(name = 'Session-analysis')
from PurchaseStream#window.session(1 min, userId)
select userId,
       count() as totalItems,
       sum(price) as totalPrice
group by userId
insert into UserIdPurchaseStream;

@info(name = 'Session-analysis-with-late-event-arrivals')
from PurchaseStream#window.session(1 min, userId, 20 sec)
select userId,
       count() as totalItems,
       sum(price) as totalPrice
group by userId
insert into OutOfOrderUserIdPurchaseStream;
define stream PurchaseStream
        (userId string, item string, price double);
@info(name = 'Session-analysis')
from PurchaseStream#window.session(1 min, userId)

Aggregate events over a userId based session window with 1 minute session gap.

select userId,
       count() as totalItems,
       sum(price) as totalPrice
group by userId

Calculate count and sum of price per userId during the session.

insert into UserIdPurchaseStream;

Output when events are added to the session.

@info(name = 'Session-analysis-with-late-event-arrivals')
from PurchaseStream#window.session(1 min, userId, 20 sec)

Aggregate events over a userId based session window with 1 minute session gap, and 20 seconds of allowed latency to capture late event arrivals.

select userId,
       count() as totalItems,
       sum(price) as totalPrice
group by userId

Calculate count and sum of price per userId during the session.

insert into OutOfOrderUserIdPurchaseStream;

Output when events are added to the session.

Aggregation Behavior

When events are sent to PurchaseStream stream, following events will get emitted at UserIdPurchaseStream stream via Session-analysis query, and OutOfOrderUserIdPurchaseStream stream via Session-analysis-with-late-event-arrivals query.

Time Event Timestamp Input to PurchaseStream Output at UserIdPurchaseStream Output at OutOfOrderUserIdPurchaseStream
9:00:00 9:00:00 ['1001', 'cake', 18.0] ['1001', 1, 18.0] ['1001', 1, 18.0]
9:00:20 9:00:20 ['1002', 'croissant', 23.0] ['1002', 1, 23.0] ['1002', 1, 23.0]
9:00:40 9:00:40 ['1002', 'cake', 22.0] ['1002', 2, 45.0] ['1002', 2, 45.0]
9:01:05 9:00:50 ['1001', 'pie', 22.0] No events, as event arrived late, and did not fall into a session. ['1001', 2, 40.0]
9:01:10 9:01:10 ['1001', 'cake', 10.0] ['1001', 1, 10.0] ['1001', 3, 50.0]
9:01:50 9:01:50 ['1002', 'cake', 20.0] ['1002', 1, 20.0] ['1002', 1, 23.0]
9:02:40 9:02:40 ['1001', 'croissant', 23.0] ['1001', 1, 23.0] ['1001', 1, 23.0]
Top