Esper: How to configure Esper to connect a Relational Database, through a JDBC, using Esper's configuration API

£可爱£侵袭症+ 提交于 2019-12-13 04:57:56

问题


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.

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.)

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.


回答1:


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);

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)

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);


来源:https://stackoverflow.com/questions/26735044/esper-how-to-configure-esper-to-connect-a-relational-database-through-a-jdbc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!