Following functions are supported by Siddhi by default. Additional functionality can be written as custom function
Convert
Data Type conversion is a special use case when dealing with event streams. This will provide a way to convert the data type of the attributes of a event.
Primitive Data Type Conversion
This provides type conversion for all the primitive types such as int, double, long, float, string and bool. Below sample for more information.
define stream cseEventStream (symbol string, price float, volume string)
from cseEventStream
select symbol, convert(volume,long)/1000 as vol, sum(convert(volume,long)) as sumVolume
group by symbol
insert into StockQuote;
For above mentioned query, if we send events as {"IBM", 75.6f, "100"} and {"IBM", 75.6f, "100"} then the output will be similar as shown below.
Events{ @timeStamp = 1381334388876, inEvents = [Event{streamId='StockQuote', timeStamp=1381334388876, data=[IBM, 0.1, 100], type=new}], RemoveEvents = null }
Events{ @timeStamp = 1381334388879, inEvents = [Event{streamId='StockQuote', timeStamp=1381334388879, data=[IBM, 0.1, 200], type=new}], RemoveEvents = null }
Similar as long, other primitive types also can be used to convert the data type.
Date Type Conversion
Converting the date format is also another important type conversion method. Below sample will gives you more information on this.
Query 1
define stream typeStream (typeS string, typeSF2 string, typeD double)
from typeStream
select convert(typeS, string,"dd:MM:yy",typeSF2) as type1;
For above mentioned query, if we send event as {"23:01:13", "MM-dd-yy", 1d} then the output will be similar as shown below.
Events{ @timeStamp = 1381338885818, inEvents = [Event{streamId='null', timeStamp=1381338885818, data=[01-23-13], type=new}], RemoveEvents = null }
Here event attribute value "23:01:13" is converted into "01-23-13"
Query 2
define stream typeStream (typeS string, typeSF string, typeD double, typeI int, typeL long, typeB bool)
from typeStream
select convert(typeL,string,typeSF) as type1;
For above mentioned query, if we send event as {"23:01:13", "dd:MM:yy", 1d, 1, 1373043027082l, true} then the output will be similar as shown below.
Events{ @timeStamp = 1381339230356, inEvents = [Event{streamId='null', timeStamp=1381339230356, data=[05:07:13], type=new}], RemoveEvents = null }
Here event attribute value "1373043027082l" (date in milli second) is converted into "05-07-13" (UTC) format.
Coalesce
Coalesce allows getting the 'not null' value from a set of attributes. i.e. it coalesces multiple attributes and if at least one of the attributes have a value which is not null, that value will be selected for further processing.
define stream cseEventStream(symbol string, price1 float, price2 float, volume long);
from cseEventStream
select symbol, coalesce(price1,price2) as price,quantity;
The above query would give the value of price1 as price if price1 is not null. Otherwise it would give the value of price2.
IsMatch
IsMatch allows to compare a particular event attribute with a regular expression to see if it is a match or not and returns true or false accordingly. This can be used to filter events with attributes that match a particular regular expression.
define stream cseEventStream(symbol string, price1 float, price2 float, volume long);
from cseEventStream[isMatch('[^0-9]+',symbol)]
select symbol, coalesce(price1,price2) as price,quantity;
The above query would match all symbols that do not have any digits and allow those events to pass through the filter.