问题描述
我想知道如何使用Esper的引擎(v5.0.0)的java客户端应用程序可以配置引擎实例,以便他可以连接到关系数据库,例如pgSQL。
I would like to know how a java client application that is using Esper's engine (v5.0.0) may configure engine instance so he can to connect to a relation database, say pgSQL.
这对于编写能够从数据库中加入带有静态/历史数据的事件流(或数据流)的EPL查询至关重要()。也就是说,从数据库读取。 (写入数据库需要使用。)
This is essential to write EPL queries capable to join event streams (or data stream) with static/historic data from a database (5.13. Accessing Relational Data via SQL). That is, to Read from a database. (Write to a database requires the usage of a EsperIO adapter.)
从我发现和类应该用于通过其API配置Esper的数据库连接。
From Esper's docs I figure out that Configuration and ConfigurationDBRef classes should be used to configure Esper's database connection through its API.
但是,根据我的研究,剩下的文档并不是很清楚整个配置过程,我很努力。
However, at the best of my research, the remaining documentation is not very clear about the entire configuration process, for which I'm struggling to.
推荐答案
以下代码段显示了整个配置过程:
The following code snippet shows the entire configuration process:
ConfigurationDBRef dbConfig = new ConfigurationDBRef();
dbConfig.setDriverManagerConnection("org.postgresql.Driver",
"jdbc:postgresql://localhost:5432/database_name",
"user",
"password");
Configuration engineConfig = new Configuration();
engineConfig.addDatabaseReference("database_alias", dbConfig);
esperEngine = EPServiceProviderManager.getDefaultProvider(engineConfig);
就是这样。 esperEngine 将是您的引擎实例,准备与 database_name 以 database_alias 作为查询statemet别名进行通信(用于查询的 FROM clause)
And that's it. esperEngine will be your engine instance prepared to communicate with database_name with database_alias as the query statemet alias (used in the query's FROM clause)
您可以通过以下方式在Esper的实例中安装查询:
You can install a query in Esper's instance in the following manner:
String statement = "SELECT datastream.column1, rel.column2" +
"FROM Datastream.Measure AS datastream, " +
"sql:database_alias ['SELECT column2 " +
"FROM \"SchemaX\".\"TableY\" ] AS rel";
//Install this query in the engine
EPStatement queryEngineObject = esperEngine.getEPAdministrator().createEPL(statement);
//Associate a Listener to this query
MyQueryListener listener = new MyQueryListener(); //that implements UpdateListener Interface
queryEngineObject.addListener(listener);
这篇关于Esper:如何使用Esper的配置API配置Esper以通过JDBC连接关系数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!