未验证 提交 8e75b9e0 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #3679 from taosdata/feature/TD-1572

update JdbcTaosdemo
......@@ -4,30 +4,29 @@ import com.taosdata.example.jdbcTaosdemo.domain.JdbcTaosdemoConfig;
import com.taosdata.example.jdbcTaosdemo.task.CreateTableTask;
import com.taosdata.example.jdbcTaosdemo.task.InsertTableDatetimeTask;
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.jdbc.TSDBDriver;
import org.apache.log4j.Logger;
import java.sql.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class JdbcTaosdemo {
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 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) {
this.config = config;
}
public static void main(String[] args) {
JdbcTaosdemoConfig config = JdbcTaosdemoConfig.build(args);
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
boolean isHelp = Arrays.asList(args).contains("--help");
if (isHelp) {
......@@ -67,7 +66,7 @@ public class JdbcTaosdemo {
private void init() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
connection = getConnection(config);
connection = ConnectionFactory.build(config);
if (connection != null)
logger.info("[ OK ] Connection established.");
} 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
*/
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);
}
/**
* drop database
*/
private void dropDatabase() {
String sql = "drop database if exists " + config.getDbName();
String sql = SqlSpeller.dropDatabaseSQL(config.getDbName());
execute(sql);
}
......@@ -104,12 +95,15 @@ public class JdbcTaosdemo {
* use database
*/
private void useDatabase() {
String sql = "use " + config.getDbName();
String sql = SqlSpeller.useDatabaseSQL(config.getDbName());
execute(sql);
}
/**
* create super table
*/
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);
}
......@@ -128,13 +122,16 @@ public class JdbcTaosdemo {
for (Thread thread : threads) {
thread.join();
}
logger.info(">>> Multi Threads create table finished.");
logger.info("<<< Multi Threads create table finished.");
} catch (InterruptedException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
/**
* insert data infinitely
*/
private void insertInfinite() {
try {
final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
......@@ -150,7 +147,7 @@ public class JdbcTaosdemo {
for (Thread thread : threads) {
thread.join();
}
logger.info(">>> Multi Threads insert table finished.");
logger.info("<<< Multi Threads insert table finished.");
} catch (InterruptedException e) {
logger.error(e.getMessage());
e.printStackTrace();
......@@ -170,48 +167,13 @@ public class JdbcTaosdemo {
for (Thread thread : threads) {
thread.join();
}
logger.info(">>> Multi Threads insert table finished.");
logger.info("<<< Multi Threads insert table finished.");
} catch (InterruptedException e) {
logger.error(e.getMessage());
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() {
String sql = "select count(*) from " + config.getDbName() + "." + config.getStbName();
executeQuery(sql);
......@@ -233,7 +195,7 @@ public class JdbcTaosdemo {
* drop super table
*/
private void dropSuperTable() {
String sql = "drop table if exists " + config.getDbName() + "." + config.getStbName();
String sql = SqlSpeller.dropSuperTableSQL(config.getDbName(), config.getStbName());
execute(sql);
}
......
package com.taosdata.example.jdbcTaosdemo.domain;
public class JdbcTaosdemoConfig {
public final class JdbcTaosdemoConfig {
//The host to connect to TDengine. Must insert one
private String host;
......@@ -10,37 +10,45 @@ public class JdbcTaosdemoConfig {
private String user = "root";
//The password to use when connecting to the server. Default is 'taosdata'
private String password = "taosdata";
//Destination database. Default is 'test'
private String dbName = "test";
//keep
private int keep = 365 * 20;
//
//days
private int days = 30;
//Super table Name. Default is 'meters'
private String stbName = "meters";
//Table name prefix. Default is 'd'
private String tbPrefix = "d";
//The number of threads. Default is 10.
private int numberOfThreads = 10;
//The number of tables. Default is 10000.
private int numberOfTable = 10000;
//The number of records per table. Default is 100000
private int numberOfRecordsPerTable = 100000;
//The number of tables. Default is 10.
private int numberOfTable = 10;
//The number of records per table. Default is 2
private int numberOfRecordsPerTable = 2;
//The number of records per request. Default is 100
private int numberOfRecordsPerRequest = 100;
//The number of threads. Default is 1.
private int numberOfThreads = 1;
//Delete data. Default is false
private boolean deleteTable = true;
private boolean deleteTable = false;
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("-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("-d database Destination database. Default is 'test'");
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 10000");
System.out.println("-n num_of_records_per_table The number of records per table. Default is 100000");
System.out.println("-t num_of_tables The number of tables. Default is 10");
System.out.println("-n num_of_records_per_table The number of records per table. Default is 2");
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("--help Give this help list");
// System.out.println("--infinite infinite insert mode");
}
/**
......@@ -49,146 +57,97 @@ public class JdbcTaosdemoConfig {
* @param args command line args
* @return JdbcTaosdemoConfig
*/
public static JdbcTaosdemoConfig build(String[] args) {
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig();
public JdbcTaosdemoConfig(String[] args) {
for (int i = 0; i < args.length; i++) {
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) {
config.setPort(Integer.parseInt(args[++i]));
port = Integer.parseInt(args[++i]);
}
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) {
config.setPassword(args[++i]);
password = args[++i];
}
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) {
config.setTbPrefix(args[++i]);
}
if ("-T".equals(args[i]) && i < args.length - 1) {
config.setNumberOfThreads(Integer.parseInt(args[++i]));
tbPrefix = args[++i];
}
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) {
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) {
config.setDeleteTable(Boolean.parseBoolean(args[++i]));
deleteTable = Boolean.parseBoolean(args[++i]);
}
}
return config;
}
public void setHost(String host) {
this.host = host;
}
public String getHost() {
return host;
}
public String getDbName() {
return dbName;
}
public void setDbName(String dbName) {
this.dbName = dbName;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getStbName() {
return stbName;
}
public void setStbName(String stbName) {
this.stbName = stbName;
public String getDbName() {
return dbName;
}
public String getTbPrefix() {
return tbPrefix;
public int getKeep() {
return keep;
}
public void setTbPrefix(String tbPrefix) {
this.tbPrefix = tbPrefix;
public int getDays() {
return days;
}
public int getNumberOfThreads() {
return numberOfThreads;
public String getStbName() {
return stbName;
}
public void setNumberOfThreads(int numberOfThreads) {
this.numberOfThreads = numberOfThreads;
public String getTbPrefix() {
return tbPrefix;
}
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 int getNumberOfThreads() {
return numberOfThreads;
}
public boolean isDeleteTable() {
return deleteTable;
}
public void setDeleteTable(boolean deleteTable) {
this.deleteTable = deleteTable;
}
public int getKeep() {
return keep;
}
public void setKeep(int keep) {
this.keep = keep;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
public int getNumberOfRecordsPerRequest() {
return numberOfRecordsPerRequest;
}
}
package com.taosdata.example.jdbcTaosdemo.task;
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
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 java.sql.Connection;
......@@ -24,14 +25,11 @@ public class CreateTableTask implements Runnable {
@Override
public void run() {
try {
Connection connection = JdbcTaosdemo.getConnection(config);
Connection connection = ConnectionFactory.build(config);
for (int i = startIndex; i < startIndex + tableNumber; i++) {
Statement statement = connection.createStatement();
String sql = JdbcTaosdemo.createTableSql(i + 1, config);
// long start = System.currentTimeMillis();
boolean execute = statement.execute(sql);
// long end = System.currentTimeMillis();
// printSql(sql, execute, (end - start));
String sql = SqlSpeller.createTableSQL(i + 1, config.getDbName(), config.getStbName());
statement.execute(sql);
statement.close();
logger.info(">>> " + sql);
}
......
package com.taosdata.example.jdbcTaosdemo.task;
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
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 java.sql.Connection;
......@@ -28,13 +29,11 @@ public class InsertTableDatetimeTask implements Runnable {
@Override
public void run() {
try {
Connection connection = JdbcTaosdemo.getConnection(config);
int valueCnt = 100;
for (long ts = startDatetime; ts < finishedDatetime; ts+= valueCnt) {
Connection connection = ConnectionFactory.build(config);
int valuesCount = config.getNumberOfRecordsPerRequest();
for (long ts = startDatetime; ts < finishedDatetime; ts += valuesCount) {
for (int i = startTableIndex; i < startTableIndex + tableNumber; i++) {
// String sql = JdbcTaosdemo.insertSql(i + 1, ts, config);
String sql = JdbcTaosdemo.batchInsertSql(i + 1, ts, valueCnt, config);
String sql = SqlSpeller.insertBatchSizeRowsSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts, valuesCount);
Statement statement = connection.createStatement();
statement.execute(sql);
statement.close();
......
package com.taosdata.example.jdbcTaosdemo.task;
import com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo;
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 java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.atomic.AtomicLong;
public class InsertTableTask implements Runnable {
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 int startIndex;
......@@ -26,10 +30,13 @@ public class InsertTableTask implements Runnable {
@Override
public void run() {
try {
Connection connection = JdbcTaosdemo.getConnection(config);
for (int i = startIndex; i < startIndex + tableNumber; i++) {
for (int j = 0; j < recordsNumber; j++) {
String sql = JdbcTaosdemo.insertSql(i + 1, config);
Connection connection = ConnectionFactory.build(config);
// iterate insert
for (int j = 0; j < recordsNumber; j++) {
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.execute(sql);
statement.close();
......
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);
}
}
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();
}
}
\ No newline at end of file
### 设置###
#log4j.rootLogger=debug,stdout,DebugLog,ErrorLog
log4j.rootLogger=debug,DebugLog,ErrorLog
log4j.rootLogger=debug,stdout,DebugLog,ErrorLog
### 输出信息到控制抬 ###
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.out
#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=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
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
### 输出DEBUG 级别以上的日志到=logs/error.log ###
log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugLog.File=logs/debug.log
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册