Esper系列(十一)NamedWindow语法Merge、Queries、Indexing、Dropping

On-Merge With Named Windows

功能:对window中的insert、update、delete操作进行组合运用。

格式:

on event_type[(filter_criteria)] [as stream_name]
merge [into] window_or_table_name [as stream_name]
[where criteria_expression]
  when [not] matched [and search_condition]
    then [
      insert [into streamname]
          [ (property_name [, property_name] [,...]) ] 
          select select_expression [, select_expression[,...]]
          [where filter_expression]
10        |
11        update set mutation_expression [, mutation_expression [,...]]
12            [where filter_expression]
13        |
14        delete
15            [where filter_expression]
16      ]
17      [then [insert|update|delete]] [,then ...]
18    [when ...  then ... [...]] 

参数说明:
1、where子句是将事件分为了matched(满足where条件)和not matched(不满足where条件)两类;
2、when子句配合matched或者not matched表示“window中满足where条件的事件,执行下面的操作/window中不满足where条件的事件,执行下面的操作”。search_condition为可选字段,表示再次过滤matched或not matched中的事件,只有没被过滤掉的事件才可以被then后面的语句操作;
3、insert子句和之前说的insert into不太一样。虽然都表示插入事件,但是由于into streamname是可选,所以在只有insert关键字的情况下,会将触发的事件插入到当前的named window中。如果要指明插入到别的named window中就要在insert之后带上into及window的名字。再之后的圆括号中的内容表示要插入的事件的属性,一般情况是在将事件插入到别的window中时,用它来重命名select中列出的属性;
4、select子句是配合insert一起使用的。select子句不可少,不然引擎就不知道要往window中插入什么内容了。select的内容可以是*,也可以是属性列表。where语句再一次限制可插入的触发事件。注意select后面没有from,因为事件来源就是当时的触发事件;
5、update子句用于更新符合条件的事件,可更新单个或多个属性,where条件判断是否可进行更新操作;
6、delete子句用于删除符合条件的事件,只包含关键字delete以及可选的where语句;
7、最后两行表示on merge中可以有多个when,每个when可以有多个then及insert或updata或delete语句,这样就能组成一个非常复杂的merge操作。

注意:
1、when not matched后面只能跟insert语句,而when matched则没有任何限制;
2、select语句中不能使用聚合函数;
3、当某一个when matched或者no matched和其search_condition满足,则这个when下的所有then都会执行,并且按照顺序执行;
4、避免在一个when中同时使用update和delete,esper不能保证两个都有效执行。

Using Fire-And-Forget Queries with Named Windows

以上的window操作,都需要在发送事件后进行触发,这个里给出一种不发送事件就能触发select/update/delete,且不需要设置监听就能得到结果的语法。

查询操作的格式:

select *[, property_name[,...]] from window_name [where criteria_expression] [oder by] [having] [subquery]

修改操作的格式:

update window_or_table_name [as stream_name]
set mutation_expression [, mutation_expression [,...]]
[where criteria_expression]

注:子查询,聚合函数等不能用于expression

删除操作的格式:

delete from window_or_table_name [as stream_name]
    [where criteria_expression]

例子:

// 创建window
String nwsql = "create window myWindow.win:keepall() as orderBean";
EPStatement nwstate = epAdmin.createEPL(nwsql);
// 向window中插入数据
String insql = "insert into myWindow select * from orderBean";
EPStatement instate = epAdmin.createEPL(insql);
EPRuntime epRuntime = epServiceProvider.getEPRuntime();
 
//发送orderBean事件到引擎
10  …………………………………………………………………
11  String select = "select * from myWindow";
12  String update = "update myWindow set key=\"chenx\" where value=111"; 
13  String delete = "delete from myWindow where value = 110";
14     
15  System.out.println("start Name Window select");
16  EPOnDemandQueryResult sResult=epRuntime.executeQuery(select);
17  EventBean[] arryBeans =sResult.getArray();
18  for (EventBean bean:arryBeans){
19      System.out.println("select:"+bean.getUnderlying());
20  }
21         
22  System.out.println("start Name Window update");
23  EPOnDemandQueryResult uResult=epRuntime.executeQuery(update);
24  arryBeans =uResult.getArray();
25  for (EventBean bean:arryBeans){
26      System.out.println("select:"+bean.getUnderlying());
27  }
28         
29  System.out.println("start Name Window delete");
30  EPOnDemandQueryResult dResult=epRuntime.executeQuery(delete);
31  arryBeans =dResult.getArray();
32  for (EventBean bean:arryBeans){
33      System.out.println("select:"+bean.getUnderlying());
34  }

 

Explicitly Indexing Named Windows

功能:对Named Window中存放的事件属性建立索引。

格式:

create [unique] index index_name on window_or_table_name (property [hash|btree] 
    [, property] [hash|btree] [,...] )

格式说明:
1、Unique:表示建立唯一索引,如果插入了重复的行,则会抛出异常并阻止重复行插入。如果不使用此关键字,则表示可以插入重复行;
2、Index_name:索引名称;
3、后面的括号中包含named window中的属性以及索引类型。索引类型分两种,hash索引不会排序,如果有=操作,建议使用此类型索引。btree索引基于排序二叉树,适合<, >, >=, <=, between, in等操作。如果不显式声明hash或者btree,则默认为hash索引;

Dropping or Removing Named Windows

功能:

注销named window,方式是直接调用EPStatement对象的destroy方法。虽然注销,但是named window的名字仍然被占用着,所以你只能重新建立和之前的named window一样结构的window,否则会抛出异常

示例子:

// Create DropWindow  
create window DropWindow.win:keepall() as select * from DropEvent  
  
// Destroy DropWindow  
EPStatement state = admin.createEPL("create window DropWindow.win:keepall() as select * from DropEvent");  
state.destroy();  
  
// Create DropEvent again(different with prior epl)  
create window DropWindow.win:keepall() as select name from DropEvent  
10  // throw Exception  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值