如何在Esper中使用模块并将EPL语句拆分到多个EPL文件?
在Esper中拆分EPL到多文件并使用模块的解决方案
错误原因
出现Failed to resolve event type...错误的核心原因:要么tally模块没显式声明依赖events模块,要么编译时没把events.epl纳入编译范围,导致Esper无法跨模块访问OrderExample事件类型。
正确的文件示例
events.epl(定义events模块及事件类型)
module events; // 定义OrderExample事件类型 create schema OrderExample (orderId string, amount double, timestamp long);
example.epl(tally模块引用events模块并查询)
module tally; // 显式声明依赖events模块,这一步是关键 uses events; // 现在可以正常访问OrderExample类型 select * from OrderExample;
编译部署注意事项
编译时需确保所有依赖模块都被包含在编译任务中,Esper编译器会自动处理依赖顺序。以下是Java编译部署的示例代码:
EPCompiler compiler = EPCompilerProvider.getCompiler(); EPCompilePath compilePath = new EPCompilePath(); // 同时传入两个EPL文件,编译器会解析依赖 EPCompileResult compileResult = compiler.compile( new File[]{new File("events.epl"), new File("example.epl")}, compilePath ); // 部署到Esper运行时 EPRuntime runtime = EPRuntimeProvider.getDefaultRuntime(); runtime.getDeploymentService().deploy(compileResult);
官方文档核心概念翻译(Esper 8.8.0)
编译器核心概念
模块是Esper组织EPL语句的基础单元,每个EPL文件必须归属一个模块,通过
module关键字声明。模块间通过uses关键字建立依赖关系,只有显式声明依赖的模块,才能访问被依赖模块中定义的事件类型、命名窗口、表等资源——这是Esper的模块隔离机制,避免命名冲突和资源混乱。编译多模块时,编译器会自动解析依赖顺序,但必须确保所有依赖模块都被纳入编译范围,或已提前编译并添加到编译上下文。
内容的提问来源于stack exchange,提问作者Dakotah North
相关产品推荐
相关产品推荐

