功能:该语法是针对所查询事件中的属性又是另一种属性的查询结果控制。
格式:
1 | [select select_expressions from] |
2 | contained_expression [@type(eventtype_name)] [as alias_name] |
3 | [where filter_expression] |
说明:
该语法的重点在于contained_expression,这个表达式需要返回事件的属性,该属性可以是另一个事件也可以是普通数据类型,也可以是一组查询结果,比如一个数组,一个集合等等;
示例:
文件名:orderEvent.java
1 | public class orderEvent { |
2 | //基本数据类型 |
3 | private String name; |
4 | private int salary; |
5 | private orderArray orderAry; |
6 | |
7 | public orderArray getOrderAry() { |
8 | return orderAry; |
9 | } |
10 | public void setOrderAry(orderArray orderAry) { |
11 | this.orderAry = orderAry; |
12 | } |
13 | .......................... |
14 | } |
文件名:orderArray
1 | public class orderArray { |
2 | private List<orderBean> orderBeans; |
3 | |
4 | public List<orderBean> getOrderBeans() { |
5 | return orderBeans; |
6 | } |
7 | |
8 | public void setOrderBeans(List<orderBean> orderBeans) { |
9 | this.orderBeans = orderBeans; |
10 | } |
11 | |
12 | @Override |
13 | public String toString() { |
14 | return "orderArray [orderBeans=" + orderBeans + "]"; |
15 | } |
16 | } |
文件名: orderBean.java
1 | public class orderBean { |
2 | private String key; |
3 | private String value; |
4 | private orderItem bean; |
5 | |
6 | // 各个字段对应的get和set方法 |
7 | //............................. |
8 | |
9 | @Override |
10 | public String toString() { |
11 | return "orderBean [key=" + key + ", value=" + value + ", bean=" + bean |
12 | + "]"; |
13 | } |
14 | } |
文件名: orderItem.java
1 | public class orderItem { |
2 | private String name; |
3 | private String size; |
4 | |
5 | // 各属性字段对应的get和set方法 |
6 | //................................. |
7 | |
8 | @Override |
9 | public String toString() { |
10 | return "orderItem [name=" + name + ", size=" + size + "]"; |
11 | } |
12 | } |
文件名: orderMainTest(程序入口)
1 | int i = 8; |
2 | int seed = (int) (Math.random() * 100); |
3 | orderEvent event = new orderEvent("张", 100 + seed); |
4 | System.out.println("seed name:" + event.getName() + ",salary:" |
5 | + event.getSalary()); |
6 | orderBean bean = new orderBean(); |
7 | bean.setKey("BeanKey:" + i); |
8 | bean.setValue("BeanValue:" + i); |
9 | event.setBean(bean); |
10 | |
11 | List<orderBean> list = new ArrayList<orderBean>(); |
12 | for (int j = 0; j < 10; j++) { |
13 | bean = new orderBean(); |
14 | bean.setKey("ListKey:" + j); |
15 | bean.setValue("ListValue:" + j); |
16 | orderItem item = new orderItem(); |
17 | item.setName("chenx"); |
18 | item.setSize("100"+j); |
19 | bean.setBean(item); |
20 | list.add(bean); |
21 | } |
22 | |
23 | orderArray ary = new orderArray(); |
24 | ary.setOrderBeans(list); |
25 | event.setOrderAry(ary); |
26 | event.setOrderBeans(list); |
27 | |
28 | String epsql = "select * from orderEvent[orderAry.orderBeans][bean]"; |
29 | EPStatement epstate = epAdmin.createEPL(epsql); |
30 | epstate.addListener(new orderListener()); |
31 | |
32 | epRuntime.sendEvent(event); |
说明:
1、epl执行语句为 select * from orderEvent[orderAry.orderBeans] 表示查询orderEvent事件中orderAry事件属性字段中的orderBeans 事件,因为orderEvent与orderAry、orderAry与orderBeans都是有一种嵌套关系,就像类中有类一样是具有层次的;
2、epl执行语句为 select * from orderEvent[orderAry.orderBeans][bean] 表示只查询orderItem事件信息(bean为orderItem类型), 因为orderAry.orderBeans返回的是orderBean类型的数组,而orderItem又是orderBean的属性成员(bean与orderAry.orderBeans不在一个层面上),所以[orderAry.orderBeans][bean]表示的返回orderItem事件;
3、返回结果的数据格式,由各事件类的toString函数定义;
4、Contained-Event在join查询中,如果事件来自于named window,那么连接操作就必须加上unidirectional关键字,作用为每次连接操作的结果都独立起来;
示例:
1 | String join5 = "select count(*) from " + mediaOrder + "[books.book] as book unidirectional, " + mediaOrder |
2 | + "[items.item] as item where productId = bookId"; |
3 | String join4 = "select count(*) from " + mediaOrder + "[books.book] as book, " + mediaOrder |
4 | + "[items.item] as item where productId = bookId"; |
说明:
Join4中当发送两个同样的mediaOrder对象,聚合函数count是要累加,而join5不会;