change
This commit is contained in:
parent
8877907e70
commit
305cbd8c51
|
@ -6,6 +6,7 @@ import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||||
import com.taosdata.taosdemo.domain.TagMeta;
|
import com.taosdata.taosdemo.domain.TagMeta;
|
||||||
import com.taosdata.taosdemo.service.DatabaseService;
|
import com.taosdata.taosdemo.service.DatabaseService;
|
||||||
import com.taosdata.taosdemo.service.InsertTask;
|
import com.taosdata.taosdemo.service.InsertTask;
|
||||||
|
import com.taosdata.taosdemo.service.SubTableService;
|
||||||
import com.taosdata.taosdemo.service.SuperTableService;
|
import com.taosdata.taosdemo.service.SuperTableService;
|
||||||
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
|
import com.taosdata.taosdemo.service.data.SuperTableMetaGenerator;
|
||||||
import com.taosdata.taosdemo.components.JdbcTaosdemoConfig;
|
import com.taosdata.taosdemo.components.JdbcTaosdemoConfig;
|
||||||
|
@ -37,6 +38,7 @@ public class TaosDemoApplication {
|
||||||
DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password);
|
DataSource dataSource = DataSourceFactory.getInstance(config.host, config.port, config.user, config.password);
|
||||||
DatabaseService databaseService = new DatabaseService(dataSource);
|
DatabaseService databaseService = new DatabaseService(dataSource);
|
||||||
SuperTableService superTableService = new SuperTableService(dataSource);
|
SuperTableService superTableService = new SuperTableService(dataSource);
|
||||||
|
SubTableService subTableService = new SubTableService(dataSource);
|
||||||
|
|
||||||
// 创建数据库
|
// 创建数据库
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
|
@ -78,7 +80,6 @@ public class TaosDemoApplication {
|
||||||
// create super table with specified field size and tag size
|
// create super table with specified field size and tag size
|
||||||
superTableMeta = SuperTableMetaGenerator.generate(config.database, config.superTable, config.numOfFields, config.prefixOfFields, config.numOfTags, config.prefixOfTags);
|
superTableMeta = SuperTableMetaGenerator.generate(config.database, config.superTable, config.numOfFields, config.prefixOfFields, config.numOfTags, config.prefixOfTags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**********************************************************************************/
|
/**********************************************************************************/
|
||||||
// 建表
|
// 建表
|
||||||
start = System.currentTimeMillis();
|
start = System.currentTimeMillis();
|
||||||
|
@ -87,7 +88,7 @@ public class TaosDemoApplication {
|
||||||
if (config.autoCreateTable)
|
if (config.autoCreateTable)
|
||||||
return;
|
return;
|
||||||
// 批量建子表
|
// 批量建子表
|
||||||
// subTableService.createSubTable(superTableMeta, config.numOfTables, config.prefixOfTable, config.numOfThreadsForCreate);
|
subTableService.createSubTable(superTableMeta, config.numOfTables, config.prefixOfTable, config.numOfThreadsForCreate);
|
||||||
}
|
}
|
||||||
end = System.currentTimeMillis();
|
end = System.currentTimeMillis();
|
||||||
logger.info(">>> create table time cost : " + (end - start) + " ms.");
|
logger.info(">>> create table time cost : " + (end - start) + " ms.");
|
||||||
|
@ -102,7 +103,7 @@ public class TaosDemoApplication {
|
||||||
long gap = (long) Math.ceil((0.0d + tableSize) / threadSize);
|
long gap = (long) Math.ceil((0.0d + tableSize) / threadSize);
|
||||||
|
|
||||||
start = System.currentTimeMillis();
|
start = System.currentTimeMillis();
|
||||||
|
// multi threads to insert
|
||||||
List<FutureTask> taskList = new ArrayList<>();
|
List<FutureTask> taskList = new ArrayList<>();
|
||||||
List<Thread> threads = IntStream.range(0, threadSize)
|
List<Thread> threads = IntStream.range(0, threadSize)
|
||||||
.mapToObj(i -> {
|
.mapToObj(i -> {
|
||||||
|
|
|
@ -9,7 +9,7 @@ public final class JdbcTaosdemoConfig {
|
||||||
public String user = "root"; //user
|
public String user = "root"; //user
|
||||||
public String password = "taosdata"; //password
|
public String password = "taosdata"; //password
|
||||||
// database
|
// database
|
||||||
public String database = "test"; //database
|
public String database = "jdbcdb"; //database
|
||||||
public int keep = 3650; //keep
|
public int keep = 3650; //keep
|
||||||
public int days = 30; //days
|
public int days = 30; //days
|
||||||
public int replica = 1; //replica
|
public int replica = 1; //replica
|
||||||
|
@ -50,7 +50,7 @@ public final class JdbcTaosdemoConfig {
|
||||||
System.out.println("-user The TDengine user name to use when connecting to the server. Default is 'root'");
|
System.out.println("-user The TDengine user name to use when connecting to the server. Default is 'root'");
|
||||||
System.out.println("-password The password to use when connecting to the server.Default is 'taosdata'");
|
System.out.println("-password The password to use when connecting to the server.Default is 'taosdata'");
|
||||||
// database
|
// database
|
||||||
System.out.println("-database Destination database. Default is 'test'");
|
System.out.println("-database Destination database. Default is 'jdbcdb'");
|
||||||
System.out.println("-keep database keep parameter. Default is 3650");
|
System.out.println("-keep database keep parameter. Default is 3650");
|
||||||
System.out.println("-days database days parameter. Default is 30");
|
System.out.println("-days database days parameter. Default is 30");
|
||||||
System.out.println("-replica database replica parameter. Default 1, min: 1, max: 3");
|
System.out.println("-replica database replica parameter. Default 1, min: 1, max: 3");
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package com.taosdata.taosdemo.dao;
|
package com.taosdata.taosdemo.dao;
|
||||||
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface DatabaseMapper {
|
public interface DatabaseMapper {
|
||||||
|
|
||||||
// create database if not exists XXX
|
// create database if not exists XXX
|
||||||
|
|
|
@ -1,14 +1,19 @@
|
||||||
package com.taosdata.taosdemo.dao;
|
package com.taosdata.taosdemo.dao;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.DatabaseMapper;
|
|
||||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class DatabaseMapperImpl implements DatabaseMapper {
|
public class DatabaseMapperImpl implements DatabaseMapper {
|
||||||
|
|
||||||
private JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public DatabaseMapperImpl(DataSource dataSource) {
|
||||||
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createDatabase(String dbname) {
|
public void createDatabase(String dbname) {
|
||||||
|
|
|
@ -1,45 +1,55 @@
|
||||||
package com.taosdata.taosdemo.dao;
|
package com.taosdata.taosdemo.dao;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.SubTableMapper;
|
|
||||||
import com.taosdata.taosdemo.domain.SubTableMeta;
|
import com.taosdata.taosdemo.domain.SubTableMeta;
|
||||||
import com.taosdata.taosdemo.domain.SubTableValue;
|
import com.taosdata.taosdemo.domain.SubTableValue;
|
||||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SubTableMapperImpl implements SubTableMapper {
|
public class SubTableMapperImpl implements SubTableMapper {
|
||||||
|
|
||||||
private JdbcTemplate jdbcTemplate;
|
private static final Logger logger = Logger.getLogger(SubTableMapperImpl.class);
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public SubTableMapperImpl(DataSource dataSource) {
|
||||||
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createUsingSuperTable(SubTableMeta subTableMeta) {
|
public void createUsingSuperTable(SubTableMeta subTableMeta) {
|
||||||
String sql = SqlSpeller.createTableUsingSuperTable(subTableMeta);
|
String sql = SqlSpeller.createTableUsingSuperTable(subTableMeta);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
jdbcTemplate.execute(sql);
|
jdbcTemplate.execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertOneTableMultiValues(SubTableValue subTableValue) {
|
public int insertOneTableMultiValues(SubTableValue subTableValue) {
|
||||||
String sql = SqlSpeller.insertOneTableMultiValues(subTableValue);
|
String sql = SqlSpeller.insertOneTableMultiValues(subTableValue);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
return jdbcTemplate.update(sql);
|
return jdbcTemplate.update(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertOneTableMultiValuesUsingSuperTable(SubTableValue subTableValue) {
|
public int insertOneTableMultiValuesUsingSuperTable(SubTableValue subTableValue) {
|
||||||
String sql = SqlSpeller.insertOneTableMultiValuesUsingSuperTable(subTableValue);
|
String sql = SqlSpeller.insertOneTableMultiValuesUsingSuperTable(subTableValue);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
return jdbcTemplate.update(sql);
|
return jdbcTemplate.update(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertMultiTableMultiValues(List<SubTableValue> tables) {
|
public int insertMultiTableMultiValues(List<SubTableValue> tables) {
|
||||||
String sql = SqlSpeller.insertMultiSubTableMultiValues(tables);
|
String sql = SqlSpeller.insertMultiSubTableMultiValues(tables);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
return jdbcTemplate.update(sql);
|
return jdbcTemplate.update(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int insertMultiTableMultiValuesUsingSuperTable(List<SubTableValue> tables) {
|
public int insertMultiTableMultiValuesUsingSuperTable(List<SubTableValue> tables) {
|
||||||
String sql = SqlSpeller.insertMultiTableMultiValuesUsingSuperTable(tables);
|
String sql = SqlSpeller.insertMultiTableMultiValuesUsingSuperTable(tables);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
return jdbcTemplate.update(sql);
|
return jdbcTemplate.update(sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,31 @@
|
||||||
package com.taosdata.taosdemo.dao;
|
package com.taosdata.taosdemo.dao;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.SuperTableMapper;
|
|
||||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
import com.taosdata.taosdemo.utils.SqlSpeller;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
public class SuperTableMapperImpl implements SuperTableMapper {
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
public class SuperTableMapperImpl implements SuperTableMapper {
|
||||||
|
private static final Logger logger = Logger.getLogger(SuperTableMapperImpl.class);
|
||||||
private JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
public SuperTableMapperImpl(DataSource dataSource) {
|
||||||
|
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createSuperTable(SuperTableMeta tableMetadata) {
|
public void createSuperTable(SuperTableMeta tableMetadata) {
|
||||||
String sql = SqlSpeller.createSuperTable(tableMetadata);
|
String sql = SqlSpeller.createSuperTable(tableMetadata);
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
jdbcTemplate.execute(sql);
|
jdbcTemplate.execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dropSuperTable(String database, String name) {
|
public void dropSuperTable(String database, String name) {
|
||||||
jdbcTemplate.execute("drop table if exists " + database + "." + name);
|
String sql = "drop table if exists " + database + "." + name;
|
||||||
|
logger.info("SQL >>> " + sql);
|
||||||
|
jdbcTemplate.execute(sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,99 +1,42 @@
|
||||||
package com.taosdata.taosdemo.service;
|
package com.taosdata.taosdemo.service;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.DatabaseMapper;
|
import com.taosdata.taosdemo.dao.DatabaseMapper;
|
||||||
import org.apache.log4j.Logger;
|
import com.taosdata.taosdemo.dao.DatabaseMapperImpl;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
|
||||||
public class DatabaseService {
|
public class DatabaseService {
|
||||||
|
|
||||||
@Autowired
|
private final DatabaseMapper databaseMapper;
|
||||||
private DatabaseMapper databaseMapper;
|
|
||||||
|
|
||||||
private DataSource dataSource;
|
|
||||||
private static Logger logger = Logger.getLogger(DatabaseService.class);
|
|
||||||
|
|
||||||
public DatabaseService(DataSource dataSource) {
|
public DatabaseService(DataSource dataSource) {
|
||||||
this.dataSource = dataSource;
|
this.databaseMapper = new DatabaseMapperImpl(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 建库,指定 name
|
// 建库,指定 name
|
||||||
public int createDatabase(String database) {
|
public void createDatabase(String database) {
|
||||||
try {
|
databaseMapper.createDatabase(database);
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
statement.execute("create database " + database);
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
// return databaseMapper.createDatabase(database);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 建库,指定参数 keep,days,replica等
|
// 建库,指定参数 keep,days,replica等
|
||||||
public int createDatabase(Map<String, String> map) {
|
public void createDatabase(Map<String, String> map) {
|
||||||
if (map.isEmpty())
|
if (map.isEmpty())
|
||||||
return 0;
|
return;
|
||||||
if (map.containsKey("database") && map.size() == 1)
|
if (map.containsKey("database") && map.size() == 1) {
|
||||||
createDatabase(map.get("database"));
|
createDatabase(map.get("database"));
|
||||||
// return databaseMapper.createDatabase(map.get("database"));
|
return;
|
||||||
// return databaseMapper.createDatabaseWithParameters(map);
|
|
||||||
try {
|
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
String sql = "create database if not exists " + map.get("database")
|
|
||||||
+ " keep " + map.get("keep")
|
|
||||||
+ " days " + map.get("days")
|
|
||||||
+ " replica " + map.get("replica");
|
|
||||||
logger.info(">>> " + sql);
|
|
||||||
statement.execute(sql);
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
return 0;
|
databaseMapper.createDatabaseWithParameters(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
// drop database
|
// drop database
|
||||||
public int dropDatabase(String dbname) {
|
public void dropDatabase(String dbname) {
|
||||||
try {
|
databaseMapper.dropDatabase(dbname);
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
String sql = "drop database if exists " + dbname;
|
|
||||||
logger.info(">>> " + sql);
|
|
||||||
statement.execute(sql);
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// use database
|
// use database
|
||||||
public int useDatabase(String dbname) {
|
public void useDatabase(String dbname) {
|
||||||
try {
|
databaseMapper.useDatabase(dbname);
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
String sql = "use " + dbname;
|
|
||||||
logger.info(">>> " + sql);
|
|
||||||
statement.execute(sql);
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
// return databaseMapper.useDatabase(dbname);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ public class InsertTask implements Callable<Integer> {
|
||||||
SubTableValueGenerator.disrupt(data, rate, range);
|
SubTableValueGenerator.disrupt(data, rate, range);
|
||||||
}
|
}
|
||||||
// insert
|
// insert
|
||||||
SubTableService subTableService = new SubTableService(connection);
|
SubTableService subTableService = new SubTableService(dataSource);
|
||||||
if (autoCreateTable) {
|
if (autoCreateTable) {
|
||||||
subTableService.insertAutoCreateTable(data);
|
subTableService.insertAutoCreateTable(data);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,57 +1,25 @@
|
||||||
package com.taosdata.taosdemo.service;
|
package com.taosdata.taosdemo.service;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.SubTableMapper;
|
import com.taosdata.taosdemo.dao.SubTableMapper;
|
||||||
import com.taosdata.taosdemo.domain.*;
|
import com.taosdata.taosdemo.dao.SubTableMapperImpl;
|
||||||
|
import com.taosdata.taosdemo.domain.SubTableMeta;
|
||||||
|
import com.taosdata.taosdemo.domain.SubTableValue;
|
||||||
|
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||||
import com.taosdata.taosdemo.service.data.SubTableMetaGenerator;
|
import com.taosdata.taosdemo.service.data.SubTableMetaGenerator;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class SubTableService extends AbstractService {
|
public class SubTableService extends AbstractService {
|
||||||
|
|
||||||
private static Logger logger = Logger.getLogger(SubTableService.class);
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private SubTableMapper mapper;
|
private SubTableMapper mapper;
|
||||||
|
|
||||||
private Connection connection;
|
public SubTableService(DataSource datasource) {
|
||||||
|
this.mapper = new SubTableMapperImpl(datasource);
|
||||||
public SubTableService() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public SubTableService(Connection connection) {
|
|
||||||
this.connection = connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 1. 选择database,找到所有supertable
|
|
||||||
* 2. 选择supertable,可以拿到表结构,包括field和tag
|
|
||||||
* 3. 指定子表的前缀和个数
|
|
||||||
* 4. 指定创建子表的线程数
|
|
||||||
*/
|
|
||||||
//TODO:指定database、supertable、子表前缀、子表个数、线程数
|
|
||||||
|
|
||||||
// 多线程创建表,指定线程个数
|
|
||||||
public int createSubTable(List<SubTableMeta> subTables, int threadSize) {
|
|
||||||
ExecutorService executor = Executors.newFixedThreadPool(threadSize);
|
|
||||||
List<Future<Integer>> futureList = new ArrayList<>();
|
|
||||||
for (SubTableMeta subTableMeta : subTables) {
|
|
||||||
executor.submit(() -> createSubTable(subTableMeta));
|
|
||||||
}
|
|
||||||
executor.shutdown();
|
|
||||||
return getAffectRows(futureList);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createSubTable(SuperTableMeta superTableMeta, long numOfTables, String prefixOfTable, int numOfThreadsForCreate) {
|
public void createSubTable(SuperTableMeta superTableMeta, long numOfTables, String prefixOfTable, int numOfThreadsForCreate) {
|
||||||
|
@ -66,7 +34,7 @@ public class SubTableService extends AbstractService {
|
||||||
public void createSubTable(SuperTableMeta superTableMeta, String tableName) {
|
public void createSubTable(SuperTableMeta superTableMeta, String tableName) {
|
||||||
// 构造数据
|
// 构造数据
|
||||||
SubTableMeta meta = SubTableMetaGenerator.generate(superTableMeta, tableName);
|
SubTableMeta meta = SubTableMetaGenerator.generate(superTableMeta, tableName);
|
||||||
mapper.createUsingSuperTable(meta);
|
createSubTable(meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建一张子表,可以指定database,supertable,tablename,tag值
|
// 创建一张子表,可以指定database,supertable,tablename,tag值
|
||||||
|
@ -74,11 +42,6 @@ public class SubTableService extends AbstractService {
|
||||||
mapper.createUsingSuperTable(subTableMeta);
|
mapper.createUsingSuperTable(subTableMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单线程创建多张子表,每张子表分别可以指定自己的database,supertable,tablename,tag值
|
|
||||||
public int createSubTable(List<SubTableMeta> subTables) {
|
|
||||||
return createSubTable(subTables, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*************************************************************************************************************************/
|
/*************************************************************************************************************************/
|
||||||
// 插入:多线程,多表
|
// 插入:多线程,多表
|
||||||
public int insert(List<SubTableValue> subTableValues, int threadSize, int frequency) {
|
public int insert(List<SubTableValue> subTableValues, int threadSize, int frequency) {
|
||||||
|
@ -89,19 +52,6 @@ public class SubTableService extends AbstractService {
|
||||||
return getAffectRows(future);
|
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(),()...
|
// 插入:单表,insert into xxx values(),()...
|
||||||
public int insert(SubTableValue subTableValue) {
|
public int insert(SubTableValue subTableValue) {
|
||||||
return mapper.insertOneTableMultiValues(subTableValue);
|
return mapper.insertOneTableMultiValues(subTableValue);
|
||||||
|
@ -117,70 +67,9 @@ public class SubTableService extends AbstractService {
|
||||||
return mapper.insertOneTableMultiValuesUsingSuperTable(subTableValue);
|
return mapper.insertOneTableMultiValuesUsingSuperTable(subTableValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
// 插入:多表,自动建表, 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) {
|
||||||
|
return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues);
|
||||||
int affectRows = 0;
|
|
||||||
try {
|
|
||||||
String sql = sql(subTableValues);
|
|
||||||
logger.info(">>> SQL : " + sql);
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
affectRows = statement.executeUpdate(sql);
|
|
||||||
statement.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return affectRows;
|
|
||||||
// return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private String sql(List<SubTableValue> subTableValues) {
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("insert into ");
|
|
||||||
for (int i = 0; i < subTableValues.size(); i++) {
|
|
||||||
SubTableValue subTableValue = subTableValues.get(i);
|
|
||||||
sb.append(subTableValue.getDatabase() + "." + subTableValue.getName() + " using " + subTableValue.getDatabase() + "." + subTableValue.getSupertable() + " tags (");
|
|
||||||
for (int j = 0; j < subTableValue.getTags().size(); j++) {
|
|
||||||
TagValue tagValue = subTableValue.getTags().get(j);
|
|
||||||
if (j == 0)
|
|
||||||
sb.append("'" + tagValue.getValue() + "'");
|
|
||||||
else
|
|
||||||
sb.append(", '" + tagValue.getValue() + "'");
|
|
||||||
}
|
|
||||||
sb.append(") values");
|
|
||||||
for (int j = 0; j < subTableValue.getValues().size(); j++) {
|
|
||||||
sb.append("(");
|
|
||||||
RowValue rowValue = subTableValue.getValues().get(j);
|
|
||||||
for (int k = 0; k < rowValue.getFields().size(); k++) {
|
|
||||||
FieldValue fieldValue = rowValue.getFields().get(k);
|
|
||||||
if (k == 0)
|
|
||||||
// sb.append("" + timestamp.getAndIncrement());
|
|
||||||
sb.append("" + fieldValue.getValue() + "");
|
|
||||||
else
|
|
||||||
sb.append(", '" + fieldValue.getValue() + "'");
|
|
||||||
}
|
|
||||||
sb.append(") ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void sleep(int sleep) {
|
|
||||||
if (sleep <= 0)
|
|
||||||
return;
|
|
||||||
try {
|
|
||||||
TimeUnit.MILLISECONDS.sleep(sleep);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/********************************************************************/
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,45 +1,22 @@
|
||||||
package com.taosdata.taosdemo.service;
|
package com.taosdata.taosdemo.service;
|
||||||
|
|
||||||
import com.taosdata.taosdemo.dao.SuperTableMapper;
|
import com.taosdata.taosdemo.dao.SuperTableMapper;
|
||||||
|
import com.taosdata.taosdemo.dao.SuperTableMapperImpl;
|
||||||
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
import com.taosdata.taosdemo.domain.SuperTableMeta;
|
||||||
import com.taosdata.taosdemo.utils.SqlSpeller;
|
|
||||||
import org.apache.log4j.Logger;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class SuperTableService {
|
public class SuperTableService {
|
||||||
|
|
||||||
private static Logger logger = Logger.getLogger(SuperTableService.class);
|
|
||||||
@Autowired
|
|
||||||
private SuperTableMapper superTableMapper;
|
private SuperTableMapper superTableMapper;
|
||||||
|
|
||||||
private DataSource dataSource;
|
|
||||||
|
|
||||||
public SuperTableService(DataSource dataSource) {
|
public SuperTableService(DataSource dataSource) {
|
||||||
this.dataSource = dataSource;
|
this.superTableMapper = new SuperTableMapperImpl(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建超级表,指定每个field的名称和类型,每个tag的名称和类型
|
// 创建超级表,指定每个field的名称和类型,每个tag的名称和类型
|
||||||
public int create(SuperTableMeta superTableMeta) {
|
public void create(SuperTableMeta superTableMeta) {
|
||||||
int result = 0;
|
superTableMapper.createSuperTable(superTableMeta);
|
||||||
try {
|
|
||||||
Connection connection = dataSource.getConnection();
|
|
||||||
Statement statement = connection.createStatement();
|
|
||||||
String sql = SqlSpeller.createSuperTable(superTableMeta);
|
|
||||||
logger.info(">>> " + sql);
|
|
||||||
result = statement.executeUpdate(sql);
|
|
||||||
statement.close();
|
|
||||||
connection.close();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drop(String database, String name) {
|
public void drop(String database, String name) {
|
||||||
|
|
|
@ -9,6 +9,7 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SubTableServiceTest {
|
public class SubTableServiceTest {
|
||||||
|
|
||||||
private SubTableService service;
|
private SubTableService service;
|
||||||
|
|
||||||
private List<SubTableMeta> subTables;
|
private List<SubTableMeta> subTables;
|
||||||
|
@ -31,13 +32,11 @@ public class SubTableServiceTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateSubTable() {
|
public void testCreateSubTable() {
|
||||||
int count = service.createSubTable(subTables);
|
|
||||||
System.out.println("count >>> " + count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreateSubTableList() {
|
public void testCreateSubTableList() {
|
||||||
int count = service.createSubTable(subTables, 10);
|
|
||||||
System.out.println("count >>> " + count);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue