我正在玩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、difference。我接受用户的输入,然后将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的差额。然后,我决定,我想要跟踪应用程序/GOO的数量也不同。数据应该是这样的:
IBM,耶,5.
对如何做到这一点有什么建议吗?我想我需要用一组新的变量创建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
);发布于 2018-01-19 13:52:18
可以让Esper使用上下文分配语句的多个分区。将变量放入"Init"-Event中,并发送此事件来分配每个变量。一个例子
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....更完整的解决方案..。single/index.html#perf-tips-27
https://stackoverflow.com/questions/48336961
复制相似问题