Based on some advice from RI Pienaar I started to experiment a little with using the Esper complex event processing (CEP) engine in JRuby.
So I've got some sample code which does a basic match if anyone is interested:
https://github.com/kbarber/esper_testing
It requires the Esper jars to be placed in lib/ to work and has only been tested with Ruby 1.6.5 & Esper 4.4.0.
In the basic example I have the script 'basic_match.rb' I ran a series of data (3 sample orders with different prices in this case):
{"itemName"=>"test","price"=>100}
{"itemName"=>"test","price"=>200}
{"itemName"=>"test","price"=>300}
So the first query I ran against it was a basic averaging EPL query:
select avg(price) from OrderEvent
This simply averages the price over time. The output is:
% ./basic_match.rb
Dec 23, 2011 10:49:21 AM com.espertech.esper.core.service.EPServiceProviderImpl doInitialize
INFO: Initializing engine URI 'default' version 4.4.0
matched:
- {"avg(price)"=>100.0}
matched:
- {"avg(price)"=>150.0}
matched:
- {"avg(price)"=>200.0}
The second query of interest was using a clause:
select * from OrderEvent where cast(price,double) > 150
Where I'm matching all data where the price is over 150. You'll notice I had to case the price to a double to make it work:
% ./basic_match.rb
Dec 23, 2011 10:51:27 AM com.espertech.esper.core.service.EPServiceProviderImpl doInitialize
INFO: Initializing engine URI 'default' version 4.4.0
unmatched:
- {"itemName"=>"test", "price"=>100}
matched:
- {"itemName"=>"test", "price"=>200}
matched:
- {"itemName"=>"test", "price"=>300}
As you can see here, I have handling for matched and unmatched data.
So I've got some sample code which does a basic match if anyone is interested:
https://github.com/kbarber/esper_testing
It requires the Esper jars to be placed in lib/ to work and has only been tested with Ruby 1.6.5 & Esper 4.4.0.
In the basic example I have the script 'basic_match.rb' I ran a series of data (3 sample orders with different prices in this case):
{"itemName"=>"test","price"=>100}
{"itemName"=>"test","price"=>200}
{"itemName"=>"test","price"=>300}
So the first query I ran against it was a basic averaging EPL query:
select avg(price) from OrderEvent
This simply averages the price over time. The output is:
% ./basic_match.rb
Dec 23, 2011 10:49:21 AM com.espertech.esper.core.service.EPServiceProviderImpl doInitialize
INFO: Initializing engine URI 'default' version 4.4.0
matched:
- {"avg(price)"=>100.0}
matched:
- {"avg(price)"=>150.0}
matched:
- {"avg(price)"=>200.0}
The second query of interest was using a clause:
select * from OrderEvent where cast(price,double) > 150
Where I'm matching all data where the price is over 150. You'll notice I had to case the price to a double to make it work:
% ./basic_match.rb
Dec 23, 2011 10:51:27 AM com.espertech.esper.core.service.EPServiceProviderImpl doInitialize
INFO: Initializing engine URI 'default' version 4.4.0
unmatched:
- {"itemName"=>"test", "price"=>100}
matched:
- {"itemName"=>"test", "price"=>200}
matched:
- {"itemName"=>"test", "price"=>300}
As you can see here, I have handling for matched and unmatched data.
Thanks a bunch for the example. Saved me a bunch of time.
ReplyDelete