change
This commit is contained in:
parent
22f38e54a7
commit
acd270836c
|
@ -1,8 +1,13 @@
|
|||
package com.taosdata.taosdemo;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@MapperScan(basePackages = {"com.taosdata.taosdemo.mapper"})
|
||||
@SpringBootApplication
|
||||
|
@ -10,6 +15,7 @@ public class TaosdemoApplication {
|
|||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TaosdemoApplication.class, args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
|
||||
@Component
|
||||
|
@ -33,17 +36,13 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
private SubTableService subTableService;
|
||||
|
||||
private SuperTableMeta superTableMeta;
|
||||
// private List<SubTableMeta> subTableMetaList;
|
||||
// private List<SubTableValue> subTableValueList;
|
||||
// private List<List<SubTableValue>> dataList;
|
||||
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
// 读配置参数
|
||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||
boolean isHelp = Arrays.asList(args).contains("--help");
|
||||
if (isHelp || config.host == null || config.host.isEmpty()) {
|
||||
if (isHelp) {
|
||||
JdbcTaosdemoConfig.printHelp();
|
||||
System.exit(0);
|
||||
}
|
||||
|
@ -100,8 +99,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
}
|
||||
|
||||
private void insertTask(JdbcTaosdemoConfig config) {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
long numOfTables = config.numOfTables;
|
||||
int numOfTablesPerSQL = config.numOfTablesPerSQL;
|
||||
long numOfRowsPerTable = config.numOfRowsPerTable;
|
||||
|
@ -118,7 +115,11 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
if (numOfTables < numOfTablesPerSQL)
|
||||
numOfTablesPerSQL = (int) numOfTables;
|
||||
|
||||
long timeCost = 0;
|
||||
|
||||
ExecutorService executors = Executors.newFixedThreadPool(config.numOfThreadsForInsert);
|
||||
List<Future<Integer>> futureList = new ArrayList<>();
|
||||
long start = System.currentTimeMillis();
|
||||
long affectRows = 0;
|
||||
|
||||
// row
|
||||
for (long rowCnt = 0; rowCnt < numOfRowsPerTable; ) {
|
||||
|
@ -135,25 +136,22 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
}
|
||||
/***********************************************/
|
||||
long startTime = config.startTime + rowCnt * config.timeGap;
|
||||
|
||||
// for (int i = 0; i < tableSize; i++) {
|
||||
// System.out.print(config.prefixOfTable + (tableCnt + i + 1) + ", tableSize: " + tableSize + ", rowSize: " + rowSize);
|
||||
// System.out.println(", startTime: " + TimeStampUtil.longToDatetime(startTime) + ",timeGap: " + config.timeGap);
|
||||
// }
|
||||
|
||||
// 生成数据
|
||||
List<SubTableValue> data = SubTableValueGenerator.generate(superTableMeta, config.prefixOfTable, tableCnt, tableSize, rowSize, startTime, config.timeGap);
|
||||
// List<SubTableValue> data = SubTableValueGenerator.generate(subTableMetaList, tableCnt, tableSize, rowSize, startTime, config.timeGap);
|
||||
// 乱序
|
||||
if (config.order != 0) {
|
||||
SubTableValueGenerator.disrupt(data, config.rate, config.range);
|
||||
}
|
||||
// insert
|
||||
if (config.autoCreateTable) {
|
||||
long a = System.currentTimeMillis();
|
||||
subTableService.insertAutoCreateTable(data, config.numOfThreadsForInsert, config.frequency);
|
||||
long b = System.currentTimeMillis();
|
||||
timeCost += (b - a);
|
||||
Future<Integer> future = executors.submit(() -> subTableService.insertAutoCreateTable(data));
|
||||
try {
|
||||
affectRows += future.get();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
subTableService.insert(data, config.numOfThreadsForInsert, config.frequency);
|
||||
}
|
||||
|
@ -162,8 +160,9 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
}
|
||||
rowCnt += rowSize;
|
||||
}
|
||||
|
||||
|
||||
executors.shutdown();
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> insert " + affectRows + " rows with time cost: " + (end - start) + "ms");
|
||||
/*********************************************************************************/
|
||||
// 批量插入,自动建表
|
||||
// dataList.stream().forEach(subTableValues -> {
|
||||
|
@ -178,8 +177,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
|
|||
|
||||
// subTableService.insert(subTableMetaList, config.numOfTables, config.tablePrefix, config.numOfThreadsForInsert, config.frequency);
|
||||
// }
|
||||
long end = System.currentTimeMillis();
|
||||
logger.info(">>> total : " + (end - start) + " ms, insert : " + (timeCost) + " ms.");
|
||||
}
|
||||
|
||||
private void prepareMetaData(JdbcTaosdemoConfig config) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import com.taosdata.taosdemo.domain.*;
|
|||
import com.taosdata.taosdemo.mapper.SubTableMapper;
|
||||
import com.taosdata.taosdemo.service.data.SubTableMetaGenerator;
|
||||
import com.taosdata.taosdemo.utils.TimeStampUtil;
|
||||
import com.zaxxer.hikari.pool.HikariPool;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -85,12 +86,17 @@ public class SubTableService extends AbstractService {
|
|||
}
|
||||
|
||||
// 插入:多线程,多表, 自动建表
|
||||
public int insertAutoCreateTable(List<SubTableValue> subTableValues, int threadSize, int frequency) {
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadSize);
|
||||
Future<Integer> future = executor.submit(() -> insertAutoCreateTable(subTableValues));
|
||||
executor.shutdown();
|
||||
return getAffectRows(future);
|
||||
}
|
||||
// public int insertAutoCreateTable(List<SubTableValue> subTableValues, int threadSize, int frequency) {
|
||||
// long a = System.currentTimeMillis();
|
||||
// ExecutorService executor = Executors.newFixedThreadPool(threadSize);
|
||||
// long b = System.currentTimeMillis();
|
||||
// Future<Integer> future = executor.submit(() -> insertAutoCreateTable(subTableValues));
|
||||
// executor.shutdown();
|
||||
// int affectRows = getAffectRows(future);
|
||||
// long c = System.currentTimeMillis();
|
||||
// logger.info(">>> total : " + (c - a) + " ms, thread: " + (b - a) + " ms, insert : " + (c - b) + " ms.");
|
||||
// return affectRows;
|
||||
// }
|
||||
|
||||
// 插入:单表,insert into xxx values(),()...
|
||||
public int insert(SubTableValue subTableValue) {
|
||||
|
@ -114,31 +120,18 @@ public class SubTableService extends AbstractService {
|
|||
|
||||
// 插入:多表,自动建表, insert into xxx using XXX tags(...) values(),()... xxx using XXX tags(...) values(),()...
|
||||
public int insertAutoCreateTable(List<SubTableValue> subTableValues) {
|
||||
Connection connection = null;
|
||||
Statement statement = null;
|
||||
|
||||
int affectRows = 0;
|
||||
try {
|
||||
connection = dataSource.getConnection();
|
||||
// String sql = sqlSessionFactory.getConfiguration()
|
||||
// .getMappedStatement("com.taosdata.taosdemo.mapper.SubTableMapper.insertMultiTableMultiValuesUsingSuperTable")
|
||||
// .getBoundSql(subTableValues)
|
||||
// .getSql();
|
||||
Connection connection = dataSource.getConnection();
|
||||
String sql = sql(subTableValues);
|
||||
logger.info(">>> SQL : " + sql);
|
||||
statement = connection.createStatement();
|
||||
// logger.info(">>> SQL : " + sql);
|
||||
Statement statement = connection.createStatement();
|
||||
affectRows = statement.executeUpdate(sql);
|
||||
statement.close();
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
if (connection != null)
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return affectRows;
|
||||
// return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues);
|
||||
|
|
|
@ -10,7 +10,9 @@ spring.datasource.password=taosdata
|
|||
#spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver
|
||||
#spring.datasource.username=root
|
||||
#spring.datasource.password=taosdata
|
||||
spring.datasource.hikari.maximum-pool-size=10
|
||||
spring.datasource.hikari.minimum-idle=10
|
||||
spring.datasource.hikari.maximum-pool-size=500
|
||||
spring.datasource.hikari.minimum-idle=500
|
||||
spring.datasource.hikari.max-lifetime=0
|
||||
logging.level.com.taosdata.taosdemo.mapper=error
|
||||
logging.level.com.taosdata.taosdemo.mapper=error
|
||||
|
||||
server.port=8888
|
Loading…
Reference in New Issue