复杂事件处理引擎—Esper参考(事件部分)

声明:Esper官方未提供中文文档,以后更新的大部分内容,均来自官方文档。本人英语小白一枚,翻译内容仅供参考。有些翻译确实不忍直视,君可略过。

(有人可能会说,翻译的不好不如不翻,可能会误人子弟;不过我认为,在学习的过程中,尤其是初期可以用“大概”来形容掌握的知识程度,在以后的实践中详加琢磨,可深入理解,并纠正过去的错误认知。简翻或者误翻,如果少量,我感觉在初期的学习中可以接受。个人理解,轻拍!!)

 

第一章以及第二章的部分内容,网络上已经有人进行了翻译,不再赘述。链接如下:

http://blog.sina.com.cn/s/blog_4a5b4b930100xxn1.html   (任我飞的新浪微博

关于第二章,补充如下内容

 

对象数组事件

事件在通过对象数组表示时,其事件属性就是该数组的元素值。属性可以使一个基本类型(数组)、java对象、map或者是另一个对象数组。

和Map事件类似,对象数组事件也可以消除java对象事件的使用,从而更容易地在运行时对事件类型等进行变更。比如,添加一个属性对于Map或者对象数组来说可以很方便的完成,但是对于java对象来说,需要对POJO进行重新编写,在属性扩展上,Map和对象数组有很大的优势。

在运行期间,通过运行配置API ConfigurationOperations对对象数组事件类型进行属性添加。注意,不能对事件类型的进行属性的更新和删除,只能做属性新增操作。另外,运行时配置API也允许清除对象数组事件类型,并且添加新的事件类型信息。

发送对象数组事件:通过运行API EPRuntime , 调用sendEvent(Object[] array, String eventTypeName),完成对象数组的发送。

发送数据时,Esper 不会对数据的长度、值类型进行校验。程序中必须确保发送事件的数组长度、属性值类型以及属性的顺序 与声明的对象数组事件类型一致。

 

对象数组属性

对象数组属性可以任意的类型。比如 java对象、Map、对象数组等。

为了使用对象数组事件,其类型名称、属性名称和属性类型可以通过Configuration 或者 create schema语法进行定义。

下面的代码定义了对象数组事件类型,创建了一个对象数组事件,并且发送该事件到Esper引擎。如下:

 

// Define  CarLocUpdateEvent event type (example for runtime-configuration interface)
String[] propertyNames = {"carId", "direction"};   // order is important
Object[] propertyTypes = {String.class, int.class};  // type order matches name order
epService.getEPAdministrator().getConfiguration().
  addEventType("CarLocUpdateEvent", propertyNames, propertyTypes);

 

Statement中对事件的处理:

 

select carId from CarLocUpdateEvent.win:time(1 min) where direction = 1

 

发送事件时:

// Send an event
Object[] event = {carId, direction};
epRuntime.sendEvent(event, "CarLocUpdateEvent");

Esper引擎也可以查询Object[]中的java对象。Account是定义的java应用对象。

 

epRuntime.sendEvent(new Object[] {txn, account}, "TxnEvent");

//EPL
select account.id, account.rate * txn.amount 
from TxnEvent.win:time(60 sec) 
group by account.id

 

 

高级对象数组类型属性应用

属性的嵌套:

String[] propertyNamesUpdField = {"name", "addressLine1", "history"};
Object[]  propertyTypesUpdField  =  {String.class,  String.class,
 UpdateHistory.class};
epService.getEPAdministrator().getConfiguration().
        addEventType("UpdatedFieldType",  propertyNamesUpdField,
 propertyTypesUpdField);
String[] propertyNamesAccountUpdate = {"accountId", "fields"};
Object[] propertyTypesAccountUpdate = {long.class, "UpdatedFieldType"};
epService.getEPAdministrator().getConfiguration().
        addEventType("AccountUpdate",  propertyNamesAccountUpdate,
 propertyTypesAccountUpdate);

发送事件:

Object[] updatedField = {"Joe Doe", "40 Popular Street", new UpdateHistory()};
Object[] accountUpdate = {10009901, updatedField};
epService.getEPRuntime().sendEvent(accountUpdate, "AccountUpdate");

EPL:

select accountId, fields.name, fields.addressLine1, fields.history.lastUpdate
from AccountUpdate

一对多的关系

下面的例子中salesPersons属性均对应一个数组:

String[] propertyNames = {"userids", "salesPersons", "items"};
Object[] propertyTypes = {int[].class, SalesPerson[].class, "OrderItem[]");
epService.getEPAdministrator().getConfiguration().
    addEventType("SaleEvent", propertyNames, propertyTypes);
select userids[0], salesPersons[1].name, 
    items[1], items[1].price.amount from SaleEvent

 

另,各种事件类型的比较如下:

 

Java Object (POJO/Bean or other)

Map

XML Document

Performance

Good

Good

Not comparable and depending on use of XPath

Memory Use

Good

Good

Depends on DOM and XPath implementation used

Call Method on Event

Yes

Yes, if contains Object(s)

No

Nested, Indexed, Mapped and Dynamic Properties

Yes

Yes

Yes

Course-grained event syntax

Yes

Yes

Yes

Insert-into that Representation

Yes

Yes

No

Runtime Type Change

Reload class, yes

Yes

Yes

Create-schema Syntax

Yes

Yes

No, runtime and static configuration

从上面看出,用java对象作为事件类型表示方式,在性能、内存使用等都有不错的表现,而且支持的功能特性也较为全面。在一般的开发中,建议使用 java对象作为事件类型。

 

posted @ 2013-09-03 23:26  A.King  阅读(2539)  评论(1编辑  收藏  举报