先确定理解了Esper的思想下:
**
* Chapter 1. Technology Overview
*
***** 1.关系数据库及其sql设计的目地主要是为了数据相对静态和复杂查询比较少的应用(OLTP,磁盘数据的存取有相应的优化结构,当然内存数据库,数据存放在内存内)。
* 对于cep系统来说,内存数据库更接近其目地。
*
***** 2.cep引擎的实现和关系数据库的实现有点相反,数据库一般把数据存放到磁盘(静态的),我们通过网络把sql流传给数据库引擎,操作数据库,返回结果流,流向应用。
* 而cep则是把查询表达式存储,数据事件流流向其中。其实查询表达式中的窗口函数及表达式等组合起来,实现了一种条件性数据结构(时间范围或符合条件的数据才能流向其中存储)。
*
***** 3.cep提供了两种机制处理事件:事件模式和事件流查询。
根据官方网站给的入门例子:
主代码:
package com.doctor.esper.tutorial; import com.espertech.esper.client.Configuration; import com.espertech.esper.client.EPServiceProvider; import com.espertech.esper.client.EPServiceProviderManager; import com.espertech.esper.client.EPStatement; /** * code for * * @see http://www.espertech.com/esper/quickstart.php * * @author doctor * * @time 2015年5月28日 下午3:51:18 */ public class QuickStart { public static void main(String[] args) { // Configuration // // Esper runs out of the box and no configuration is required. However configuration can help make statements more readable and provides the // opportunity to plug-in extensions and to configure relational database access. // // One useful configuration item specifies Java package names from which to take event classes. // // This snippet of using the configuration API makes the Java package of the OrderEvent class known to an engine instance: // In order to query the OrderEvent events, we can now remove the package name from the statement:see line40 Configuration configuration = new Configuration(); configuration.addEventTypeAutoName("com.doctor.esper.tutorial"); // Creating a Statement // A statement is a continuous query registered with an Esper engine instance that provides results to listeners as new data arrives, in // real-time, or by demand via the iterator (pull) API. // The next code snippet obtains an engine instance and registers a continuous query. The query returns the average price over all OrderEvent // events that arrived in the last 30 seconds: EPServiceProvider epServiceProvider = EPServiceProviderManager.getDefaultProvider(configuration); String expression = "select avg(price) from OrderEvent.win:time(30 sec)"; EPStatement epStatement = epServiceProvider.getEPAdministrator().createEPL(expression); // By attaching the listener to the statement the engine provides the statement's results to the listener: MyListener myListener = new MyListener(); epStatement.addListener(myListener); // Sending events // The runtime API accepts events for processing. As a statement's results change, the engine indicates the new results to listeners right // when the events are processed by the engine. // Sending events is straightforward as well: OrderEvent orderEvent = new OrderEvent("shirt", 75.50D); epServiceProvider.getEPRuntime().sendEvent(orderEvent); } }
定义事件(java普通类形式):
package com.doctor.esper.tutorial; import com.alibaba.fastjson.JSON; /** * Creating a Java Event Class * * Java classes are a good choice for representing events, however Map-based or XML event representations can also be good choices depending on * your * architectural requirements. * * A sample Java class that represents an order event is shown below. A simple plain-old Java class that provides getter-methods for access to * event * properties works best: * * @author doctor * * @time 2015年5月28日 下午3:59:02 */ public class OrderEvent { private String itemName; private double price; public OrderEvent(String itemName, double price) { this.itemName = itemName; this.price = price; } public String getItemName() { return itemName; } public double getPrice() { return price; } @Override public String toString() { return JSON.toJSONString(this); } }
监听器/触发器:
/** * Adding a Listener * * Listeners are invoked by the engine in response to one or more events that change a statement's result set. Listeners implement the UpdateListener * interface and act on EventBean instances as the next code snippet outlines * * @author doctor * * @time 2015年5月28日 下午4:02:37 */ public class MyListener implements UpdateListener { @Override public void update(EventBean[] newEvents, EventBean[] oldEvents) { EventBean eventBean = newEvents[0]; System.out.println("avg = " + eventBean.get("avg(price)")); } }
运行下结果:
06-20 16:03:27.702 main INFO c.e.e.c.s.EPServiceProviderImpl - Initializing engine URI ‘default‘ version 5.2.0
avg = 75.5
时间: 2024-10-05 23:27:26