(我从Esper跳到Siddhi)
我正在尝试在同一个流中部署多个查询。此处的示例https://docs.wso2.com/display/CEP400/Using+Siddhi+as+a+Library显示了如何在流中部署查询,完全在一个ExecutionPlan中。那么,如果我想将查询添加到同一个执行计划,同一个流?
,该怎么办?我有兴趣用Java语言编写这个代码。
更新
我想在现有的执行计划中添加查询。这意味着当流到达时,而不是之前。 使用发布为答案的示例:
SiddhiManager siddhiManager = new SiddhiManager();
String executionPlan = "" +
"@Plan:name('demo') " +
"" +
"define stream cseEventStream (symbol string, price float, volume long);" +
"" +
"from cseEventStream[symbol==\"WSO2\"] " +
"insert into wso2Stream;" +
"" +
"from cseEventStream[symbol==\"ABC\"] " +
"insert into abcStream;";
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(executionPlan);
StreamCallback streamCallback = new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
}
};
executionPlanRuntime.addCallback("wso2Stream", streamCallback);
//Similarly, we can add another call back for abcStream
streamCallback.startProcessing();
InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
executionPlanRuntime.start();
// HERE: ADD new query <---------------------------------------------
inputHandler.send(new Object[]{"WSO2", 700f, 100l});
inputHandler.send(new Object[]{"ABC", 60.5f, 200l});
inputHandler.send(new Object[]{"WSO2", 60.5f, 200l});
streamCallback.stopProcessing();
executionPlanRuntime.shutdown();
答案 0 :(得分:1)
以下示例在同一个执行计划中为同一个流(cseEventStream
)添加多个查询。
SiddhiManager siddhiManager = new SiddhiManager();
String executionPlan = "" +
"@Plan:name('demo') " +
"" +
"define stream cseEventStream (symbol string, price float, volume long);" +
"" +
"from cseEventStream[symbol==\"WSO2\"] " +
"insert into wso2Stream;" +
"" +
"from cseEventStream[symbol==\"ABC\"] " +
"insert into abcStream;";
ExecutionPlanRuntime executionPlanRuntime = siddhiManager.createExecutionPlanRuntime(executionPlan);
StreamCallback streamCallback = new StreamCallback() {
@Override
public void receive(Event[] events) {
EventPrinter.print(events);
}
};
executionPlanRuntime.addCallback("wso2Stream", streamCallback);
//Similarly, we can add another call back for abcStream
streamCallback.startProcessing();
InputHandler inputHandler = executionPlanRuntime.getInputHandler("cseEventStream");
executionPlanRuntime.start();
inputHandler.send(new Object[]{"WSO2", 700f, 100l});
inputHandler.send(new Object[]{"ABC", 60.5f, 200l});
inputHandler.send(new Object[]{"WSO2", 60.5f, 200l});
streamCallback.stopProcessing();
executionPlanRuntime.shutdown();
我使用的是Siddhi版本3.0.6-beta2。
此代码示例是对Siddhi Passthrough test cases(PassThroughTest4
)之一的修改。
查看ExecutionPlanRuntime class,似乎无法在运行中添加查询&#34;。
因此,根据我的理解,您必须关闭当前的执行计划运行时,添加这些新查询并重新启动它。