實作事件的類可以是一個實作java.util.map接口的類。映射事件的事件屬性是其通過get方法能夠獲得的值。
和數組對象事件類型一樣,映射事件類型考慮了系統中的綜合類型,是的不需要使用java類來描述事件類型,這是的更容易在運作時更改事件,或者從其他類型生成類型資訊。
一個給定的map事件類型可以有一個或者多個超類型,這些超類型也必須是map 事件類型。是以在超類中可以擷取的屬性在該映射事件中也可以獲得。此外,在epl中任何一個map超類被使用,那麼其子類都可以比對該表達式。
應用可以通過configurationoperation(配置操作)updatemapeventtype在運作時為一個已經存在的映射事件類型添加屬性。屬性隻能增加,不能更新或者删除。運作時配置允許删除一個映射事件類型并修改後再添加回系統中。
通過數組可以在映射事件類型中表示一對多的關系。映射事件類型中的一個屬性可以是一個内置類型的數組,java對象的數組,map的數組以及數組的數組,都是可以的。
引擎通過epruntime接口中的sendevent(map map,string eventtypename)方法處理java.util.map事件。映射中的條目代表這屬性。關鍵字必須是java.util.string類型,為了使引擎能夠通過比對名字取查找事件屬性。
引擎并不驗證映射事件屬性名和值。應用必須保證事件屬性比對create schema屬性名和類型。
映射事件屬性可以是任意類型。它可以是java應用對象、java.util.map類型、object[]數組類型等。這些複雜的類型可以提供一下更強的功能:
1) 使用java application object的屬性可以嵌套、索引、映射以及使用動态屬性等。
2) 使用map作為事件屬性可以表達更為複雜的事件,并且該屬性可以被嵌套、索引、動态屬性等。
3) 使用數組對象作為事件屬性也是如此。。。。
為了使用映射事件類型,事件類型名稱和屬性名以及屬性類型等必須讓引擎知道,可以通過configuration或者create schema epl文法實作。具體例子參見5.15節“declaring aneventtype:create schema”和第16.4.2節“eventsrepresed by java.util.map”
下面的代碼定義了一個映射事件類型,穿件一個映射事件并将該事件發送到引擎中。示例中通過運作時配置接口(create schema 和靜态配置也可以)。
// define carlocupdateevent event type(example for runtime-configuration interface)
map<string, object> def = newhashmap<string, object>;
def.put("carid", string.class);
def.put("direction", int.class);
epservice.getepadministrator().getconfiguration().
addeventtype("carlocupdateevent", def);
carlocupdateevent事件類型可以在如下語句中使用:
select carid fromcarlocupdateevent.win:time(1 min) where direction = 1
建立一個carlocupdateevent事件并将它發送到引擎中:
// create a carlocupdateevent event andsend it into the engine for processing
map<string, object> event = newhashmap<string, object>();
event.put("carid", carid);
event.put("direction",direction);
epruntime.sendevent(event,"carlocupdateevent");
通過嵌套屬性,引擎允許把一個對象當做映射事件中的屬性值來查詢。這樣map就可以用來聚合多個資料結構到單個事件中。下面是這樣的一個例子。
map event = new hashmap();
event.put("txn", txn);
event.put("account", account);
epruntime.sendevent(event,"txnevent");
一個示例語句可以如下:
select account.id, account.rate *txn.amount
from txnevent.win:time(60 sec)
group by account.id
你的映射事件類型可以聲明一個或者多個超類。
映射事件的超類也必須是映射事件類型。超類的所有屬性名和類型在子類中都可以擷取,并且是同名覆寫。此外,在epl中使用超類的地方,子類的對象也可以比對該表達式。
下面accountupdate有一個baseupdate超類。
addeventtype("accountupdate", accountupdatedef,
new string[] {"baseupdate"});
下面是epl語句,子類對象都可以比對:
// receive baseupdate and any subtypesincluding subtypes of subtypes
select * from baseupdate
下面通過一個示例說明映射事件的嵌套屬性。
事件類型的定義:
map<string, object> updatedfielddef =new hashmap<string, object>();
updatedfielddef.put("name",string.class);
updatedfielddef.put("addressline1",string.class);
updatedfielddef.put("history",updatehistory.class);
addeventtype("updatedfieldtype", updatedfielddef);
map<string, object> accountupdatedef= new hashmap<string, object>();
accountupdatedef.put("accountid",long.class);
accountupdatedef.put("fields","updatedfieldtype");
// the latter can also be: accountupdatedef.put("fields",updatedfielddef);
addeventtype("accountupdate",accountupdatedef);
定義和發送事件:
map<string, object> updatedfield =new hashmap<string, object>();
updatedfield.put("name","joe doe");
updatedfield.put("addressline1","40 popular street");
updatedfield.put("history", newupdatehistory());
map<string, object> accountupdate =new hashmap<string, object>();
accountupdate.put("accountid",10009901);
accountupdate.put("fields",updatedfield);
epservice.getepruntime().sendevent(accountupdate,"accountupdate");
最後查詢事件:
select accountid, fields.name, fields.addressline1,fields.history.lastupdate
from accountupdate
使用數組作為屬性。
map<string, object> sale = newhashmap<string, object>();
sale.put("userids", int[].class);
sale.put("salespersons",salesperson[].class);
sale.put("items","orderitem[]"); // theproperty type is the name itself appended by []
addeventtype("saleevent", sale);
select userids[0], salespersons[1].name,
items[1], items[1].price.amount from saleevent