5-minute Open Esper Tour

Source: Internet
Author: User

Hara Coffeeonesugar

Translation: Liu Binhua

In my previous post, I mentioned my recent passion for complex event processing (CEP) (complex incident handling). In short, the CEP takes the data stream as input, redirecting the data (or part of the data) to the listener based on a set of predefined rules, or triggering an event when the implied patterns (pattern) are found in the data. CEP is particularly useful in scenarios where large amounts of data are generated and need to be analyzed in real time.

There is a very nice software project that allows you to do this, called Esper. You can find the website of the project here. Esper provides programmers with a language called EPL, some similar to the SQL language, which can be well used to configure rules and patterns that will be applied to the data flow.

The documentation that accompanies the Esper is quite complete, but lacks practical examples, which makes it seem difficult to use. So here's a Esper 5-minute guide. While the following example is written in Java, Esper is actually supported both in Java and C #. I assume here that you have downloaded Esper, and if not, you can do so by clicking on the link. After extracting the file you just downloaded, you should be able to find a folder called esper-3.1.0 somewhere on your disk. (Translator: This article is relatively early, now Esper the latest version is 5.2.0)

Before you start, you need to add a few library files to your project, of course, Esper-3.1.0.jar is one of them and you also need 4 other third-party library files that they can find in the Esper-3.1.0.jar/esper/lib folder.

Now let's start with our 5 minutes. Before you throw the data you need to analyze into the pipeline of the CEP engine, you need to structure the data into objects. Let's use a simple example of writing a class (or bean) to describe the price of a stock at a given time:

import java.util.Date;  Public Static classTick {String symbol;        Double Price;         Date TimeStamp;  PublicTick (String S,DoublePLongt) {symbol=s; Price=p; TimeStamp=NewDate (t); }         Public DoubleGetPrice () {returnPrice ;}  PublicString Getsymbol () {returnsymbol;}  PublicDate Gettimestamp () {returnTimeStamp;} @Override PublicString toString () {return "Price :"+ price.tostring () +"Time :"+timestamp.tostring (); }    }

It has 3 properties: stock ticker, price, and timestamp. Before we start generating billions of data, we need to inform the engine what objects it needs to process, which is achieved by using a configuration object when instantiating the CEP engine:

