--------------------------------------------------------------------------------------------------------------------------------------
表结构
--------------------------------------------------------------------------------------------------------------------------------------
/*Table: marketdata*/
---------------------
/*Column Information*/
----------------------
FIELD TYPE COLLATION NULL KEY DEFAULT Extra PRIVILEGES COMMENT
-------- ---------------- --------------- ------ ------ ------- ------ ------------------------------- -------
ticker VARCHAR(200) utf8_general_ci NO PRI SELECT,INSERT,UPDATE,REFERENCES
exchange VARCHAR(200) utf8_general_ci YES (NULL) SELECT,INSERT,UPDATE,REFERENCES
amount INT(10) UNSIGNED (NULL) YES (NULL) SELECT,INSERT,UPDATE,REFERENCES
/*Index Information*/
---------------------
TABLE Non_unique Key_name Seq_in_index Column_name COLLATION Cardinality Sub_part Packed NULL Index_type COMMENT
---------- ---------- -------- ------------ ----------- --------- ----------- -------- ------ ------ ---------- -------
marketdata 0 PRIMARY 1 ticker A 41 (NULL) (NULL) BTREE
/*DDL Information*/
-------------------
CREATE TABLE `marketdata` (
`ticker` VARCHAR(200) NOT NULL DEFAULT '',
`exchange` VARCHAR(200) DEFAULT NULL,
`amount` INT(10) UNSIGNED DEFAULT NULL,
PRIMARY KEY (`ticker`)
) ENGINE=INNODB DEFAULT CHARSET=utf8
--------------------------------------------------------------------------------------------------------------------------------------
源代码
--------------------------------------------------------------------------------------------------------------------------------------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import junit.framework.TestCase;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import com.espertech.esper.client.Configuration;
import com.espertech.esper.client.ConfigurationDBRef;
import com.espertech.esper.client.ConfigurationRevisionEventType;
import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esperio.db.EsperIODBAdapter;
import com.espertech.esperio.db.config.BindingParameter;
import com.espertech.esperio.db.config.ConfigurationDBAdapter;
import com.espertech.esperio.db.config.DMLQuery;
import com.espertech.esperio.db.config.Executor;
public class DemoMemoryTest extends TestCase
{
public void before() {
}
public void testTimeIsolation() throws Exception
{
Configuration config = new Configuration();
// 定义event
ConfigurationRevisionEventType configRev = new ConfigurationRevisionEventType();
configRev.addNameBaseEventType("MarketData");
// configRev.setKeyPropertyNames(new String[] {"ticker","exchange"});
configRev.setPropertyRevision(ConfigurationRevisionEventType.PropertyRevision.MERGE_EXISTS);
config.addEventType("MarketData", MarketData.class);
// 初始化Esper引擎
EPServiceProvider epService = EPServiceProviderManager.getProvider("engineURI", config);
// 配置数据源
Properties props = new Properties();
props.put("username", "root");
props.put("password", "root");
props.put("driverClassName", "com.mysql.jdbc.Driver");
props.put("url", "jdbc:mysql://localhost:3306/test");
props.put("initialSize", 2);
props.put("validationQuery", "select 1 from dual");
ConfigurationDBRef configDB = new ConfigurationDBRef();
configDB.setDataSourceFactory(props, BasicDataSourceFactory.class.getName());
configDB.setConnectionLifecycleEnum(ConfigurationDBRef.ConnectionLifecycleEnum.POOLED);
// configDB.setDriverManagerConnection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", props);
// 配置数据源适配器
ConfigurationDBAdapter adapterConfig = new ConfigurationDBAdapter();
adapterConfig.getJdbcConnections().put("db1", configDB);
// 配置DMLadd additional configuration such as DML and Upsert
DMLQuery dmlquery=new DMLQuery();
dmlquery.setRetry(2);
// 设置DML参数(将event事件流中的event保存到数据库中)
dmlquery.setExecutorName("queue1");
dmlquery.setConnection("db1");
dmlquery.setName("MyInsertQuery");
dmlquery.setStream("MarketData");
dmlquery.setSql("insert into MarketData(ticker, exchange, amount)values (?, ?, ?)");
List<BindingParameter> bindings=new ArrayList<BindingParameter>();
// 绑定要插入数据库的Event参数
bindParam(bindings,1,"ticker");
bindParam(bindings,2,"exchange");
bindParam(bindings,3,"amount");
dmlquery.setBindings(bindings);
adapterConfig.getDmlQueries().add(dmlquery);
// 配置执行线程
Executor executor=new Executor();
executor.setNumThreads(2);
Map<String,Executor> executors=new HashMap<String,Executor>();
executors.put("queue1", executor);
adapterConfig.setExecutors(executors);
// 启动数据库适配器进行监听start adapter
EsperIODBAdapter dbAdapter = new EsperIODBAdapter(adapterConfig, "engineURI");
dbAdapter.start();
// 生成event事件流
int ww=0;
// while(true) {
for (int i = 0; i < 500; i++) { //0000
epService.getEPRuntime().sendEvent(new MarketData(Integer.toString(i), Integer.toString(i)+"test",i+1));
ww++;
System.out.println("the number of sending message id is ["+i+"]" );
Thread.sleep(1000);
}
// }
}
private void bindParam(List<BindingParameter> bindings,int position,String property) {
BindingParameter bindingParameter=new BindingParameter();
bindingParameter.setPosition(position);
bindingParameter.setPropertyName(property);
bindings.add(bindingParameter);
}
public static class MarketData {
private String ticker;
private String exchange;
private int amount;
public int getAmount() {
return amount;
}
public void setAmount(final int amount) {
this.amount = amount;
}
public MarketData(String ticker, String exchange,int amount) {
this.ticker = ticker;
this.exchange = exchange;
this.amount = amount;
}
public String getTicker() {
return ticker;
}
public String getExchange() {
return exchange;
}
}
}