├── README.md
└── csdn-esper
├── .classpath
├── .gitignore
├── .project
├── .settings
├── org.eclipse.jdt.core.prefs
└── org.eclipse.m2e.core.prefs
├── csdn-esper.iml
├── etc
├── esper.examples.cfg.xml
└── log4j.xml
├── pom.xml
└── src
└── main
└── java
├── example
├── AccessRDBMSTest.java
├── AddArrayEventTypeTest.java
├── AverageBatchListener.java
├── AverageBatchTest.java
├── AverageListener.java
├── AverageTest.java
├── ConfigTest.java
├── ContextListener.java
├── ContextTest.java
├── CreateNamedWindowTest.java
├── DeleteWindowTest.java
├── DropNamedWindowTest.java
├── ErrorCountTest.java
├── EveryDistinctTest.java
├── ExternallyTimeBatchListener.java
├── ExternallyTimeBatchTest.java
├── ForTest.java
├── GroupbyListener.java
├── GroupbyTest.java
├── IncreaseTest.java
├── InsertIntoListener.java
├── InsertIntoTest.java
├── InvocationMethodJava.java
├── InvocationMethodMap.java
├── InvocationMethodTest.java
├── IteratorSQLTest.java
├── IteratorUpdateListener.java
├── IteratorUpdateListenerTest.java
├── JoinContainedEventTest.java
├── JoinTest.java
├── JoinUnidirectionalTest.java
├── LimitFollowedTest.java
├── LogProcessListener.java
├── LogProcessTest.java
├── LogView.java
├── LogViewFactory.java
├── MapProcessListener.java
├── MapProcessTest.java
├── MaxValueTest.java
├── MySQLColumnTypeConvertor.java
├── MySQLOutputRowConvertor.java
├── OnDeleteWindowTest.java
├── OnMergeWindowTest.java
├── OnSelectDeleteWindowTest.java
├── OnSelectWindowTest.java
├── OnSetVariableTest.java
├── OnUpdateWindowTest.java
├── PattenListener.java
├── PatternConsumeTest.java
├── PatternFollowedTest.java
├── PatternTest.java
├── PojoInterfaceEventTypeTest.java
├── PreparedStatementTest.java
├── ReplaylastResultTest.java
├── ResultListener.java
├── SQLColumnTypeConversionTest.java
├── SQLOutputRowConversionTest.java
├── SelectContainedEventTest.java
├── SelectNamedWindowTest.java
├── SelectWindowEventTest.java
├── SelectWindowTest.java
├── SplitDuplicateTest.java
├── StandardGroupWinListener.java
├── StandardGroupWinTest.java
├── StatementStartedOrStoppedTest.java
├── SubqueryAggregationTest.java
├── SubqueryExistsTest.java
├── TimeBatchListener.java
├── TimeBatchTest.java
├── TimeOldEventsListener.java
├── TimeOldEventsTest.java
├── UpdateEventTest.java
├── UpdateWindowTest.java
├── VariableTest.java
├── ViewListener.java
└── model
│ ├── ESB.java
│ ├── Fruit.java
│ ├── Product.java
│ ├── T.java
│ ├── Timer.java
│ ├── User.java
│ └── contained
│ ├── Book.java
│ ├── Books.java
│ ├── Item.java
│ ├── Items.java
│ ├── MediaOrder.java
│ └── Review.java
└── test
├── Test.java
└── TestListener.java
/README.md:
--------------------------------------------------------------------------------
1 | csdn-esper
2 | ==========
3 |
4 | Esper专栏源代码
5 |
6 | 博客地址:http://blog.csdn.net/luonanqin
7 |
--------------------------------------------------------------------------------
/csdn-esper/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/csdn-esper/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | /.DS_Store
3 |
--------------------------------------------------------------------------------
/csdn-esper/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | csdn-esper
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 | org.eclipse.m2e.core.maven2Builder
15 |
16 |
17 |
18 |
19 |
20 | org.eclipse.m2e.core.maven2Nature
21 | org.eclipse.jdt.core.javanature
22 |
23 |
24 |
--------------------------------------------------------------------------------
/csdn-esper/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 | org.eclipse.jdt.core.compiler.compliance=1.6
6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
12 | org.eclipse.jdt.core.compiler.source=1.6
13 |
--------------------------------------------------------------------------------
/csdn-esper/.settings/org.eclipse.m2e.core.prefs:
--------------------------------------------------------------------------------
1 | activeProfiles=
2 | eclipse.preferences.version=1
3 | resolveWorkspaceProjects=true
4 | version=1
5 |
--------------------------------------------------------------------------------
/csdn-esper/csdn-esper.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/csdn-esper/etc/esper.examples.cfg.xml:
--------------------------------------------------------------------------------
1 |
>
--------------------------------------------------------------------------------
/csdn-esper/etc/log4j.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
17 |
18 |
19 |
20 |
21 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/csdn-esper/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | csdn-esper
5 | csdn-esper
6 | 0.0.1-SNAPSHOT
7 |
8 |
9 | log4j
10 | log4j
11 | 1.2.16
12 |
13 |
14 | commons-logging
15 | commons-logging-api
16 | 1.1
17 |
18 |
19 | cglib
20 | cglib-nodep
21 | 2.2
22 |
23 |
24 | org.antlr
25 | antlr-runtime
26 | 3.2
27 |
28 |
29 | junit
30 | junit
31 | 4.8.1
32 |
33 |
34 | mysql
35 | mysql-connector-java
36 | 5.1.8
37 |
38 |
39 | com.espertech
40 | esper
41 | 4.9.0
42 |
43 |
44 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AccessRDBMSTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.Configuration;
4 | import com.espertech.esper.client.EPAdministrator;
5 | import com.espertech.esper.client.EPRuntime;
6 | import com.espertech.esper.client.EPServiceProvider;
7 | import com.espertech.esper.client.EPServiceProviderManager;
8 | import com.espertech.esper.client.EPStatement;
9 | import com.espertech.esper.client.EventBean;
10 | import com.espertech.esper.client.UpdateListener;
11 |
12 | /**
13 | *
14 | *
15 | * @author luonanqin
16 | */
17 |
18 | class Student {
19 | private int sid;
20 | private String name;
21 |
22 | public int getSid() {
23 | return sid;
24 | }
25 |
26 | public void setSid(int sid) {
27 | this.sid = sid;
28 | }
29 |
30 | public String getName() {
31 | return name;
32 | }
33 |
34 | public void setName(String name) {
35 | this.name = name;
36 | }
37 | }
38 |
39 | class StudentListener implements UpdateListener {
40 |
41 | public void update(EventBean[] newEvents, EventBean[] oldEvents) {
42 | if (newEvents != null) {
43 | System.out.println(newEvents.length);
44 | }
45 | }
46 | }
47 |
48 | public class AccessRDBMSTest {
49 |
50 | public static void main(String[] args) throws InterruptedException {
51 | Configuration config = new Configuration();
52 | config.configure("esper.examples.cfg.xml");
53 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider(config);
54 |
55 | EPAdministrator admin = epService.getEPAdministrator();
56 | EPRuntime runtime = epService.getEPRuntime();
57 | String epl1 = "select id, name from " + Student.class.getName() + ",sql:test['select id from test1 where id=${sid}']";
58 | System.out.println(epl1);
59 | EPStatement state1 = admin.createEPL(epl1);
60 | state1.addListener(new StudentListener());
61 |
62 | Student s1 = new Student();
63 | s1.setSid(1);
64 | s1.setName("name");
65 | runtime.sendEvent(s1);
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AddArrayEventTypeTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.ConfigurationOperations;
4 | import com.espertech.esper.client.EPAdministrator;
5 | import com.espertech.esper.client.EPServiceProvider;
6 | import com.espertech.esper.client.EPServiceProviderManager;
7 | import com.espertech.esper.client.EventType;
8 |
9 | import java.util.Arrays;
10 |
11 | /**
12 | * 1.新增数组类EventBean需要输入事件名,包含的属性列表,及列表类型。列表和列表类型一一对应
13 | * 2.更新数组类EventBean为增量更新,只能增加属性,不能删除属性
14 | *
15 | * @author luonanqin
16 | *
17 | */
18 | public class AddArrayEventTypeTest {
19 |
20 | public static void main(String[] args) throws InterruptedException {
21 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
22 |
23 | EPAdministrator admin = epService.getEPAdministrator();
24 | ConfigurationOperations config = admin.getConfiguration();
25 |
26 | config.addEventType("arrayTest", new String[] { "a", "b" }, new Object[] { String.class, int.class });
27 | EventType event = config.getEventType("arrayTest");
28 | System.out.println("Event Names: " + Arrays.asList(event.getPropertyNames()));
29 |
30 | config.updateObjectArrayEventType("arrayTest", new String[] { "c" }, new Object[] { long.class });
31 | event = config.getEventType("arrayTest");
32 | System.out.println("Event Names: " + Arrays.asList(event.getPropertyNames()));
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AverageBatchListener.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.EventBean;
4 | import com.espertech.esper.client.UpdateListener;
5 |
6 | public class AverageBatchListener implements UpdateListener {
7 |
8 | public void update(EventBean[] newEvents, EventBean[] oldEvents) {
9 | if (newEvents != null) {
10 | EventBean event = newEvents[0];
11 | System.out.println("avg price(win:length_batch(2)): " + event.get("avg(price)"));
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AverageBatchTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.EPAdministrator;
4 | import com.espertech.esper.client.EPRuntime;
5 | import com.espertech.esper.client.EPServiceProvider;
6 | import com.espertech.esper.client.EPServiceProviderManager;
7 | import com.espertech.esper.client.EPStatement;
8 | import example.model.Product;
9 |
10 | /**
11 | * avg函数用于计算view里包含的事件的平均值,具体计算的平均值是由avg里的内容决定的
12 | * 以如下view所示,如果为length_batch,则当batch满足数量并触发监听器时,只计算view里的平均price,不会将oldEvent计算在内
13 | * 如果为length,则请参看AverageTest例子
14 | *
15 | * @author luonanqin
16 | *
17 | */
18 | public class AverageBatchTest {
19 |
20 | public static void main(String[] args) throws InterruptedException {
21 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
22 |
23 | EPAdministrator admin = epService.getEPAdministrator();
24 |
25 | String product = Product.class.getName();
26 | String epl1 = "select avg(price) from " + product + ".win:length_batch(2)";
27 |
28 | EPStatement state = admin.createEPL(epl1);
29 | state.addListener(new AverageBatchListener());
30 |
31 | EPRuntime runtime = epService.getEPRuntime();
32 |
33 | Product esb = new Product();
34 | esb.setPrice(1);
35 | esb.setType("esb");
36 | runtime.sendEvent(esb);
37 |
38 | Product eos = new Product();
39 | eos.setPrice(2);
40 | eos.setType("eos");
41 | runtime.sendEvent(eos);
42 |
43 | Product esb1 = new Product();
44 | esb1.setPrice(2);
45 | esb1.setType("esb");
46 | runtime.sendEvent(esb1);
47 |
48 | Product eos1 = new Product();
49 | eos1.setPrice(5);
50 | eos1.setType("eos");
51 | runtime.sendEvent(eos1);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AverageListener.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.EventBean;
4 | import com.espertech.esper.client.UpdateListener;
5 |
6 | public class AverageListener implements UpdateListener {
7 |
8 | public void update(EventBean[] newEvents, EventBean[] oldEvents) {
9 | if (newEvents != null) {
10 | EventBean event = newEvents[0];
11 | System.out.println("avg price(win:length(2)): " + event.get("avg(price)"));
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/AverageTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.EPAdministrator;
4 | import com.espertech.esper.client.EPRuntime;
5 | import com.espertech.esper.client.EPServiceProvider;
6 | import com.espertech.esper.client.EPServiceProviderManager;
7 | import com.espertech.esper.client.EPStatement;
8 | import example.model.Product;
9 |
10 | /**
11 | * avg函数用于计算view里包含的事件的平均值,具体计算的平均值是由avg里的内容决定的
12 | * 以如下view所示,如果为length,则每次事件进入并触发监听器时,只计算view里的平均price,不会将oldEvent计算在内
13 | * 如果为length_batch,则请参看AverageBatchTest例子
14 | *
15 | * @author luonanqin
16 | *
17 | */
18 | public class AverageTest {
19 |
20 | public static void main(String[] args) throws InterruptedException {
21 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
22 |
23 | EPAdministrator admin = epService.getEPAdministrator();
24 |
25 | String product = Product.class.getName();
26 | String epl1 = "select avg(price) from " + product + ".win:length(2)";
27 |
28 | EPStatement state = admin.createEPL(epl1);
29 | state.addListener(new AverageListener());
30 |
31 | EPRuntime runtime = epService.getEPRuntime();
32 |
33 | Product esb = new Product();
34 | esb.setPrice(1);
35 | esb.setType("esb");
36 | runtime.sendEvent(esb);
37 |
38 | Product eos = new Product();
39 | eos.setPrice(2);
40 | eos.setType("eos");
41 | runtime.sendEvent(eos);
42 |
43 | Product esb1 = new Product();
44 | esb1.setPrice(2);
45 | esb1.setType("esb");
46 | runtime.sendEvent(esb1);
47 |
48 | Product eos1 = new Product();
49 | eos1.setPrice(5);
50 | eos1.setType("eos");
51 | runtime.sendEvent(eos1);
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/ConfigTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.io.File;
4 |
5 | import com.espertech.esper.client.Configuration;
6 | import com.espertech.esper.client.ConfigurationOperations;
7 | import com.espertech.esper.client.EPAdministrator;
8 | import com.espertech.esper.client.EPServiceProvider;
9 | import com.espertech.esper.client.EPServiceProviderManager;
10 | import com.espertech.esper.client.EventType;
11 |
12 | import example.model.ESB;
13 | import example.model.Product;
14 |
15 | /**
16 | * ConfigurationOperations对象可以获取配置文件和运行时的EventType等其他配置
17 | *
18 | * @author luonanqin
19 | *
20 | */
21 | public class ConfigTest {
22 |
23 | /**
24 | * @param args
25 | */
26 | public static void main(String[] args) {
27 | Configuration config = new Configuration();
28 | config.configure(new File("etc/esper.examples.cfg.xml"));
29 | EPServiceProvider epService = EPServiceProviderManager.getProvider("default", config);
30 |
31 | EPAdministrator admin = epService.getEPAdministrator();
32 |
33 | String esb = ESB.class.getName();
34 | String context1 = "create context esbtest partition by id from " + esb;
35 | String epl1 = "context esbtest select avg(price) as aPrice from " + esb;
36 |
37 | admin.createEPL(context1, "context1");
38 | admin.createEPL(epl1, "epl1");
39 |
40 | ConfigurationOperations conf = admin.getConfiguration();
41 | conf.addEventType("product", Product.class);
42 |
43 | EventType[] types = conf.getEventTypes();
44 | for (EventType type : types) {
45 | System.out.println("name: " + type.getName() + ", underlyingName: " + type.getUnderlyingType());
46 | }
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/ContextListener.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import com.espertech.esper.client.EventBean;
4 | import com.espertech.esper.client.UpdateListener;
5 |
6 | public class ContextListener implements UpdateListener {
7 |
8 | public void update(EventBean[] newEvents, EventBean[] oldEvents) {
9 | if (newEvents != null) {
10 | EventBean event = newEvents[0];
11 | System.out.println("id: " + event.get("id") + ", avgPrice: " + event.get("aPrice"));
12 | }
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/csdn-esper/src/main/java/example/ContextTest.java:
--------------------------------------------------------------------------------
1 | package example;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Iterator;
5 | import java.util.List;
6 |
7 | import com.espertech.esper.client.EPAdministrator;
8 | import com.espertech.esper.client.EPRuntime;
9 | import com.espertech.esper.client.EPServiceProvider;
10 | import com.espertech.esper.client.EPServiceProviderManager;
11 | import com.espertech.esper.client.EPStatement;
12 | import com.espertech.esper.client.EventBean;
13 | import com.espertech.esper.client.context.ContextPartitionSelectorSegmented;
14 | import example.model.ESB;
15 |
16 | /**
17 | * 1.context的作用是对特定的context定义产生隔离,如例子所示,以不同的id进行隔离,id为1的平均价格只有当id为1的事件
18 | * 进入时才会计算,id为2的价格不参与id为1的平均价格计算
19 | * 2.遍历context中的所有evnet,需要先定义ContextPartitionSelector接口,如44行所示,其中接口的实现是将你要遍历的
20 | * 对应context定义的内容值按照规定格式返回,如getPartitionKeys方法所示
21 | *
22 | * @author luonanqin
23 | *
24 | */
25 | public class ContextTest {
26 |
27 | public static void main(String[] args) throws InterruptedException {
28 | EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
29 |
30 | EPAdministrator admin = epService.getEPAdministrator();
31 |
32 | String esb = ESB.class.getName();
33 | // 创建context
34 | String epl1 = "create context esbtest partition by id from " + esb;
35 | String epl2 = "context esbtest select avg(price) as aPrice, id from " + esb;
36 |
37 | EPStatement context = admin.createEPL(epl1);
38 | EPStatement state = admin.createEPL(epl2);
39 | state.addListener(new ContextListener());
40 |
41 | EPRuntime runtime = epService.getEPRuntime();
42 | // 查看id为1的平均价格
43 | ContextPartitionSelectorSegmented selectCtx = new ContextPartitionSelectorSegmented() {
44 |
45 | // 该方法的实现方式与context定义的properties有关,如果有两个property id和time,则Object数组长度为2,obj[0]为id值,
46 | // obj[1]为time值,然后再添加到list中并返回
47 | public List