www.espertech.comDocumentation
This section provides information for using objects that implement the java.util.Map
interface to represent events.
For NEsper .NET also see Section J.9, “.NET IDictionary Events”.
In order to use Map
events, the event type name and property names and types must be made known to the compiler via Configuration or create schema
EPL syntax. Please see examples in Section 5.15, “Declaring an Event Type: Create Schema” and Section 17.4.4, “Events Represented by java.util.Map”.
The code snippet below defines a Map event type, creates a Map event and sends the event into the runtime. The sample defines the CarLocUpdateEvent
event type via configuration (create schema
could have been used instead).
// Define CarLocUpdateEvent event type Map<String, Object> def = new HashMap<String, Object>; def.put("carId", String.class); def.put("direction", int.class); Configuration configuration = new Configuration(); configuration.getCommon().addEventType("CarLocUpdateEvent", def);
The CarLocUpdateEvent
can now be used in a statement:
select carId from CarLocUpdateEvent(direction = 1)#time(1 min)
// Create a CarLocUpdateEvent event and send it into the runtime for processing Map<String, Object> event = new HashMap<String, Object>(); event.put("carId", carId); event.put("direction", direction); runtime.getEventService().sendEventMap(event, "CarLocUpdateEvent");
The runtime can also query Java objects as values in a Map
event via the nested property syntax. Thus Map
events can be used to
aggregate multiple data structures into a single event and query the composite information in a convenient way. The example below demonstrates a Map
event with a transaction and an account object.
Map event = new HashMap(); event.put("txn", txn); event.put("account", account); runtime.getEventService().sendEventMap(event, "TxnEvent");
An example statement could look as follows.
select account.id, account.rate * txn.amount from TxnEvent#time(60 sec) group by account.id
Your Map
event type may declare one or more supertypes when configuring the type.
Configuration configuration = new Configuration(); configuration.getCommon().addEventType("AccountUpdate", accountUpdateDef, new String[] {"BaseUpdate"});
// Receive BaseUpdate and any subtypes including subtypes of subtypes select * from BaseUpdate
See Section 17.4.4, “Events Represented by java.util.Map” for more information on configuring Map event types.
Map<String, Object> updatedFieldDef = new HashMap<String, Object>(); updatedFieldDef.put("name", String.class); updatedFieldDef.put("addressLine1", String.class); updatedFieldDef.put("history", UpdateHistory.class); Configuration configuration = new Configuration(); configuration.getCommon().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); configuration.getCommon().addEventType("AccountUpdate", accountUpdateDef);
The next code snippet populates a sample event and sends the event into the runtime:
Map<String, Object> updatedField = new HashMap<String, Object>(); updatedField.put("name", "Joe Doe"); updatedField.put("addressLine1", "40 Popular Street"); updatedField.put("history", new UpdateHistory()); Map<String, Object> accountUpdate = new HashMap<String, Object>(); accountUpdate.put("accountId", 10009901); accountUpdate.put("fields", updatedField); runtime.getEventService().sendEventMap(accountUpdate, "AccountUpdate");
Last, a sample statement to interrogate AccountUpdate
events is as follows:
select accountId, fields.name, fields.addressLine1, fields.history.lastUpdate from AccountUpdate
Map<String, Object> sale = new HashMap<String, Object>(); sale.put("userids", int[].class); sale.put("salesPersons", SalesPerson[].class); sale.put("items", "OrderItem[]"); // The property type is the name itself appended by [] Configuration configuration = new Configuration(); configuration.getCommon().addEventType("SaleEvent", sale);
The three properties that the above example declares are:
The next statement is a sample statement asking for property values held by arrays:
select userids[0], salesPersons[1].name, items[1], items[1].price.amount from SaleEvent