Import com.espertech.esper.client.*;  Public classMain { Public Static voidmain (String [] args) {//The Configuration is meant only as an Initialization-time object.Configuration Cepconfig =NewConfiguration (); //We Register Ticks as objects the engine would have to handleCepconfig.addeventtype ("Stocktick", Tick.class. GetName ()); //We setup the engineEpserviceprovider CEP = Epserviceprovidermanager.getprovider ("Mycepengine", Cepconfig); }}

For testing purposes, we now create a function to generate random data, and throw them into the CEP engine, we call this function "Generaterandomtick", which takes the Epruntime object as a parameter, which is used to pass events to the CEP engine:

import Java.util.random;import com.espertech.esper.client.*;  Public classExamplemain {Private StaticRandom generator=NewRandom ();  Public Static voidGeneraterandomtick (Epruntime ceprt) {DoublePrice = (Double) Generator.nextint (Ten); LongTimeStamp =System.currenttimemillis (); String symbol="AAPL"; Tick Tick=NewTick (Symbol,price,timestamp); System. out. println ("Sending tick:"+tick);     Ceprt.sendevent (tick);}  Public Static voidMain (string[] args) {//The Configuration is meant only as an Initialization-time object.Configuration Cepconfig =NewConfiguration (); Cepconfig.addeventtype ("Stocktick", Tick.class. GetName ()); Epserviceprovider CEP=epserviceprovidermanager.getprovider ("Mycepengine", Cepconfig); Epruntime ceprt=Cep.getepruntime (); }}

Now that we have a working CEP engine, and the constant input of false data, it's time to create our first rule, with Esper's argument, our first EPL statement. To do this, we need to ask the administrator of the engine to record our statements. The CEP engine then filters the data it receives according to the definition of the EPL statement, triggering the event when the data satisfies the selection criteria or pattern in the statement.

 Public Static voidMain (string[] args) {//The Configuration is meant only as an Initialization-time object.Configuration Cepconfig =NewConfiguration (); Cepconfig.addeventtype ("Stocktick", Tick.class. GetName ()); Epserviceprovider CEP= Epserviceprovidermanager.getprovider ("Mycepengine", Cepconfig); Epruntime ceprt=Cep.getepruntime (); //We Register an EPL statementEpadministrator Cepadm =Cep.getepadministrator (); Epstatement cepstatement= Cepadm.createepl ("SELECT * from"+"Stocktick (symbol= ' AAPL '). Win:length (2)"+"Have avg (price) > 6.0"); }

Here, our rule is set to trigger an event whenever the average of the last 2 data is greater than 6.0.

The next step is to create a listener and correlate it with the events generated by our selection rules. You can do this:

Cepstatement.addlistener (new Ceplistener ());

There are different ways to implement listeners, the simplest of which is the following. The listener here simply prints it out of the object received from the engine:

 Public Static class  publicvoid  update (eventbean[] NewData, eventbean[] olddata) {         System. out. println (""                            + newdata[0].getunderlying ());}    }

So far, it looks good. Now is the time to test our code. Let's generate some data to see if everything works. You can do this by adding a line to the main function:

 for (int05; i++)    Generaterandomtick (CEPRT);

Now all the code looks like this (I put the tick class and the entry function together so you can copy and paste them into a file and run them)

Import com.espertech.esper.client.*; import Java.util.random;import java.util.Date; Public classExamplemain { Public Static classTick {String symbol;        Double Price;         Date TimeStamp;  PublicTick (String S,DoublePLongt) {symbol=s; Price=p; TimeStamp=NewDate (t); }         Public DoubleGetPrice () {returnPrice ;}  PublicString Getsymbol () {returnsymbol;}  PublicDate Gettimestamp () {returnTimeStamp;} @Override PublicString toString () {return "Price :"+ price.tostring () +"Time :"+timestamp.tostring (); }    }     Private StaticRandom generator =NewRandom ();  Public Static voidGeneraterandomtick (Epruntime ceprt) {DoublePrice = (Double) Generator.nextint (Ten); LongTimeStamp =System.currenttimemillis (); String symbol="AAPL"; Tick Tick=NewTick (symbol, price, timeStamp); System. out. println ("Sending tick:"+tick);     Ceprt.sendevent (tick); }      Public Static classCeplistener implements Updatelistener { Public voidUpdate (eventbean[] NewData, eventbean[] olddata) {System. out. println ("Event Received:"+ newdata[0].getunderlying ()); }    }      Public Static voidMain (string[] args) {//The Configuration is meant only as an Initialization-time object.Configuration Cepconfig =NewConfiguration (); Cepconfig.addeventtype ("Stocktick", Tick.class. GetName ()); Epserviceprovider CEP= Epserviceprovidermanager.getprovider ("Mycepengine", Cepconfig); Epruntime ceprt=Cep.getepruntime (); Epadministrator Cepadm=Cep.getepadministrator (); Epstatement cepstatement= Cepadm.createepl ("SELECT * from"+"Stocktick (symbol= ' AAPL '). Win:length (2)"+"Have avg (price) > 6.0"); Cepstatement.addlistener (NewCeplistener ()); //We generate a few ticks ...         for(inti =0; I <5; i++) {Generaterandomtick (CEPRT); }    }}


Output:

6.0 time:tue Jul 01:11:15 cest0.0 time:tue Jul 01:11:15 cest7.0 time:tue Jul 21 01:11 : CEST4.0 time:tue Jul 01:11:15 cest9.0 time:tue Jul 01:11:15 cest 9.0 Time:tue Jul 01:11:15 CEST 2009

As you can see, only the last two rows of data mean more than 6, so only one event was eventually triggered by the engine. Pretty good!

Oh, you might be worried about the first line in the output, yes, there's a little bit of a problem here. In fact, the log generation package log4j used by Esper caused this warning. It can be configured by a file called Log4j.xml, and you can find it in the/etc directory under an example in Esper-3.1.0/examples. I don't think it's a good idea to get an XML configuration file for all of our programs, so we'll use the following code to configure our logger with some import and code at the beginning of your file:

ImportOrg.apache.log4j.ConsoleAppender;Importorg.apache.log4j.SimpleLayout;ImportOrg.apache.log4j.Level;ImportOrg.apache.log4j.Logger;//And this in the main function before the rest of your code: Public Static voidmain (String [] args) {simplelayout layout=Newsimplelayout (); Consoleappender Appender=NewConsoleappender (Newsimplelayout ());      Logger.getrootlogger (). Addappender (Appender); Logger.getrootlogger (). SetLevel (level) Level.warn);(...)

5 minutes to this end.

In the next article, I'll delve deeper into the EPL statement, providing some code to connect two engines to achieve what is known as "event refinement" (Refinement) (translator: It seems that the author has never been updated before, so don't expect to follow up:))

Original address: https://coffeeonesugar.wordpress.com/2009/07/21/getting-started-with-esper-in-5-minutes/. Liu Binhua original translation, reproduced please indicate the source

5-minute Open Esper Tour

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.