java - 创建 Esper 的 epl 实例

标签 java complex-event-processing esper stocks epl

我正在和 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 数量的差异。然后我决定也想跟踪 APP/GOO,但金额不同。数据会是这样的:

[IBM,是的,5] [咕咕,应用程序,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

关于java - 创建 Esper 的 epl 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48336961/

相关文章:

java - 使用 EPStatementObjectModel (Esper) 引用事件过滤器内的命名全局表达式

visual-studio - 适用于 Visual Studio 的 Azure 流分析工具 : Error when executing aggregated queries - "Object reference not set to an instance of an object"

java - 在 Esper 中查找字符串列表中的字符串是否在另一个字符串列表中

java - EPL Esper 从 java Set 中选择

java - 如何知道字符串变量是否只包含java中的空格?

java - NetBeans 平台主机

java - GNUmakefile无法编译bison和flex程序

java - addPlugInSingleRowFunction 将 Esper 版本从 5.3 升级到 8.3 时出现问题

java - 关于esper引擎的示例代码

java - 选择具有最大日期值的记录