sper是一个Java开发的事件流处理(ESP:Event Stream Processing)和复杂事件处理(CEP:Complex Event Processing)引擎。CEP:是一种实时事件处理并从大量事件数据流中挖掘复杂模式的技术。ESP:是一种从大量事件数据流中过滤,分析有意义的事件,并能够实时取得这些有意义的信息的技术。该引擎可应用于网络入侵探测,SLA监测,RFID读取,航空运输调控,金融方面(风险管理,欺诈探测)等领域。
在esper官网的Quick Start中有一个简单的例子,但是并没有说明如何在eclipse中建立该工程。同时,发现quick start中的例子在新版本esper-4.9.0中有一丁点的问题。我下面将说明如何建立esper_demo的工程,并做简单的解释。
一、建立工程导入依赖包
在eclipse先建立一个java project,然后导入5个必要的依赖jar:esper-4.9.0.jar、antlr-runtime-3.2.jar、cglib-nodep-2.2.jar、commons-logging-1.1.1.jar、log4j-1.2.16.jar,这个几个依赖包都在官网下载来的包中有。
二、代码编写
1、创建Event Class
根据官网上的例子,先在src建立一个package:org.myapp.event,然后在包下新建一个OrderEvent类。
2
3 public class OrderEvent {
4 private String itemName;
5 private double price;
6
7 public OrderEvent(String itemName, double price) {
8 this.itemName = itemName;
9 this.price = price;
10 }
11
12 public String getItemName() {
13 return itemName;
14 }
15
16 public double getPrice() {
17 return price;
18 }
19 }
2、建立MyListener类,添加一个Listner
2
3 import com.espertech.esper.client.EventBean;
4 import com.espertech.esper.client.UpdateListener;
5
6 public class MyListener implements UpdateListener {
7 public void update(EventBean[] newEvents, EventBean[] oldEvents) {
8 EventBean event = newEvents[0];
9 System.out.println("avg=" + event.get("avg(price)"));
10 }
11 }
3、建立一个main类
2
3 import com.espertech.esper.client.Configuration;
4 import com.espertech.esper.client.EPServiceProvider;
5 import com.espertech.esper.client.EPServiceProviderManager;
6 import com.espertech.esper.client.EPStatement;
7
8 public class test {
9
10 public static void main(String[] args) {
11
12 // EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
13 // String expression = "select avg(price) from org.myapp.event.OrderEvent.win:time(30 sec)";
14 // EPStatement statement = epService.getEPAdministrator().createEPL(expression);
15
16 Configuration config = new Configuration();
17 config.addEventTypeAutoName("org.myapp.event");
18 EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(config);
19
20 String epl = "select avg(price) from OrderEvent.win:time(30 sec)";
21 EPStatement statement = epService.getEPAdministrator().createEPL(epl);
22
23 MyListener listener = new MyListener();
24 statement.addListener(listener);
25
26 OrderEvent event = new OrderEvent("shirt", 74.50);
27 epService.getEPRuntime().sendEvent(event);
28
29 }
30
31 }
17行,在官网中应该是config.addEventTypeAutoAlias("org.myapp.event");但是,在ec
1. Stop database and CRS on both node
$ srvctl stop database -d mydb
# crsctl stop crs
2. Turn Off RAC
SQL> startup
ORA-29702 error occurred in Cluster Group Service operation
Relink with the RAC OFF.
$ cd $ORACLE_HOME/rdbms/lib
$ /usr/ccs/bin/make -f ins_rdbms.mk rac_off
Relinking oracle
$ make -f ins_rdbms.mk ioracle
## OR , both working fine
$ cd $ORACLE_HOME/bin
$ relink oracle
If ASM Instance Exist, run below command as root
# /oracle/product/10.2.0/db/bin/localconfig delete
# /oracle/product/10.2.0/db/bin/localconfig add
3. Parameter(Pfile/spfile) & database changes
SQL> startup
SQL> alter database disable thread 2;
SQL> alter system set remote_listener='';
3a. Remove unwanted logfile
SQL> select thread#, group# from v$log;
SQL> alter database drop logfile group 3;
SQL> alter database drop logfile group 4;
3b. Remove unwanted tablespace
SQL> drop tablespace UNDOTBS2 including contents and datafiles;
3c. Rename instance name.
SQL> alter system set instance_name=<new_name> scope=spfile;
SQL> shutdown immediate
SQL> startup
- Change your ORACLE_SID environment
4. Run $ORA_CRS_HOME/install/rootdelete.sh on both node
- This will stop and remove all CRS startup related file
5. Remove $ORA_CRS_HOME binary using Clusterware OUI installer
- Ignore any error if 2nd node already down
- rm -rf $ORA_CRS_HOME
6. Modify listener file
$ vi $ORACLE_HOME/network/admin/listener.ora
6a. Modify tnsname file
$ vi $ORACLE_HOME/network/admin/tnsnames.ora
应该还要把初始化参数中的cluster_database改成false
redis支持有序集合,即sorted set。sorted set在set的基础上,增加了排序属性,是set的升级版。这里简要谈谈sorted set的常用函数:
1)insert
a) zadd
语法:zadd key score member [[score member] [score member] ...]
解释:增加一个或多个member【根据score排序】到有序集key中,如果member已经存在,只更新score。返回增加member个数,不包含已经存在的member
[root@xsf001 ~]# redis-cli redis 127.0.0.1:6379> ZADD score 230 zhangsan (integer) 1 redis 127.0.0.1:6379> zadd score 240 lisi 250 wangwu (integer) 2 redis 127.0.0.1:6379> zadd score 100 liuli redis 127.0.0.1:6379> zadd score 249 wangwu (integer) 0
注意:有些redis版本,只能一次添加一个member
2)select
a)zrange
语法:zrange key start stop [withscores]
解释:返回有续集key中指定范围【通过索引 start stop】的member[及其score];
redis 127.0.0.1:6379> zrange score 0 -1 #所有元素 1) "liuli" 2) "zhangsan" 3) "lisi" 4) "wangwu" redis 127.0.0.1:6379> zrange score 0 -1 withscores #所有member及score 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" 7) "wangwu" 8) "249" redis 127.0.0.1:6379> zrange score 0 2 withscores #前三个元素 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" redis 127.0.0.1:6379> zrange score 0 -2 #第一个元素到 1) "liuli" 2) "zhangsan" 3) "lisi"
注意:0,表示第一个元素,-1 表示最后一个元素,-2 表示倒数第二个元素
b)zcount
语法:zcount key min max
解释:统计key中score值介于min 和max之间的member个数
redis 127.0.0.1:6379> zrange score 0 -1 withscores 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" 7) "wangwu" 8) "249" redis 127.0.0.1:6379> zcount score 230 240 # count(score >=230 && score <=240) (integer) 2c) zscore
语法:zscore key member
解释:返回有续集key中member的score
redis 127.0.0.1:6379> zscore score liuli "100"
d)zrevrange
语法:zrevrange key start stop [withscores]
解释:返回有续集key中指定范围[通过索引start stop]的member[及score],返回member根据score按降序排列
redis 127.0.0.1:6379> zrevrange score 0 -1 withscores 1) "wangwu" 2) "249" 3) "lisi" 4) "240" 5) "zhangsan" 6) "230" 7) "liuli" 8) "100" redis 127.0.0.1:6379> zrevrange score 1 -2 1) "lisi" 2) "zhangsan"
注意:索引0 表示第一个元素,-1 最后一个元素,-2 倒数第二个元素;依次类推
e)zrangebyscore
语法:zrangebyscore key min max [withscores] [limit offset count]
解释:返回有续集key中,score大于等于min并且小于等于max的member。返回结果按照score递增的次序排列。可选withscores决定返回结果集中仅仅返回member还是返回member和score;可选参数limit 指定返回结果的数量区间。
redis 127.0.0.1:6379> zrangebyscore score 100 10000 withscores 1) "liuli" 2) "100" 3) "zhangsan" 4) "230" 5) "lisi" 6) "240" 7) "wangwu" 8) "249" redis 127.0.0.1:6379> zrangebyscore score 100 10000 limit 1 3 1) "zhangsan" 2) "lisi" 3) "wangwu"
f)zrevrangebyscore
语法:zrevrangescore key max min [withscores] [limit offset count]
解释:返回有续集key中score<=max并且score>=min 的元素,返回结果根据score从大到小顺序排列。可选参数withscores决定结果集中是否包含score,可选参数limit 指定返回结果集范围。
redis 127.0.0.1:6379> zrevrangebyscore score 0 10000 (empty list or set) redis 127.0.0.1:6379> zrevrangebyscore score 10000 0 1) "wangwu" 2) "lisi" 3) "zhangsan" 4) "liuli" redis 127.0.0.1:6379> zrevrangebyscore score 10000 0 withscores limit 0, 2 1) "wangwu" 2) "249" 3) "lisi" 4) "240"
注意:max在min之前
g)zrank
语法:zrank key member
解释:根据score从低到高,返回member在有续集中的index
redis 127.0.0.1:6379> zrange score 0 -1 1) "liuli" 2) "zhangsan" 3) "lisi" 4) "wangwu" redis 127.0.0.1:6379> zrank score liuli (integer) 0 redis 127.0.0.1:6379> zrank score wangwu (integer) 3
h)zrevrank
语法:zrevrank key member
解释:根据score从高到低排序,返回member在有序集key中的index
redis 127.0.0.1:6379> zrange score 0 -1 1) "liuli" 2) "zhangsan" 3) "lisi" 4) "wangwu" redis 127.0.0.1:6379> zrevrank score liuli (integer) 3 redis 127.0.0.1:6379> zrevrank score wangwu (integer) 0i)zcard
语法:zcard key
解释:返回有续集key的基数
redis 127.0.0.1:6379> zcard score (integer) 4 redis 127.0.0.1:6379> zcard stdu #有续集不存在返回0 (integer) 0
3)update
a)zincrby
语法:zincrby key increment member
解释:有续集key的member增加增量increment,返回增加后的score
redis 127.0.0.1:6379> zscore score liuli "100" redis 127.0.0.1:6379> zincrby score 300 liuli "400" redis 127.0.0.1:6379> zscore score liuli "400" redis 127.0.0.1:6379> zincrby score 500 xie "500"
注意:如果member在key中不存在,则新增个member
4)delete
a)zrem
语法:zrem key member [member ...]
解释:移除有续集中的一个或多个member,返回移除member的个数
redis 127.0.0.1:6379> zrange score 0 -1 1) "zhangsan" 2) "lisi" 3) "wangwu" 4) "liuli" 5) "xie" redis 127.0.0.1:6379> zrem score xie wwww #www不存在 (integer) 1 redis 127.0.0.1:6379> zrange score 0 -1 1) "zhangsan" 2) "lisi" 3) "wangwu" 4) "liuli"
注意:如果member不存在,则忽略
b)zremrangebyrank
语法:zremrangebyrank key start stop
解释:移除有续集中指定排名范围【start stop】的元素,返回移除元素个数
redis 127.0.0.1:6379> zrange score 0 -1 1) "zhangsan" 2) "lisi" 3) "wangwu" 4) "liuli" redis 127.0.0.1:6379> zremrangebyrank score 0 1 (integer) 2 redis 127.0.0.1:6379> zrange score 0 -1 1) "wangwu" 2) "liuli"
注意:如果stop在start之前,则移除0个
c)zremrangebyscore