Esper (ii) database Chapter 2 inserting data into the database __ database

Source: Internet
Author: User
Tags static class

--------------------------------------------------------------------------------------------------------------- -----------------------

Table structure

--------------------------------------------------------------------------------------------------------------- -----------------------

/*table:marketdata*/
---------------------

/*column information*/
----------------------

FIELD TYPE collation NULL KEY DEFAULT Extra Privileges COMMENT
--------  ----------------  ---------------  ------  ------  -------  ------  -------------------------------  -------
Ticker VARCHAR (m) utf8_general_ci NO PRI select,insert,update,references
Exchange VARCHAR utf8_general_ci YES (NULL) select,insert,update,references
Amount INT (a) UNSIGNED (null) YES (NULL) select,insert,update,references

/*index information*/
---------------------

TABLE non_unique key_name seq_in_index column_name collation cardinality sub_part NULL Packed COMMENT
----------  ----------  --------  ------------  -----------  ---------  -----------  --------  ------  ------  ---------- -------
Marketdata 0 PRIMARY 1 ticker A (NULL) (NULL) btree

/*DDL information*/
-------------------

CREATE TABLE ' Marketdata ' (
' Ticker ' VARCHAR not NULL DEFAULT ',
' Exchange ' VARCHAR DEFAULT NULL,
' Amount ' INT (a) UNSIGNED DEFAULT NULL,
PRIMARY KEY (' ticker ')
) Engine=innodb DEFAULT Charset=utf8

--------------------------------------------------------------------------------------------------------------- -----------------------

Source

--------------------------------------------------------------------------------------------------------------- -----------------------


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 ();
Define 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);
Initializing the Esper Engine
Epserviceprovider Epservice = Epserviceprovidermanager.getprovider ("Engineuri", config);
Configure a data source
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);
Configure Data source Adapters
Configurationdbadapter adapterconfig = new Configurationdbadapter ();
Adapterconfig.getjdbcconnections (). Put ("DB1", configdb);

Configure Dmladd additional configuration such as DML and Upsert
Dmlquery dmlquery=new dmlquery ();
Dmlquery.setretry (2);
To set the DML parameter (save event in event flow to the database)
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> ();
Bind the event argument to insert the database
Bindparam (bindings,1, "ticker");
Bindparam (bindings,2, "exchange");
Bindparam (bindings,3, "Amount");
Dmlquery.setbindings (bindings);
Adapterconfig.getdmlqueries (). Add (Dmlquery);
Configuring Execution Threads
Executor executor=new Executor ();
Executor.setnumthreads (2);
Map<string,executor> executors=new hashmap<string,executor> ();
Executors.put ("queue1", executor);
Adapterconfig.setexecutors (executors);
Start the database adapter to listen for start adapter
Esperiodbadapter dbadapter = new Esperiodbadapter (adapterconfig, "Engineuri");
Dbadapter.start ();
Generate Event Event Flow
int ww=0;
while (true) {
for (int i = 0; i < 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;
}
}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.