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 H.8, “.NET IDictionary Events”.
Events can also be represented by objects that implement the java.util.Map
interface.
Event properties of Map
events are the values in the map accessible through the get
method exposed by the java.util.Map
interface.
Similar to the Object-array event type, the Map event type takes part in the comprehensive type system that can eliminate the need to use Java classes as event types, thereby making it easier to change types at runtime or generate type information from another source.
A given Map event type can have one or more supertypes that must also be Map event types. All properties available on any of the Map supertypes are available on the type itself. In addition, anywhere within EPL that an event type name of a Map supertype is used, any of its Map subtypes and their subtypes match that expression.
Your application can add properties to an existing Map event type during runtime using the configuration operation updateMapEventType
. Properties may not be updated or deleted - properties can only be added, and nested properties can be added as well. The runtime configuration also allows removing Map event types and adding them back with new type information.
After your application configures a Map event type by providing a type name, the type name can be used when defining further Map or Object-array event types by specifying the type name as a property type or an array property type.
One-to-Many relationships in Map event types are represented via arrays. A property in a Map event type may be an array of primitive, an array of Java object, an array of Map or an an array of Object-array.
The engine can process java.util.Map
events via the sendEvent(Map map, String eventTypeName)
method on the EPRuntime
interface. Entries in the Map represent event properties. Keys must be of type java.util.String
for the engine to be able to look up event property names specified by pattern or EPL statements.
The engine does not validate Map event property names or values. Your application should ensure that objects passed in as event properties match the create schema
property names and types, or the configured event type information when using runtime or static configuration.
Map event properties can be of any type. Map event properties that are Java application objects or that are of type java.util.Map
(or arrays thereof) or that are of type Object[]
(object-array) (or arrays thereof) offer additional power:
Properties that are Java application objects can be queried via the nested, indexed, mapped and dynamic property syntax as outlined earlier.
Properties that are of type Map
allow Maps to be nested arbitrarily deep and thus can be used to represent complex domain information. The nested, indexed, mapped and dynamic property syntax can be used to query Maps within Maps and arrays of Maps within Maps.
Properties that are of type Object[]
(object-array) allow object-arrays to be nested arbitrarily deep. The nested, indexed, mapped and dynamic property syntax can be used to query nested Maps and object-arrays alike.
In order to use Map
events, the event type name and property names and types must be made known to the engine via Configuration or create schema
EPL syntax. Please see examples in Section 5.15, “Declaring an Event Type: Create Schema” and Section 17.4.2, “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 engine. The sample defines the CarLocUpdateEvent
event type via runtime configuration interface (create schema
or static configuration could have been used instead).
// Define CarLocUpdateEvent event type (example for runtime-configuration interface) Map<String, Object> def = new HashMap<String, Object>; def.put("carId", String.class); def.put("direction", int.class); epService.getEPAdministrator().getConfiguration(). 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 engine for processing Map<String, Object> event = new HashMap<String, Object>(); event.put("carId", carId); event.put("direction", direction); epRuntime.sendEvent(event, "CarLocUpdateEvent");
The engine 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); epRuntime.sendEvent(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 at engine initialization time or at runtime through the administrative interface.
Supertypes of a Map
event type must also be Map event types. All property names and types of a supertype are also available on a subtype and override such same-name properties of the subtype. In addition, anywhere within EPL that an event type name of a Map supertype is used, any of its Map subtypes also matches that expression (similar to the concept of interface in Java).
This example assumes that the BaseUpdate
event type has been declared and acts as a supertype to the AccountUpdate
event type (both Map event types):
epService.getEPAdministrator().getConfiguration(). addEventType("AccountUpdate", accountUpdateDef, new String[] {"BaseUpdate"});
Your application EPL statements may select BaseUpdate
events and receive both BaseUpdate
and AccountUpdate
events, as well as any other subtypes of BaseUpdate
and their subtypes.
// Receive BaseUpdate and any subtypes including subtypes of subtypes select * from BaseUpdate
Your application Map event type may have multiple supertypes. The multiple inheritance hierarchy between Maps can be arbitrarily deep, however cyclic dependencies are not allowed. If using runtime configuration, supertypes must exist before a subtype to a supertype can be added.
See Section 17.4.2, “Events represented by java.util.Map” for more information on configuring Map event types.
Strongly-typed nested Map
-within-Map
events can be used to build rich, type-safe event types on the fly. Use the addEventType
method on Configuration
or ConfigurationOperations
for initialization-time and runtime-time type definition, or the create schema
EPL syntax.
Noteworthy points are:
JavaBean (POJO) objects can appear as properties in Map
event types.
One may represent Map-within-Map and Map-Array within Map (same for object-array) using the name of a previously registered Map (or object-array) event type.
There is no limit to the number of nesting levels.
Dynamic properties can be used to query Map
-within-Map
keys that may not be known in advance.
The engine returns a null
value for properties for which the access path into the nested structure cannot be followed where map entries do not exist.
For demonstration, in this example our top-level event type is an AccountUpdate
event, which has an UpdatedFieldType
structure as a property. Inside the UpdatedFieldType
structure the example defines various fields, as well as a property by name 'history' that holds a JavaBean class UpdateHistory
to represent the update history for the account. The code snippet to define the event type is thus:
Map<String, Object> updatedFieldDef = new HashMap<String, Object>(); updatedFieldDef.put("name", String.class); updatedFieldDef.put("addressLine1", String.class); updatedFieldDef.put("history", UpdateHistory.class); epService.getEPAdministrator().getConfiguration(). 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); epService.getEPAdministrator().getConfiguration(). addEventType("AccountUpdate", accountUpdateDef);
The next code snippet populates a sample event and sends the event into the engine:
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); epService.getEPRuntime().sendEvent(accountUpdate, "AccountUpdate");
Last, a sample query to interrogate AccountUpdate
events is as follows:
select accountId, fields.name, fields.addressLine1, fields.history.lastUpdate from AccountUpdate
To model repeated properties within a Map, you may use arrays as properties in a Map. You may use an array of primitive types or an array of JavaBean objects or an array of a previously declared Map or object-array event type.
When using a previously declared Map event type as an array property, the literal []
must be appended after the event type name.
This following example defines a Map event type by name Sale
to hold array properties of the various types. It assumes a SalesPerson
Java class exists and a Map event type by name OrderItem
was declared:
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 [] epService.getEPAdministrator().getConfiguration(). addEventType("SaleEvent", sale);
The three properties that the above example declares are:
An integer array of user ids.
An array of SalesPerson
Java objects.
An array of Maps for order items.
The next EPL statement is a sample query asking for property values held by arrays:
select userids[0], salesPersons[1].name, items[1], items[1].price.amount from SaleEvent