提交 acd27083 编写于 作者: Z zyyang

change

上级 22f38e54
package com.taosdata.taosdemo; package com.taosdata.taosdemo;
import com.zaxxer.hikari.HikariDataSource;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; 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"}) @MapperScan(basePackages = {"com.taosdata.taosdemo.mapper"})
@SpringBootApplication @SpringBootApplication
...@@ -10,6 +15,7 @@ public class TaosdemoApplication { ...@@ -10,6 +15,7 @@ public class TaosdemoApplication {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(TaosdemoApplication.class, args); SpringApplication.run(TaosdemoApplication.class, args);
} }
} }
...@@ -15,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired; ...@@ -15,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner; import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.time.Duration; import java.time.Duration;
import java.time.Instant; import java.time.Instant;
import java.util.*; import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@Component @Component
...@@ -33,17 +36,13 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -33,17 +36,13 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
private SubTableService subTableService; private SubTableService subTableService;
private SuperTableMeta superTableMeta; private SuperTableMeta superTableMeta;
// private List<SubTableMeta> subTableMetaList;
// private List<SubTableValue> subTableValueList;
// private List<List<SubTableValue>> dataList;
@Override @Override
public void run(String... args) throws Exception { public void run(String... args) throws Exception {
// 读配置参数 // 读配置参数
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args); JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
boolean isHelp = Arrays.asList(args).contains("--help"); boolean isHelp = Arrays.asList(args).contains("--help");
if (isHelp || config.host == null || config.host.isEmpty()) { if (isHelp) {
JdbcTaosdemoConfig.printHelp(); JdbcTaosdemoConfig.printHelp();
System.exit(0); System.exit(0);
} }
...@@ -100,8 +99,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -100,8 +99,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
} }
private void insertTask(JdbcTaosdemoConfig config) { private void insertTask(JdbcTaosdemoConfig config) {
long start = System.currentTimeMillis();
long numOfTables = config.numOfTables; long numOfTables = config.numOfTables;
int numOfTablesPerSQL = config.numOfTablesPerSQL; int numOfTablesPerSQL = config.numOfTablesPerSQL;
long numOfRowsPerTable = config.numOfRowsPerTable; long numOfRowsPerTable = config.numOfRowsPerTable;
...@@ -118,7 +115,11 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -118,7 +115,11 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
if (numOfTables < numOfTablesPerSQL) if (numOfTables < numOfTablesPerSQL)
numOfTablesPerSQL = (int) numOfTables; 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 // row
for (long rowCnt = 0; rowCnt < numOfRowsPerTable; ) { for (long rowCnt = 0; rowCnt < numOfRowsPerTable; ) {
...@@ -135,25 +136,22 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -135,25 +136,22 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
} }
/***********************************************/ /***********************************************/
long startTime = config.startTime + rowCnt * config.timeGap; 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(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) { if (config.order != 0) {
SubTableValueGenerator.disrupt(data, config.rate, config.range); SubTableValueGenerator.disrupt(data, config.rate, config.range);
} }
// insert // insert
if (config.autoCreateTable) { if (config.autoCreateTable) {
long a = System.currentTimeMillis(); Future<Integer> future = executors.submit(() -> subTableService.insertAutoCreateTable(data));
subTableService.insertAutoCreateTable(data, config.numOfThreadsForInsert, config.frequency); try {
long b = System.currentTimeMillis(); affectRows += future.get();
timeCost += (b - a); } catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
} else { } else {
subTableService.insert(data, config.numOfThreadsForInsert, config.frequency); subTableService.insert(data, config.numOfThreadsForInsert, config.frequency);
} }
...@@ -162,8 +160,9 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -162,8 +160,9 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
} }
rowCnt += rowSize; rowCnt += rowSize;
} }
executors.shutdown();
long end = System.currentTimeMillis();
logger.info(">>> insert " + affectRows + " rows with time cost: " + (end - start) + "ms");
/*********************************************************************************/ /*********************************************************************************/
// 批量插入,自动建表 // 批量插入,自动建表
// dataList.stream().forEach(subTableValues -> { // dataList.stream().forEach(subTableValues -> {
...@@ -178,8 +177,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { ...@@ -178,8 +177,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner {
// subTableService.insert(subTableMetaList, config.numOfTables, config.tablePrefix, config.numOfThreadsForInsert, config.frequency); // 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) { private void prepareMetaData(JdbcTaosdemoConfig config) {
......
...@@ -4,6 +4,7 @@ import com.taosdata.taosdemo.domain.*; ...@@ -4,6 +4,7 @@ import com.taosdata.taosdemo.domain.*;
import com.taosdata.taosdemo.mapper.SubTableMapper; import com.taosdata.taosdemo.mapper.SubTableMapper;
import com.taosdata.taosdemo.service.data.SubTableMetaGenerator; import com.taosdata.taosdemo.service.data.SubTableMetaGenerator;
import com.taosdata.taosdemo.utils.TimeStampUtil; import com.taosdata.taosdemo.utils.TimeStampUtil;
import com.zaxxer.hikari.pool.HikariPool;
import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -85,12 +86,17 @@ public class SubTableService extends AbstractService { ...@@ -85,12 +86,17 @@ public class SubTableService extends AbstractService {
} }
// 插入:多线程,多表, 自动建表 // 插入:多线程,多表, 自动建表
public int insertAutoCreateTable(List<SubTableValue> subTableValues, int threadSize, int frequency) { // public int insertAutoCreateTable(List<SubTableValue> subTableValues, int threadSize, int frequency) {
ExecutorService executor = Executors.newFixedThreadPool(threadSize); // long a = System.currentTimeMillis();
Future<Integer> future = executor.submit(() -> insertAutoCreateTable(subTableValues)); // ExecutorService executor = Executors.newFixedThreadPool(threadSize);
executor.shutdown(); // long b = System.currentTimeMillis();
return getAffectRows(future); // 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(),()... // 插入:单表,insert into xxx values(),()...
public int insert(SubTableValue subTableValue) { public int insert(SubTableValue subTableValue) {
...@@ -114,31 +120,18 @@ public class SubTableService extends AbstractService { ...@@ -114,31 +120,18 @@ public class SubTableService extends AbstractService {
// 插入:多表,自动建表, insert into xxx using XXX tags(...) values(),()... xxx using XXX tags(...) values(),()... // 插入:多表,自动建表, insert into xxx using XXX tags(...) values(),()... xxx using XXX tags(...) values(),()...
public int insertAutoCreateTable(List<SubTableValue> subTableValues) { public int insertAutoCreateTable(List<SubTableValue> subTableValues) {
Connection connection = null;
Statement statement = null;
int affectRows = 0; int affectRows = 0;
try { try {
connection = dataSource.getConnection(); Connection connection = dataSource.getConnection();
// String sql = sqlSessionFactory.getConfiguration()
// .getMappedStatement("com.taosdata.taosdemo.mapper.SubTableMapper.insertMultiTableMultiValuesUsingSuperTable")
// .getBoundSql(subTableValues)
// .getSql();
String sql = sql(subTableValues); String sql = sql(subTableValues);
logger.info(">>> SQL : " + sql); // logger.info(">>> SQL : " + sql);
statement = connection.createStatement(); Statement statement = connection.createStatement();
affectRows = statement.executeUpdate(sql); affectRows = statement.executeUpdate(sql);
statement.close();
connection.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null)
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
return affectRows; return affectRows;
// return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues); // return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues);
......
...@@ -10,7 +10,9 @@ spring.datasource.password=taosdata ...@@ -10,7 +10,9 @@ spring.datasource.password=taosdata
#spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver #spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver
#spring.datasource.username=root #spring.datasource.username=root
#spring.datasource.password=taosdata #spring.datasource.password=taosdata
spring.datasource.hikari.maximum-pool-size=10 spring.datasource.hikari.maximum-pool-size=500
spring.datasource.hikari.minimum-idle=10 spring.datasource.hikari.minimum-idle=500
spring.datasource.hikari.max-lifetime=0 spring.datasource.hikari.max-lifetime=0
logging.level.com.taosdata.taosdemo.mapper=error logging.level.com.taosdata.taosdemo.mapper=error
\ No newline at end of file
server.port=8888
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册