from [every] <stream> -> [every] <stream> ... <stream> within <time>
insert into <stream-name> <attribute-name> {<attribute-name>}
...
Following example show a simple pattern query.
If an event arrival at Stream1 with price >= 20 is followed by an event arrival at Stream2 having price >= 1st event’s price, an output event will be triggered via StockQuote stream. The event will have two attributes; 1st event’s symbol, and 2nd event’s price.
from e1=Stream1[price >= 20] -> e2=Stream2[price >= e1.price]
insert into StockQuote e1.symbol as symbol, e2.price as price
...
Without “every” keyword, the query will only run once. If you have the “every” enclosing a pattern, then the query runs for every occurrence of that pattern.
Furthermore, If “within <time>” is used, Siddhi triggers only the patterns where the first and the last events constituting to the pattern have arrived within the given time period. In the following example, a1 and b1 should be within 3000 msec as specified.
For every infoStock event having action == "buy" following an confirmOrder event having command =="OK", the StockExchangeStream event will be matched when its price is > infoStock event’s price.
from every (a1 = infoStock[action == "buy"]
-> a2 = confirmOrder[command == "OK"] )
-> b1 = StockExchangeStream [price > infoStock.price]
within 3000
insert into StockQuote
a1.action as action, b1.price as price
...
Following example shows a sample query. It waits till the ‘buy’ action form both OrderStock1 and OrderStock2 before matching the prices in StockExchangeStream.
For every OrderStock1 event with action == "buy" and OrderStock2 event with action == "buy", the StockExchangeStream will be matched for events having price > 70 followed by events having price > 75.
from every a1 = OrderStock1[action == "buy"] and
a2 = OrderStock2[action == "buy"] ->
b1 = StockExchangeStream[price > 70] ->
b2 = StockExchangeStream[price > 75]
insert into StockQuote
a1.action as action, b1.price as priceA, b2.price as priceB
...
You can count the number of event occurrences of the same event stream with the minimum and maximum limits. For example, <1:4> means 1 to 4 events, <2:> means 2 or more, and [3] means exactly 3 events.
For every two or more infoStock events, the StockExchangeStream will be matched for three events having price > 70 followed by one to four events having price > 75.
from every a1 = infoStock[action == "buy"]<2:> ->
b1 = StockExchangeStream[price > 70]<3> ->
b2 = StockExchangeStream[price > 75]<1:4>
insert into StockQuote
a1[0].action as action, b1.price as priceA, b2[2].price as priceB
Excerpt | ||
---|---|---|
| ||
Siddhi Patterns in wiki format |
When referring to the resuts events matching the count pattern, square brackets should be used to access a specific occurrence of that event. In the above example, a1[0] will refer the 1st event of the many events that have matched the pattern and arrived via the ‘infoStock’ stream.