Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

from [every] <stream> -> [every] <stream> ... <stream> within <time>
select <attribute-name> {,<attribute-name>}
insert into <stream-name> partition by <partition-id>

...

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. Assume that a partition has already been defined in the form 'define partition stockSymbol by e1.symbol'

from e1=Stream1[price >= 20] -> e2=Stream2[price >= e1.price]
select e1.symbol as symbol, e2.price as price
insert into StockQuote partition by stockSymbol

...

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.

Info

Please note that if you use parentheses with 'every' keyword, Siddhi will wait for the pattern to complete and disregard other events till the pattern match within parentheses is complete. The conditions outside of the parentheses will be tested only after the pattern inside the parentheses is matched.

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
select a1.action as action, b1.price as price
insert into StockQuote

 

Logical Operations

You can combine streams in patterns using logical OR and AND.
1. and - occurrence of two events in any order
2. or - occurrence of an event from either of the steams in any order

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]
select  a1.action as action, b1.price as priceA, b2.price as priceB
insert into StockQuote partition by stockSymbol

...

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>
select a1[0].action as action, b1.price as priceA, b2[2].price as priceB
insert into StockQuote

               

...