packagetest.threadinsert;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Date;importjava.util.List;importorg.apache.log4j.Logger;/*** Used a thread to insert records to a table
**/
public class ThreadInserter extendsThread{private static Logger log = Logger.getLogger(ThreadInserter.class);private static final int BatchSize=500;private int tbSN; //Table's serial number
private String tableName; //Tbale's name
private int count; //how many records should be inserted
private String[] innerArr; //array contains table types/fields
private Connection conn; //Connection used in single thread
private Statement stmt; //statemenet used in single throead
private int times; //How many times this thread should run
private InserterManager manager;//Reference to manager
/*** Constructor
*@paramtbSN
*@paramtableName
*@paramcount
*@paraminnerArr*/
public ThreadInserter(int tbSN,String tableName,intcount,String[] innerArr,InserterManager manager) {this.tbSN=tbSN;this.tableName=tableName;this.count=count;this.innerArr=innerArr;this.times=count/BatchSize;this.manager=manager;
}/*** thread method*/
public voidrun() {try{
log.info("Start...");long startTime =System.currentTimeMillis();//Initialize conn/stmt
DbParam_Dev dbParam=newDbParam_Dev();
Class.forName(dbParam.Driver).newInstance();
conn=DriverManager.getConnection(dbParam.DbUrl, dbParam.User, dbParam.Pswd);
stmt=conn.createStatement();//Clear
truncateTable();//Insert
insertTestDataTo();long endTime =System.currentTimeMillis();
String timeElasped=sec2DHMS((endTime - startTime)/1000);
log.info("#"+tbSN+" End. "+count+" records have been inserted to '"+tableName+"'.( time elapsed: " + timeElasped +")");///
manager.reportFinished(String.valueOf(tbSN), tableName, timeElasped);
}catch(Exception ex) {
ex.printStackTrace();
}finally{try{
stmt.close();
conn.close();
}catch(SQLException e) {
System.out.print("Can't close stmt/conn because of " +e.getMessage());
}
}
}/*** 清空一个表的数据,注意此功能有破坏性,不可恢复,注意备份好数据
*@paramtableName
*@paramconn
*@paramstmt
*@throwsSQLException*/
private void truncateTable() throwsSQLException{
String sql="truncate table "+tableName;
stmt.execute(sql);
log.info("truncated table:"+tableName);
}/*** 向一个表插入数据
*@paramtableName
*@paramcount
*@paraminnerArr
*@paramconn
*@paramstmt
*@throwsSQLException*/
private void insertTestDataTo() throwsSQLException{//得到字段名和字段类型
List typefields=new ArrayList();for(int i=1;i
String temp=innerArr[i];
String[] arrTmp=temp.split(":");
TypeField tf=newTypeField();
tf.type=arrTmp[0];
tf.field=arrTmp[1];
typefields.add(tf);
}
List fields=new ArrayList();
List values=new ArrayList();int index=0;for(TypeField tf:typefields) {
fields.add(tf.field);
values.add("''{"+index+"}''");
index++;
}int interval=2*365*24*60*60/count;//两年的秒数除以总个数即为间隔
index=0;//int times=count/BatchSize;
for(int i=0;i
StringBuilder sb=newStringBuilder();
sb.append("INSERT ALL ");for(int j=0;j
index=i*BatchSize+j;
sb.append(getInsertSql(tableName,typefields,index,interval));
}
sb.append(" select * from dual");
String sql=sb.toString();long startTime =System.currentTimeMillis();
stmt.executeUpdate(sql);long endTime =System.currentTimeMillis();
log.info("#"+tbSN+"-("+i+"/"+this.times+") "+BatchSize+" records inserted to '"+tableName+"' used " + sec2DHMS((endTime - startTime)/1000));
}
}/*** 得到批量插入语句
*@paramtableName
*@paramtypefields
*@paramindex
*@return
*/
private String getInsertSql(String tableName,List typefields,int index,intinterval) {
String currTime=getDatetimeBefore(index,interval);
StringBuilder sb=newStringBuilder();
sb.append(" INTO "+tableName+"(");
List fields=new ArrayList();for(TypeField tf:typefields) {
fields.add(tf.field);
}
sb.append(String.join(",",fields));
sb.append(") values(");
List values=new ArrayList();for(TypeField tf:typefields) {if(tf.type.equals("PK")) {//values.add("'"+String.valueOf(index)+"'");
if(tableName.contains("DELIVERY_INFO_HISTORY")) {
values.add("'0'");
}else{
values.add("'"+String.valueOf(index)+"'");
}
}else if(tf.type.equals("CH")) {
values.add("'0'");
}else if(tf.type.equals("US")) {
values.add("'unknown'");
}else if(tf.type.equals("DT")) {
values.add("to_date('"+currTime+"','yyyy-MM-dd HH24:mi:ss')");
}
}
sb.append(String.join(",",values));
sb.append(")");
String insertSql=sb.toString();returninsertSql;
}/*** 以当前时间为基准减去数十秒
*@paramn
*@return
*/
private static String getDatetimeBefore(int n,intinterval) {try{
Calendar now=Calendar.getInstance();
now.add(Calendar.SECOND,-n*interval);//日期减去n*10秒
Date newDate=now.getTime();/*3432432432432*/SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String retval=sdf.format(newDate);returnretval;
}catch(Exception ex) {
ex.printStackTrace();return null;
}
}/*** 将秒转化为日时分秒
*@paramsecondCount
*@return
*/
private static String sec2DHMS(longsecondCount) {
String retval= null;long days = secondCount / (60 * 60 * 24);long hours = (secondCount % (60 * 60 * 24)) / (60 * 60);long minutes = (secondCount % (60 * 60)) / 60;long seconds = secondCount % 60;
String strSeconds="";if(seconds!=0) {
strSeconds=seconds + "s";
}if (days > 0) {
retval= days + "d" + hours + "h" + minutes + "m" +strSeconds;
}else if (hours > 0) {
retval= hours + "h" + minutes + "m" +strSeconds;
}else if (minutes > 0) {
retval= minutes + "m" +strSeconds;
}else{
retval=strSeconds;
}
String str="AAA";
str="\"";
str="";
str="";
str="";returnretval;
}
}