update JdbcTaosdemo
This commit is contained in:
parent
a5a4f7026f
commit
56c8889ee4
|
@ -4,30 +4,29 @@ import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||||
import com.taosdata.example.jdbcTaosdemo.task.CreateTableTask;
|
import com.taosdata.example.jdbcTaosdemo.task.CreateTableTask;
|
||||||
import com.taosdata.example.jdbcTaosdemo.task.InsertTableDatetimeTask;
|
import com.taosdata.example.jdbcTaosdemo.task.InsertTableDatetimeTask;
|
||||||
import com.taosdata.example.jdbcTaosdemo.task.InsertTableTask;
|
import com.taosdata.example.jdbcTaosdemo.task.InsertTableTask;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||||
import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil;
|
import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil;
|
||||||
import com.taosdata.jdbc.TSDBDriver;
|
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.ArrayList;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class JdbcTaosdemo {
|
public class JdbcTaosdemo {
|
||||||
|
|
||||||
private static Logger logger = Logger.getLogger(JdbcTaosdemo.class);
|
private static Logger logger = Logger.getLogger(JdbcTaosdemo.class);
|
||||||
private static AtomicLong beginTimestamp = new AtomicLong(TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000"));
|
|
||||||
private final JdbcTaosdemoConfig config;
|
private final JdbcTaosdemoConfig config;
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
private static final String[] locations = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "HangZhou", "Tianjin", "Wuhan", "Changsha", "Nanjing", "Xian"};
|
|
||||||
private static Random random = new Random(System.currentTimeMillis());
|
|
||||||
|
|
||||||
public JdbcTaosdemo(JdbcTaosdemoConfig config) {
|
public JdbcTaosdemo(JdbcTaosdemoConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
JdbcTaosdemoConfig config = JdbcTaosdemoConfig.build(args);
|
|
||||||
|
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
|
||||||
|
|
||||||
boolean isHelp = Arrays.asList(args).contains("--help");
|
boolean isHelp = Arrays.asList(args).contains("--help");
|
||||||
if (isHelp) {
|
if (isHelp) {
|
||||||
|
@ -67,7 +66,7 @@ public class JdbcTaosdemo {
|
||||||
private void init() {
|
private void init() {
|
||||||
try {
|
try {
|
||||||
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
Class.forName("com.taosdata.jdbc.TSDBDriver");
|
||||||
connection = getConnection(config);
|
connection = ConnectionFactory.build(config);
|
||||||
if (connection != null)
|
if (connection != null)
|
||||||
logger.info("[ OK ] Connection established.");
|
logger.info("[ OK ] Connection established.");
|
||||||
} catch (ClassNotFoundException | SQLException e) {
|
} catch (ClassNotFoundException | SQLException e) {
|
||||||
|
@ -76,27 +75,19 @@ public class JdbcTaosdemo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Connection getConnection(JdbcTaosdemoConfig config) throws SQLException {
|
|
||||||
Properties properties = new Properties();
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, config.getHost());
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_USER, config.getUser());
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, config.getPassword());
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
|
||||||
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
|
||||||
return DriverManager.getConnection("jdbc:TAOS://" + config.getHost() + ":" + config.getPort() + "/" + config.getDbName() + "", properties);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create database
|
* create database
|
||||||
*/
|
*/
|
||||||
private void createDatabase() {
|
private void createDatabase() {
|
||||||
String sql = "create database if not exists " + config.getDbName() + " keep " + config.getKeep() + " days " + config.getDays();
|
String sql = SqlSpeller.createDatabaseSQL(config.getDbName(), config.getKeep(), config.getDays());
|
||||||
execute(sql);
|
execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* drop database
|
||||||
|
*/
|
||||||
private void dropDatabase() {
|
private void dropDatabase() {
|
||||||
String sql = "drop database if exists " + config.getDbName();
|
String sql = SqlSpeller.dropDatabaseSQL(config.getDbName());
|
||||||
execute(sql);
|
execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -104,12 +95,15 @@ public class JdbcTaosdemo {
|
||||||
* use database
|
* use database
|
||||||
*/
|
*/
|
||||||
private void useDatabase() {
|
private void useDatabase() {
|
||||||
String sql = "use " + config.getDbName();
|
String sql = SqlSpeller.useDatabaseSQL(config.getDbName());
|
||||||
execute(sql);
|
execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* create super table
|
||||||
|
*/
|
||||||
private void createSuperTable() {
|
private void createSuperTable() {
|
||||||
String sql = "create table if not exists " + config.getStbName() + "(ts timestamp, current float, voltage int, phase float) tags(location binary(64), groupId int)";
|
String sql = SqlSpeller.createSuperTableSQL(config.getStbName());
|
||||||
execute(sql);
|
execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,13 +122,16 @@ public class JdbcTaosdemo {
|
||||||
for (Thread thread : threads) {
|
for (Thread thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
logger.info(">>> Multi Threads create table finished.");
|
logger.info("<<< Multi Threads create table finished.");
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.error(e.getMessage());
|
logger.error(e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* insert data infinitely
|
||||||
|
*/
|
||||||
private void insertInfinite() {
|
private void insertInfinite() {
|
||||||
try {
|
try {
|
||||||
final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
|
final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
|
||||||
|
@ -150,7 +147,7 @@ public class JdbcTaosdemo {
|
||||||
for (Thread thread : threads) {
|
for (Thread thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
logger.info(">>> Multi Threads insert table finished.");
|
logger.info("<<< Multi Threads insert table finished.");
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.error(e.getMessage());
|
logger.error(e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -170,48 +167,13 @@ public class JdbcTaosdemo {
|
||||||
for (Thread thread : threads) {
|
for (Thread thread : threads) {
|
||||||
thread.join();
|
thread.join();
|
||||||
}
|
}
|
||||||
logger.info(">>> Multi Threads insert table finished.");
|
logger.info("<<< Multi Threads insert table finished.");
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
logger.error(e.getMessage());
|
logger.error(e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String insertSql(int tableIndex, JdbcTaosdemoConfig config) {
|
|
||||||
float current = 10 + random.nextFloat();
|
|
||||||
int voltage = 200 + random.nextInt(20);
|
|
||||||
float phase = random.nextFloat();
|
|
||||||
String sql = "insert into " + config.getDbName() + "." + config.getTbPrefix() + "" + tableIndex + " " +
|
|
||||||
"values(" + beginTimestamp.getAndIncrement() + ", " + current + ", " + voltage + ", " + phase + ") ";
|
|
||||||
return sql;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String insertSql(int tableIndex, long ts, JdbcTaosdemoConfig config) {
|
|
||||||
float current = 10 + random.nextFloat();
|
|
||||||
int voltage = 200 + random.nextInt(20);
|
|
||||||
float phase = random.nextFloat();
|
|
||||||
String sql = "insert into " + config.getDbName() + "." + config.getTbPrefix() + "" + tableIndex + " " +
|
|
||||||
"values(" + ts + ", " + current + ", " + voltage + ", " + phase + ") ";
|
|
||||||
return sql;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String batchInsertSql(int tableIndex, long ts, int valueCnt, JdbcTaosdemoConfig config) {
|
|
||||||
float current = 10 + random.nextFloat();
|
|
||||||
int voltage = 200 + random.nextInt(20);
|
|
||||||
float phase = random.nextFloat();
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("insert into " + config.getDbName() + "." + config.getTbPrefix() + "" + tableIndex + " " + "values");
|
|
||||||
for (int i = 0; i < valueCnt; i++) {
|
|
||||||
sb.append("(" + (ts + i) + ", " + current + ", " + voltage + ", " + phase + ") ");
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String createTableSql(int tableIndex, JdbcTaosdemoConfig config) {
|
|
||||||
String location = locations[random.nextInt(locations.length)];
|
|
||||||
return "create table d" + tableIndex + " using " + config.getDbName() + "." + config.getStbName() + " tags('" + location + "'," + tableIndex + ")";
|
|
||||||
}
|
|
||||||
|
|
||||||
private void countFromSuperTable() {
|
private void countFromSuperTable() {
|
||||||
String sql = "select count(*) from " + config.getDbName() + "." + config.getStbName();
|
String sql = "select count(*) from " + config.getDbName() + "." + config.getStbName();
|
||||||
executeQuery(sql);
|
executeQuery(sql);
|
||||||
|
@ -233,7 +195,7 @@ public class JdbcTaosdemo {
|
||||||
* drop super table
|
* drop super table
|
||||||
*/
|
*/
|
||||||
private void dropSuperTable() {
|
private void dropSuperTable() {
|
||||||
String sql = "drop table if exists " + config.getDbName() + "." + config.getStbName();
|
String sql = SqlSpeller.dropSuperTableSQL(config.getDbName(), config.getStbName());
|
||||||
execute(sql);
|
execute(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package com.taosdata.example.jdbcTaosdemo.domain;
|
package com.taosdata.example.jdbcTaosdemo.domain;
|
||||||
|
|
||||||
public class JdbcTaosdemoConfig {
|
public final class JdbcTaosdemoConfig {
|
||||||
|
|
||||||
//The host to connect to TDengine. Must insert one
|
//The host to connect to TDengine. Must insert one
|
||||||
private String host;
|
private String host;
|
||||||
|
@ -10,37 +10,45 @@ public class JdbcTaosdemoConfig {
|
||||||
private String user = "root";
|
private String user = "root";
|
||||||
//The password to use when connecting to the server. Default is 'taosdata'
|
//The password to use when connecting to the server. Default is 'taosdata'
|
||||||
private String password = "taosdata";
|
private String password = "taosdata";
|
||||||
|
|
||||||
//Destination database. Default is 'test'
|
//Destination database. Default is 'test'
|
||||||
private String dbName = "test";
|
private String dbName = "test";
|
||||||
//keep
|
//keep
|
||||||
private int keep = 365 * 20;
|
private int keep = 365 * 20;
|
||||||
//
|
//days
|
||||||
private int days = 30;
|
private int days = 30;
|
||||||
|
|
||||||
//Super table Name. Default is 'meters'
|
//Super table Name. Default is 'meters'
|
||||||
private String stbName = "meters";
|
private String stbName = "meters";
|
||||||
//Table name prefix. Default is 'd'
|
//Table name prefix. Default is 'd'
|
||||||
private String tbPrefix = "d";
|
private String tbPrefix = "d";
|
||||||
//The number of threads. Default is 10.
|
//The number of tables. Default is 10.
|
||||||
private int numberOfThreads = 10;
|
private int numberOfTable = 10;
|
||||||
//The number of tables. Default is 10000.
|
//The number of records per table. Default is 2
|
||||||
private int numberOfTable = 10000;
|
private int numberOfRecordsPerTable = 2;
|
||||||
//The number of records per table. Default is 100000
|
//The number of records per request. Default is 100
|
||||||
private int numberOfRecordsPerTable = 100000;
|
private int numberOfRecordsPerRequest = 100;
|
||||||
|
|
||||||
|
//The number of threads. Default is 1.
|
||||||
|
private int numberOfThreads = 1;
|
||||||
//Delete data. Default is false
|
//Delete data. Default is false
|
||||||
private boolean deleteTable = true;
|
private boolean deleteTable = false;
|
||||||
|
|
||||||
public static void printHelp() {
|
public static void printHelp() {
|
||||||
System.out.println("Usage: java -jar JDBCConnectorChecker.jar -h host [OPTION...]");
|
System.out.println("Usage: java -jar JDBCConnectorChecker.jar [OPTION...]");
|
||||||
|
System.out.println("-h host The host to connect to TDengine. you must input one");
|
||||||
System.out.println("-p port The TCP/IP port number to use for the connection. Default is 6030");
|
System.out.println("-p port The TCP/IP port number to use for the connection. Default is 6030");
|
||||||
System.out.println("-u user The TDengine user name to use when connecting to the server. Default is 'root'");
|
System.out.println("-u user The TDengine user name to use when connecting to the server. Default is 'root'");
|
||||||
System.out.println("-P password The password to use when connecting to the server.Default is 'taosdata'");
|
System.out.println("-P password The password to use when connecting to the server.Default is 'taosdata'");
|
||||||
System.out.println("-d database Destination database. Default is 'test'");
|
System.out.println("-d database Destination database. Default is 'test'");
|
||||||
System.out.println("-m tablePrefix Table prefix name. Default is 'd'");
|
System.out.println("-m tablePrefix Table prefix name. Default is 'd'");
|
||||||
System.out.println("-T num_of_threads The number of threads. Default is 10");
|
System.out.println("-t num_of_tables The number of tables. Default is 10");
|
||||||
System.out.println("-t num_of_tables The number of tables. Default is 10000");
|
System.out.println("-n num_of_records_per_table The number of records per table. Default is 2");
|
||||||
System.out.println("-n num_of_records_per_table The number of records per table. Default is 100000");
|
System.out.println("-r num_of_records_per_req The number of records per request. Default is 100");
|
||||||
|
System.out.println("-T num_of_threads The number of threads. Default is 1");
|
||||||
System.out.println("-D delete table Delete data methods. Default is false");
|
System.out.println("-D delete table Delete data methods. Default is false");
|
||||||
System.out.println("--help Give this help list");
|
System.out.println("--help Give this help list");
|
||||||
|
// System.out.println("--infinite infinite insert mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -49,146 +57,97 @@ public class JdbcTaosdemoConfig {
|
||||||
* @param args command line args
|
* @param args command line args
|
||||||
* @return JdbcTaosdemoConfig
|
* @return JdbcTaosdemoConfig
|
||||||
*/
|
*/
|
||||||
public static JdbcTaosdemoConfig build(String[] args) {
|
public JdbcTaosdemoConfig(String[] args) {
|
||||||
|
|
||||||
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig();
|
|
||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
if ("-h".equals(args[i]) && i < args.length - 1) {
|
if ("-h".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setHost(args[++i]);
|
host = args[++i];
|
||||||
}
|
}
|
||||||
if ("-p".equals(args[i]) && i < args.length - 1) {
|
if ("-p".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setPort(Integer.parseInt(args[++i]));
|
port = Integer.parseInt(args[++i]);
|
||||||
}
|
}
|
||||||
if ("-u".equals(args[i]) && i < args.length - 1) {
|
if ("-u".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setUser(args[++i]);
|
user = args[++i];
|
||||||
}
|
}
|
||||||
if ("-P".equals(args[i]) && i < args.length - 1) {
|
if ("-P".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setPassword(args[++i]);
|
password = args[++i];
|
||||||
}
|
}
|
||||||
if ("-d".equals(args[i]) && i < args.length - 1) {
|
if ("-d".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setDbName(args[++i]);
|
dbName = args[++i];
|
||||||
}
|
}
|
||||||
if ("-m".equals(args[i]) && i < args.length - 1) {
|
if ("-m".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setTbPrefix(args[++i]);
|
tbPrefix = args[++i];
|
||||||
}
|
|
||||||
if ("-T".equals(args[i]) && i < args.length - 1) {
|
|
||||||
config.setNumberOfThreads(Integer.parseInt(args[++i]));
|
|
||||||
}
|
}
|
||||||
if ("-t".equals(args[i]) && i < args.length - 1) {
|
if ("-t".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setNumberOfTable(Integer.parseInt(args[++i]));
|
numberOfTable = Integer.parseInt(args[++i]);
|
||||||
}
|
}
|
||||||
if ("-n".equals(args[i]) && i < args.length - 1) {
|
if ("-n".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setNumberOfRecordsPerTable(Integer.parseInt(args[++i]));
|
numberOfRecordsPerTable = Integer.parseInt(args[++i]);
|
||||||
|
}
|
||||||
|
if ("-r".equals(args[i]) && i < args.length - 1) {
|
||||||
|
numberOfRecordsPerRequest = Integer.parseInt(args[++i]);
|
||||||
|
}
|
||||||
|
if ("-T".equals(args[i]) && i < args.length - 1) {
|
||||||
|
numberOfThreads = Integer.parseInt(args[++i]);
|
||||||
}
|
}
|
||||||
if ("-D".equals(args[i]) && i < args.length - 1) {
|
if ("-D".equals(args[i]) && i < args.length - 1) {
|
||||||
config.setDeleteTable(Boolean.parseBoolean(args[++i]));
|
deleteTable = Boolean.parseBoolean(args[++i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHost(String host) {
|
|
||||||
this.host = host;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getHost() {
|
public String getHost() {
|
||||||
return host;
|
return host;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDbName() {
|
|
||||||
return dbName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDbName(String dbName) {
|
|
||||||
this.dbName = dbName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getPort() {
|
public int getPort() {
|
||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPort(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUser() {
|
public String getUser() {
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUser(String user) {
|
|
||||||
this.user = user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) {
|
public String getDbName() {
|
||||||
this.password = password;
|
return dbName;
|
||||||
}
|
|
||||||
|
|
||||||
public String getStbName() {
|
|
||||||
return stbName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStbName(String stbName) {
|
|
||||||
this.stbName = stbName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTbPrefix() {
|
|
||||||
return tbPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTbPrefix(String tbPrefix) {
|
|
||||||
this.tbPrefix = tbPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getNumberOfThreads() {
|
|
||||||
return numberOfThreads;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNumberOfThreads(int numberOfThreads) {
|
|
||||||
this.numberOfThreads = numberOfThreads;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getNumberOfTable() {
|
|
||||||
return numberOfTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNumberOfTable(int numberOfTable) {
|
|
||||||
this.numberOfTable = numberOfTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getNumberOfRecordsPerTable() {
|
|
||||||
return numberOfRecordsPerTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNumberOfRecordsPerTable(int numberOfRecordsPerTable) {
|
|
||||||
this.numberOfRecordsPerTable = numberOfRecordsPerTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isDeleteTable() {
|
|
||||||
return deleteTable;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeleteTable(boolean deleteTable) {
|
|
||||||
this.deleteTable = deleteTable;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getKeep() {
|
public int getKeep() {
|
||||||
return keep;
|
return keep;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKeep(int keep) {
|
|
||||||
this.keep = keep;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getDays() {
|
public int getDays() {
|
||||||
return days;
|
return days;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDays(int days) {
|
public String getStbName() {
|
||||||
this.days = days;
|
return stbName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTbPrefix() {
|
||||||
|
return tbPrefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfTable() {
|
||||||
|
return numberOfTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfRecordsPerTable() {
|
||||||
|
return numberOfRecordsPerTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfThreads() {
|
||||||
|
return numberOfThreads;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isDeleteTable() {
|
||||||
|
return deleteTable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getNumberOfRecordsPerRequest() {
|
||||||
|
return numberOfRecordsPerRequest;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package com.taosdata.example.jdbcTaosdemo.task;
|
package com.taosdata.example.jdbcTaosdemo.task;
|
||||||
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
@ -24,14 +25,11 @@ public class CreateTableTask implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Connection connection = JdbcTaosdemo.getConnection(config);
|
Connection connection = ConnectionFactory.build(config);
|
||||||
for (int i = startIndex; i < startIndex + tableNumber; i++) {
|
for (int i = startIndex; i < startIndex + tableNumber; i++) {
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
String sql = JdbcTaosdemo.createTableSql(i + 1, config);
|
String sql = SqlSpeller.createTableSQL(i + 1, config.getDbName(), config.getStbName());
|
||||||
// long start = System.currentTimeMillis();
|
statement.execute(sql);
|
||||||
boolean execute = statement.execute(sql);
|
|
||||||
// long end = System.currentTimeMillis();
|
|
||||||
// printSql(sql, execute, (end - start));
|
|
||||||
statement.close();
|
statement.close();
|
||||||
logger.info(">>> " + sql);
|
logger.info(">>> " + sql);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
package com.taosdata.example.jdbcTaosdemo.task;
|
package com.taosdata.example.jdbcTaosdemo.task;
|
||||||
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
@ -28,13 +29,11 @@ public class InsertTableDatetimeTask implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Connection connection = JdbcTaosdemo.getConnection(config);
|
Connection connection = ConnectionFactory.build(config);
|
||||||
int valueCnt = 100;
|
int valuesCount = config.getNumberOfRecordsPerRequest();
|
||||||
for (long ts = startDatetime; ts < finishedDatetime; ts+= valueCnt) {
|
for (long ts = startDatetime; ts < finishedDatetime; ts += valuesCount) {
|
||||||
for (int i = startTableIndex; i < startTableIndex + tableNumber; i++) {
|
for (int i = startTableIndex; i < startTableIndex + tableNumber; i++) {
|
||||||
// String sql = JdbcTaosdemo.insertSql(i + 1, ts, config);
|
String sql = SqlSpeller.insertBatchSizeRowsSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts, valuesCount);
|
||||||
|
|
||||||
String sql = JdbcTaosdemo.batchInsertSql(i + 1, ts, valueCnt, config);
|
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
statement.execute(sql);
|
statement.execute(sql);
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
|
@ -1,15 +1,19 @@
|
||||||
package com.taosdata.example.jdbcTaosdemo.task;
|
package com.taosdata.example.jdbcTaosdemo.task;
|
||||||
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
|
|
||||||
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.ConnectionFactory;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.SqlSpeller;
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.utils.TimeStampUtil;
|
||||||
import org.apache.log4j.Logger;
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
public class InsertTableTask implements Runnable {
|
public class InsertTableTask implements Runnable {
|
||||||
private static final Logger logger = Logger.getLogger(InsertTableTask.class);
|
private static final Logger logger = Logger.getLogger(InsertTableTask.class);
|
||||||
|
private static AtomicLong beginTimestamp = new AtomicLong(TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000"));
|
||||||
|
|
||||||
private final JdbcTaosdemoConfig config;
|
private final JdbcTaosdemoConfig config;
|
||||||
private final int startIndex;
|
private final int startIndex;
|
||||||
|
@ -26,10 +30,13 @@ public class InsertTableTask implements Runnable {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
Connection connection = JdbcTaosdemo.getConnection(config);
|
Connection connection = ConnectionFactory.build(config);
|
||||||
for (int i = startIndex; i < startIndex + tableNumber; i++) {
|
// iterate insert
|
||||||
for (int j = 0; j < recordsNumber; j++) {
|
for (int j = 0; j < recordsNumber; j++) {
|
||||||
String sql = JdbcTaosdemo.insertSql(i + 1, config);
|
long ts = beginTimestamp.getAndIncrement();
|
||||||
|
// insert data into echo table
|
||||||
|
for (int i = startIndex; i < startIndex + tableNumber; i++) {
|
||||||
|
String sql = SqlSpeller.insertOneRowSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts);
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
statement.execute(sql);
|
statement.execute(sql);
|
||||||
statement.close();
|
statement.close();
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||||
|
|
||||||
|
import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
|
||||||
|
import com.taosdata.jdbc.TSDBDriver;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
public class ConnectionFactory {
|
||||||
|
|
||||||
|
public static Connection build(JdbcTaosdemoConfig config) throws SQLException {
|
||||||
|
return build(config.getHost(), config.getPort(), config.getDbName(), config.getUser(), config.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Connection build(String host, int port, String dbName) throws SQLException {
|
||||||
|
return build(host, port, dbName, "root", "taosdata");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Connection build(String host, int port, String dbName, String user, String password) throws SQLException {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.setProperty(TSDBDriver.PROPERTY_KEY_USER, user);
|
||||||
|
properties.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, password);
|
||||||
|
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
|
||||||
|
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
|
||||||
|
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
|
||||||
|
return DriverManager.getConnection("jdbc:TAOS://" + host + ":" + port + "/" + dbName + "", properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,58 @@
|
||||||
|
package com.taosdata.example.jdbcTaosdemo.utils;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class SqlSpeller {
|
||||||
|
private static final Random random = new Random(System.currentTimeMillis());
|
||||||
|
private static final String[] locations = {
|
||||||
|
"Beijing", "Shanghai", "Guangzhou", "Shenzhen",
|
||||||
|
"HangZhou", "Tianjin", "Wuhan", "Changsha", "Nanjing", "Xian"
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String createDatabaseSQL(String dbName, int keep, int days) {
|
||||||
|
return "create database if not exists " + dbName + " keep " + keep + " days " + days;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String dropDatabaseSQL(String dbName) {
|
||||||
|
return "drop database if exists " + dbName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String useDatabaseSQL(String dbName) {
|
||||||
|
return "use " + dbName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String createSuperTableSQL(String superTableName) {
|
||||||
|
return "create table if not exists " + superTableName + "(ts timestamp, current float, voltage int, phase float) tags(location binary(64), groupId int)";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String dropSuperTableSQL(String dbName, String superTableName) {
|
||||||
|
return "drop table if exists " + dbName + "." + superTableName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String createTableSQL(int tableIndex, String dbName, String superTableName) {
|
||||||
|
String location = locations[random.nextInt(locations.length)];
|
||||||
|
return "create table d" + tableIndex + " using " + dbName + "." + superTableName + " tags('" + location + "'," + tableIndex + ")";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String insertOneRowSQL(String dbName, String tbPrefix, int tableIndex, long ts) {
|
||||||
|
float current = 10 + random.nextFloat();
|
||||||
|
int voltage = 200 + random.nextInt(20);
|
||||||
|
float phase = random.nextFloat();
|
||||||
|
String sql = "insert into " + dbName + "." + tbPrefix + "" + tableIndex + " " + "values(" + ts + ", " + current + ", " + voltage + ", " + phase + ")";
|
||||||
|
return sql;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String insertBatchSizeRowsSQL(String dbName, String tbPrefix, int tbIndex, long ts, int valuesCount) {
|
||||||
|
float current = 10 + random.nextFloat();
|
||||||
|
int voltage = 200 + random.nextInt(20);
|
||||||
|
float phase = random.nextFloat();
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("insert into " + dbName + "." + tbPrefix + "" + tbIndex + " " + "values");
|
||||||
|
for (int i = 0; i < valuesCount; i++) {
|
||||||
|
sb.append("(" + (ts + i) + ", " + current + ", " + voltage + ", " + phase + ") ");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -1,11 +1,10 @@
|
||||||
### 设置###
|
### 设置###
|
||||||
#log4j.rootLogger=debug,stdout,DebugLog,ErrorLog
|
log4j.rootLogger=debug,stdout,DebugLog,ErrorLog
|
||||||
log4j.rootLogger=debug,DebugLog,ErrorLog
|
|
||||||
### 输出信息到控制抬 ###
|
### 输出信息到控制抬 ###
|
||||||
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||||
#log4j.appender.stdout.Target=System.out
|
log4j.appender.stdout.Target=System.out
|
||||||
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||||
#log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
|
log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
|
||||||
### 输出DEBUG 级别以上的日志到=logs/error.log ###
|
### 输出DEBUG 级别以上的日志到=logs/error.log ###
|
||||||
log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender
|
log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender
|
||||||
log4j.appender.DebugLog.File=logs/debug.log
|
log4j.appender.DebugLog.File=logs/debug.log
|
||||||
|
|
Loading…
Reference in New Issue