提交 4149545b 编写于 作者: Z zyyang

[TD-1572]<feature>: A JdbcTaosdemo is supported now.

上级 3deb4591
......@@ -63,7 +63,12 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.4</version>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
package com.taosdata.example;
import com.taosdata.example.domain.JdbcTaosdemoConfig;
import com.taosdata.example.task.CreateTableTask;
import com.taosdata.example.task.InsertTableDatetimeTask;
import com.taosdata.example.task.InsertTableTask;
import com.taosdata.example.utils.TimeStampUtil;
import com.taosdata.jdbc.TSDBDriver;
import org.apache.log4j.Logger;
import java.sql.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicLong;
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;
}
private static void printHelp() {
System.out.println("Usage: java -jar JDBCConnectorChecker.jar -h host [OPTION...]");
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("-D delete table Delete data methods. Default is false");
System.out.println("--help Give this help list");
}
public static void main(String[] args) {
JdbcTaosdemoConfig config = JdbcTaosdemoConfig.build(args);
boolean isHelp = Arrays.asList(args).contains("--help");
if (isHelp) {
printHelp();
return;
}
if (config.getHost() == null) {
printHelp();
return;
}
boolean infinite = Arrays.asList().contains("--infinite");
JdbcTaosdemo taosdemo = new JdbcTaosdemo(config);
taosdemo.init();
taosdemo.dropDatabase();
taosdemo.createDatabase();
taosdemo.useDatabase();
taosdemo.createSuperTable();
taosdemo.createTableMultiThreads();
if (infinite) {
taosdemo.insertInfinite();
} else {
taosdemo.insertMultiThreads();
taosdemo.countFromSuperTable();
if (config.isDeleteTable())
taosdemo.dropSuperTable();
taosdemo.close();
}
}
/**
* establish the connection
*/
private void init() {
try {
Class.forName("com.taosdata.jdbc.TSDBDriver");
connection = getConnection(config);
if (connection != null)
logger.info("[ OK ] Connection established.");
} catch (ClassNotFoundException | SQLException e) {
logger.error(e.getMessage());
throw new RuntimeException("connection failed: " + config.getHost());
}
}
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();
execute(sql);
}
private void dropDatabase() {
String sql = "drop database if exists " + config.getDbName();
execute(sql);
}
/**
* use database
*/
private void useDatabase() {
String sql = "use " + config.getDbName();
execute(sql);
}
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)";
execute(sql);
}
/**
* create table use super table with multi threads
*/
private void createTableMultiThreads() {
try {
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < config.getNumberOfThreads(); i++) {
Thread thread = new Thread(new CreateTableTask(config, i * tableSize, tableSize), "Thread-" + i);
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
logger.info(">>> Multi Threads create table finished.");
} catch (InterruptedException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
private void insertInfinite() {
try {
final long startDatetime = TimeStampUtil.datetimeToLong("2005-01-01 00:00:00.000");
final long finishDatetime = TimeStampUtil.datetimeToLong("2030-01-01 00:00:00.000");
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < config.getNumberOfThreads(); i++) {
Thread thread = new Thread(new InsertTableDatetimeTask(config, i * tableSize, tableSize, startDatetime, finishDatetime), "Thread-" + i);
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
logger.info(">>> Multi Threads insert table finished.");
} catch (InterruptedException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
private void insertMultiThreads() {
try {
final int tableSize = config.getNumberOfTable() / config.getNumberOfThreads();
final int numberOfRecordsPerTable = config.getNumberOfRecordsPerTable();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < config.getNumberOfThreads(); i++) {
Thread thread = new Thread(new InsertTableTask(config, i * tableSize, tableSize, numberOfRecordsPerTable), "Thread-" + i);
threads.add(thread);
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
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 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);
}
private void close() {
try {
if (connection != null) {
this.connection.close();
logger.info("connection closed.");
}
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
/**
* drop super table
*/
private void dropSuperTable() {
String sql = "drop table if exists " + config.getDbName() + "." + config.getStbName();
execute(sql);
}
/**
* execute sql, use this method when sql is create, alter, drop..
*/
private void execute(String sql) {
try (Statement statement = connection.createStatement()) {
long start = System.currentTimeMillis();
boolean execute = statement.execute(sql);
long end = System.currentTimeMillis();
printSql(sql, execute, (end - start));
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
private void printSql(String sql, boolean succeed, long cost) {
logger.info("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
}
private void executeQuery(String sql) {
try (Statement statement = connection.createStatement()) {
long start = System.currentTimeMillis();
ResultSet resultSet = statement.executeQuery(sql);
long end = System.currentTimeMillis();
printSql(sql, true, (end - start));
printResult(resultSet);
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
private void printResult(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
while (resultSet.next()) {
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnLabel = metaData.getColumnLabel(i);
String value = resultSet.getString(i);
sb.append(columnLabel + ": " + value + "\t");
}
logger.info(sb.toString());
}
}
}
package com.taosdata.example.domain;
public class JdbcTaosdemoConfig {
//The host to connect to TDengine. Must insert one
private String host;
//The TCP/IP port number to use for the connection. Default is 6030.
private int port = 6030;
//The TDengine user name to use when connecting to the server. Default is 'root'
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";
//
private int keep = 365 * 20;
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;
//Delete data. Default is false
private boolean deleteTable = true;
/**
* parse args from command line
*
* @param args command line args
* @return JdbcTaosdemoConfig
*/
public static JdbcTaosdemoConfig build(String[] args) {
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig();
for (int i = 0; i < args.length; i++) {
if ("-h".equals(args[i]) && i < args.length - 1) {
config.setHost(args[++i]);
}
if ("-p".equals(args[i]) && i < args.length - 1) {
config.setPort(Integer.parseInt(args[++i]));
}
if ("-u".equals(args[i]) && i < args.length - 1) {
config.setUser(args[++i]);
}
if ("-P".equals(args[i]) && i < args.length - 1) {
config.setPassword(args[++i]);
}
if ("-d".equals(args[i]) && i < args.length - 1) {
config.setDbName(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]));
}
if ("-t".equals(args[i]) && i < args.length - 1) {
config.setNumberOfTable(Integer.parseInt(args[++i]));
}
if ("-n".equals(args[i]) && i < args.length - 1) {
config.setNumberOfRecordsPerTable(Integer.parseInt(args[++i]));
}
if ("-D".equals(args[i]) && i < args.length - 1) {
config.setDeleteTable(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 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() {
return keep;
}
public void setKeep(int keep) {
this.keep = keep;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
}
package com.taosdata.example.task;
import com.taosdata.example.JdbcTaosdemo;
import com.taosdata.example.domain.JdbcTaosdemoConfig;
import org.apache.log4j.Logger;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTableTask implements Runnable {
private static Logger logger = Logger.getLogger(CreateTableTask.class);
private final JdbcTaosdemoConfig config;
private final int startIndex;
private final int tableNumber;
public CreateTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber) {
this.config = config;
this.startIndex = startIndex;
this.tableNumber = tableNumber;
}
@Override
public void run() {
try {
Connection connection = JdbcTaosdemo.getConnection(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));
statement.close();
logger.info(">>> " + sql);
}
connection.close();
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
package com.taosdata.example.task;
import com.taosdata.example.JdbcTaosdemo;
import com.taosdata.example.domain.JdbcTaosdemoConfig;
import org.apache.log4j.Logger;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertTableDatetimeTask implements Runnable {
private static Logger logger = Logger.getLogger(InsertTableDatetimeTask.class);
private final JdbcTaosdemoConfig config;
private final int startTableIndex;
private final int tableNumber;
private final long startDatetime;
private final long finishedDatetime;
public InsertTableDatetimeTask(JdbcTaosdemoConfig config, int startTableIndex, int tableNumber, long startDatetime, long finishedDatetime) {
this.config = config;
this.startTableIndex = startTableIndex;
this.tableNumber = tableNumber;
this.startDatetime = startDatetime;
this.finishedDatetime = finishedDatetime;
}
@Override
public void run() {
try {
Connection connection = JdbcTaosdemo.getConnection(config);
for (long ts = startDatetime; ts < finishedDatetime; ts++) {
for (int i = startTableIndex; i < startTableIndex + tableNumber; i++) {
String sql = JdbcTaosdemo.insertSql(i + 1, ts, config);
Statement statement = connection.createStatement();
statement.execute(sql);
statement.close();
logger.info(Thread.currentThread().getName() + ">>> " + sql);
}
}
connection.close();
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
package com.taosdata.example.task;
import com.taosdata.example.JdbcTaosdemo;
import com.taosdata.example.domain.JdbcTaosdemoConfig;
import org.apache.log4j.Logger;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class InsertTableTask implements Runnable {
private static Logger logger = Logger.getLogger(InsertTableTask.class);
private final JdbcTaosdemoConfig config;
private final int startIndex;
private final int tableNumber;
private final int recordsNumber;
public InsertTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber, int recordsNumber) {
this.config = config;
this.startIndex = startIndex;
this.tableNumber = tableNumber;
this.recordsNumber = recordsNumber;
}
@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);
Statement statement = connection.createStatement();
statement.execute(sql);
statement.close();
logger.info(Thread.currentThread().getName() + ">>> " + sql);
}
}
connection.close();
} catch (SQLException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
package com.taosdata.example.utils;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class TimeStampUtil {
private static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss.SSS";
public static long datetimeToLong(String dateTime) {
SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
try {
return sdf.parse(dateTime).getTime();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
public static String longToDatetime(long time) {
SimpleDateFormat sdf = new SimpleDateFormat(datetimeFormat);
return sdf.format(new Date(time));
}
public static void main(String[] args) {
final String startTime = "2005-01-01 00:00:00.000";
long start = TimeStampUtil.datetimeToLong(startTime);
System.out.println(start);
String datetime = TimeStampUtil.longToDatetime(1519833600000L);
System.out.println(datetime);
}
}
### 设置###
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
### 输出DEBUG 级别以上的日志到=logs/error.log ###
log4j.appender.DebugLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugLog.File=logs/debug.log
log4j.appender.DebugLog.Append=true
log4j.appender.DebugLog.Threshold=DEBUG
log4j.appender.DebugLog.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志到=logs/error.log ###
log4j.appender.ErrorLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorLog.File=logs/error.log
log4j.appender.ErrorLog.Append=true
log4j.appender.ErrorLog.Threshold=ERROR
log4j.appender.ErrorLog.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorLog.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册