複雜事件處理引擎—Esper參考(事件部分)
宣告:Esper官方未提供中文文件,以後更新的大部分內容,均來自官方文件。本人英語小白一枚,翻譯內容僅供參考。有些翻譯確實不忍直視,君可略過。
(有人可能會說,翻譯的不好不如不翻,可能會誤人子弟;不過我認為,在學習的過程中,尤其是初期可以用“大概”來形容掌握的知識程度,在以後的實踐中詳加琢磨,可深入理解,並糾正過去的錯誤認知。簡翻或者誤翻,如果少量,我感覺在初期的學習中可以接受。個人理解,輕拍!!)
第一章以及第二章的部分內容,網路上已經有人進行了翻譯,不再贅述。連結如下:
關於第二章,補充如下內容
物件陣列事件
事件在通過物件陣列表示時,其事件屬性就是該陣列的元素值。屬性可以使一個基本型別(陣列)、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物件作為事件型別。