Windows
from <stream-name> {<conditions>}#window.<window-name>(<parameters>)
insert [<output-type>] into <stream-name> ( {<attribute-name>}| ‘*’|)
Window is a limited subset of events from an event stream. Users can define a window and then use the events on the window for his calculations.
A window has two type of outputs: current events and expired events. Current events will be emitted by a window when a new event arrives at the window. Expired events will be emitted whenever an already arrived event has expired from that window.
There are several types of windows.
1. lengthWindowLength windows - a sliding window that keeps last N events.
2. Time window - a sliding window that keeps events arrived within the last T time period.
3. Time batch window - a time window that processes events in batches. This in a loop collects the incoming events arrived within last T time period and outputs them as a batch.
4. Length batch window - a length window that outputs events as a batch only at the nth event arrival.
5. Time length window (not supported in the current version) - a sliding window that keeps the last N events that arrived within the last T time period.
6. Unique window (not supported in the current version) - keeps only the latest events that are unique according to the given unique attribute.
7. First unique window (not supported in the current version) - keeps the first events that are unique according to the given unique attribute.
Siddhi queries can have three different output types: ‘current-events’, ‘expired-events’ and ‘all-events’. Users can define these output types by adding these keywords in between ‘insert’ and ‘into’ in the syntax. Here with ‘current-events’ keyword, the outputs is only triggered when new events arrive at the window and no notification will be given when the query was triggered by the expired events from the windows. Similarly when ‘expired-events’ keyword is defined, query emits output only when it was triggered by the expired events from the windows and not from new events. When ‘all-events’ keyword is defined, the outputs are emitted when the query was triggered by both newly arrived and expired events from the window. Here when no keyword is given, by default the query assigns ‘current-events’ to its output stream.
In output event stream, users may define aggregate functions to calculate aggregations within the defined window.
We support the following types of aggregate functions.
1. sum
2. avg
3. max
4. min
5. count
6. median (not supported in the current version)
7. stddev (not supported in the current version)
8. avedev (not supported in the current version)
Aggregate function must be named using ‘as’ keyword, and this name will be used for referring that attribute and will be used as the attribute name in the output stream.
Following examples shows some sample queries
Stream defined by "define stream StockExchangeStream (symbol string, price int, volume float );" is used in below queries.
Length window
A sliding window that keeps last N events.
From the events having price >= 20 of the StockExchangeStream stream, output the expiring events of the length window to the StockQuote stream. Here the output events will have symbol and the per symbol average price as their attributes, only if the per symbol average price > 50.
from StockExchangeStream[price >= 20]#window.length(50)
insert expired-events into StockQuote symbol, avg(price) as avgPrice
group by symbol
having avgPrice>50
In the above query, avg(prize) is an aggregate function.
Time window
A sliding window that keeps events arrived within the last T time period.
From the events having symbol == 20 of the StockExchangeStream stream, output the both the newly arriving and expiring events of the time window to the IBMStockQuote stream. Here the output events will have maximum, average and minimum prices that has arrived within last minute as their attributes.
from StockExchangeStream[symbol == 'IBM']#window.time(60000)
insert all-events into IBMStockQuote max(price) as maxPrice, avg(price) as avgPrice, min(price) as minPrice
Time batch window
A time window that processes events in batches. This in a loop collects the incoming events arrived within last T time period and outputs them as a batch.
From the events of the StockExchangeStream stream, output the events per every 2 minutes from the timeBatch window to the StockQuote stream. Here the output events will have symbol and the per symbol sum of volume for last 2 minutes as their attributes.
from StockExchangeStream#window.timeBatch(120000)
insert into StockQuote symbol, sum(volume) as totalVolume
group by symbol
Length batch window
A length window that outputs events as a batch only at the nth event arrival.
From the events having price >= 20 of the StockExchangeStream stream, output the expiring events of the lengthBatch window to the StockQuote stream. Here the output events will have symbol and the per symbol average price> 50 as their attributes.
from StockExchangeStream[price >= 20]#window.lengthBatch(50)
insert expired-events into StockQuote symbol, avg(price) as avgPrice
group by symbol
having avgPrice>50
Supported units for time windows
The following units are supported when specifying the time for a time window.
Unit | Syntax |
---|---|
Year | year | years |
Month | month | months |
Week | week | weeks |
Day | day | days |
Hour | hour | hours |
Minutes | minute | minutes | min |
Seconds | second | seconds | sec |
Milliseconds | millisecond | milliseconds |
Note that each unit supports both the singular and plural format (e.g. second, seconds) and some units have a shortened form (i.e. sec, min).
Following examples use different formats available for 'minutes'.
from StockExchangeStream[symbol == 'WSO2']#window.time( 1 minute )
select max(price) as maxPrice, avg(price) as avgPrice, min(price) as minPrice
insert into WSO2StockQuote for all-events
from StockExchangeStream[symbol == 'FBX']#window.timeBatch( 5 minutes )
select max(price) as maxPrice, avg(price) as avgPrice, volume
insert into FBStockQuote
from StockExchangeStream[symbol == 'FBX']#window.timeBatch( 5 min )
select max(price) as maxPrice, avg(price) as avgPrice, volume
insert into FBStockQuote