我正在和Esper一起玩,学习如何使用更高级的概念。
我有一个程序可以触发3种不同股票的模拟股票事件。我目前有一个具有match_recognize模式EPL的模块,如下所示:
module queries;
import events.*;
import configDemo.*;
import annotations.*;
create schema MyTickEvent as TickEvent;
@Name('compareStocks')
@Description('Compare the difference amount between two stocks')
@Subscriber(className='configDemo.MySubscriber')
select * from TickEvent
match_recognize (
measures A.currentPrice as a_currentPrice, B.currentPrice as b_currentPrice,
A.stockName as a_stockName, B.stockName as b_stockName
pattern (A C* B)
define
A as A.stockName = firstStock,
B as A.currentPrice - B.currentPrice >= difference and B.stockName =
secondStock
);
如您所见,其中包含三个变量-firstStock,secondStock,差异。我接受用户的输入,然后像这样在Java代码中设置变量:
System.out.println("Please enter 3 char stock name for the first stock: ");
System.out.println("Available stocks: IBM, YAH, MIC");
first = scanner.nextLine();
engineHelper.getAdmin().getConfiguration().addVariable("firstStock", String.class, first);
System.out.println("Please enter 3 char stock name for the second stock: ");
second = scanner.nextLine();
engineHelper.getAdmin().getConfiguration().addVariable("secondStock", String.class, second);
System.out.println("Please enter integer value for stock difference: ");
difference = scanner.nextInt();
engineHelper.getAdmin().getConfiguration().addVariable("difference", Integer.class, difference);
如果我一次只想跟踪一对股票,那效果很好。我正在努力寻找一种针对多对的方法。我希望能够动态创建/删除/开始/停止对。例如,假设我有YAH,APP,MIC,GOO股票。事件开始运行,我决定要跟踪MIC / GOO之间的差异是否超过X数量。然后,我决定我也要跟踪APP / GOO的金额。数据将如下所示:
[IBM,YAH,5]
[GOO,APP,3]
....
关于如何做的任何建议?我想我需要用一组新变量创建一个新的EPL实例。我可以在Java代码中轻松地做到这一点,但我想尽可能地远离它。我想使用模块文件。由于它本质上是相同的EPL,因此有意义的是,有一种方法可以将其用作具有不同股票对的多个“实例”的模板。
另外,还有其他方法可以有效地实现这一目标吗?
我可以正常工作了,我发现错误来自文件中不再存在的文本,因此我删除了它并重写了它,然后它起作用了。现在已成功部署所有组件,外观如下:
module context;
import events.*;
import configDemo.*;
import annotations.*;
import main.*;
import subscribers.*;
create schema InitEvent(firstStock string, secondStock string, bias double);
create context TwoStocksContext
initiated by InitEvent as initEvent;
@Name('compareStocks')
@Description('Compare the difference between two different stocks and make a
decision')
@Subscriber(className='subscribers.MySubscriber')
context TwoStocksContext
select * from TickEvent
match_recognize (
measures A.currentPrice as a_currentPrice, B.currentPrice as b_currentPrice, A.stockCode as a_stockCode, B.stockCode as b_stockCode
pattern (A C* B)
define
A as A.stockCode = context.initEvent.firstStock,
B as A.currentPrice - B.currentPrice >= context.initEvent.bias and
B.stockCode = context.initEvent.secondStock
);
最佳答案
您可以让Esper使用上下文分配语句的多个分区。将变量放入“ Init”事件中,并发送该事件以分配每个事件。
一个例子
create schema InitEvent(firststock string, secondstock string, diff double);
create context AnalyzePerFirstAndSecond initiated by InitEvent as initEvent; // add terminated here if needed
context AnalyzePerFirstAndSecond select .... define A as A.stock = context.initEvent.firststock....
文档章节链接可提供更完整的解决方案...
http://esper.espertech.com/release-7.0.0/esper-reference/html_single/index.html#perf-tips-27