提交 dc23dd49 编写于 作者: Z zyyang

update jdbcTaosDemo

上级 49413180
......@@ -5,18 +5,11 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>jdbcdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>jdbcChecker</artifactId>
<version>SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugins</artifactId>
<version>30</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
......@@ -30,7 +23,7 @@
<configuration>
<archive>
<manifest>
<mainClass>TSDBSyncSample</mainClass>
<mainClass>com.taosdata.example.jdbcTaosdemo.JdbcTaosdemo</mainClass>
</manifest>
</archive>
<descriptorRefs>
......@@ -63,12 +56,18 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.4</version>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
......@@ -2,12 +2,14 @@
TDengine's JDBC demo project is organized in a Maven way so that users can easily compile, package and run the project. If you don't have Maven on your server, you may install it using
<pre>sudo apt-get install maven</pre>
## Compile and Install JDBC Driver
TDengine's JDBC driver jar is not yet published to maven center repo, so we need to manually compile it and install it to the local Maven repository. This can be easily done with Maven. Go to source directory of the JDBC driver ``TDengine/src/connector/jdbc`` and execute
<pre>mvn clean package install</pre>
## Install TDengine Client
Make sure you have already installed a tdengine client on your current develop environment.
Download the tdengine package on our website: ``https://www.taosdata.com/cn/all-downloads/`` and install the client.
## Compile the Demo Code and Run It
To compile the demo project, go to the source directory ``TDengine/tests/examples/JDBC/JDBCDemo`` and execute
<pre>mvn clean assembly:single package</pre>
The ``pom.xml`` is configured to package all the dependencies into one executable jar file. To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute
<pre>java -jar jdbcdemo-1.0-SNAPSHOT-jar-with-dependencies.jar</pre>
<pre>mvn clean package assembly:single</pre>
The ``pom.xml`` is configured to package all the dependencies into one executable jar file.
To run it, go to ``examples/JDBC/JDBCDemo/target`` and execute
<pre>java -jar jdbcChecker-SNAPSHOT-jar-with-dependencies.jar -host localhost</pre>
\ No newline at end of file
import java.sql.*;
public class TSDBSyncSample {
private static final String JDBC_PROTOCAL = "jdbc:TAOS://";
private static final String TSDB_DRIVER = "com.taosdata.jdbc.TSDBDriver";
private String host = "127.0.0.1";
private String user = "root";
private String password = "taosdata";
private int port = 0;
private String jdbcUrl = "";
private String databaseName = "db";
private String metricsName = "mt";
private String tablePrefix = "t";
private int tablesCount = 1;
private int loopCount = 2;
private int batchSize = 10;
private long beginTimestamp = 1519833600000L;
private long rowsInserted = 0;
static {
try {
Class.forName(TSDB_DRIVER);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
TSDBSyncSample tester = new TSDBSyncSample();
tester.doReadArgument(args);
System.out.println("---------------------------------------------------------------");
System.out.println("Start testing...");
System.out.println("---------------------------------------------------------------");
tester.doMakeJdbcUrl();
tester.doCreateDbAndTable();
tester.doExecuteInsert();
tester.doExecuteQuery();
System.out.println("\n---------------------------------------------------------------");
System.out.println("Stop testing...");
System.out.println("---------------------------------------------------------------");
}
private void doReadArgument(String[] args) {
System.out.println("Arguments format: host tables loop batchs");
if (args.length >= 1) {
this.host = args[0];
}
if (args.length >= 2) {
this.tablesCount = Integer.parseInt(args[1]);
}
if (args.length >= 3) {
this.loopCount = Integer.parseInt(args[2]);
}
if (args.length >= 4) {
this.batchSize = Integer.parseInt(args[3]);
}
}
private void doMakeJdbcUrl() {
// jdbc:TSDB://127.0.0.1:0/dbname?user=root&password=taosdata
System.out.println("\nJDBC URL to use:");
this.jdbcUrl = String.format("%s%s:%d/%s?user=%s&password=%s", JDBC_PROTOCAL, this.host, this.port, "",
this.user, this.password);
System.out.println(this.jdbcUrl);
}
private void doCreateDbAndTable() {
System.out.println("\n---------------------------------------------------------------");
System.out.println("Start creating databases and tables...");
String sql = "";
try (Connection conn = DriverManager.getConnection(jdbcUrl);
Statement stmt = conn.createStatement()){
sql = "create database if not exists " + this.databaseName;
stmt.executeUpdate(sql);
System.out.printf("Successfully executed: %s\n", sql);
sql = "use " + this.databaseName;
stmt.executeUpdate(sql);
System.out.printf("Successfully executed: %s\n", sql);
sql = "create table if not exists " + this.metricsName + " (ts timestamp, v1 int) tags(t1 int)";
stmt.executeUpdate(sql);
System.out.printf("Successfully executed: %s\n", sql);
for (int i = 0; i < this.tablesCount; i++) {
sql = String.format("create table if not exists %s%d using %s tags(%d)", this.tablePrefix, i,
this.metricsName, i);
stmt.executeUpdate(sql);
System.out.printf("Successfully executed: %s\n", sql);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.printf("Failed to execute SQL: %s\n", sql);
System.exit(4);
} catch (Exception e) {
e.printStackTrace();
System.exit(4);
}
System.out.println("Successfully created databases and tables");
}
public void doExecuteInsert() {
System.out.println("\n---------------------------------------------------------------");
System.out.println("Start inserting data...");
int start = (int) System.currentTimeMillis();
StringBuilder sql = new StringBuilder("");
try (Connection conn = DriverManager.getConnection(jdbcUrl);
Statement stmt = conn.createStatement()){
stmt.executeUpdate("use " + databaseName);
for (int loop = 0; loop < this.loopCount; loop++) {
for (int table = 0; table < this.tablesCount; ++table) {
sql = new StringBuilder("insert into ");
sql.append(this.tablePrefix).append(table).append(" values");
for (int batch = 0; batch < this.batchSize; ++batch) {
int rows = loop * this.batchSize + batch;
sql.append("(").append(this.beginTimestamp + rows).append(",").append(rows).append(")");
}
int affectRows = stmt.executeUpdate(sql.toString());
this.rowsInserted += affectRows;
}
}
} catch (SQLException e) {
e.printStackTrace();
System.out.printf("Failed to execute SQL: %s\n", sql.toString());
System.exit(4);
} catch (Exception e) {
e.printStackTrace();
System.exit(4);
}
int end = (int) System.currentTimeMillis();
System.out.println("Inserting completed!");
System.out.printf("Total %d rows inserted, %d rows failed, time spend %d seconds.\n", this.rowsInserted,
this.loopCount * this.batchSize - this.rowsInserted, (end - start) / 1000);
}
public void doExecuteQuery() {
System.out.println("\n---------------------------------------------------------------");
System.out.println("Starting querying data...");
ResultSet resSet = null;
StringBuilder sql = new StringBuilder("");
StringBuilder resRow = new StringBuilder("");
try (Connection conn = DriverManager.getConnection(jdbcUrl);
Statement stmt = conn.createStatement()){
stmt.executeUpdate("use " + databaseName);
for (int i = 0; i < this.tablesCount; ++i) {
sql = new StringBuilder("select * from ").append(this.tablePrefix).append(i);
resSet = stmt.executeQuery(sql.toString());
if (resSet == null) {
System.out.println(sql + " failed");
System.exit(4);
}
ResultSetMetaData metaData = resSet.getMetaData();
System.out.println("Retrieve metadata of " + tablePrefix + i);
for (int column = 1; column <= metaData.getColumnCount(); ++column) {
System.out.printf("Column%d: name = %s, type = %d, type name = %s, display size = %d\n", column, metaData.getColumnName(column), metaData.getColumnType(column),
metaData.getColumnTypeName(column), metaData.getColumnDisplaySize(column));
}
int rows = 0;
System.out.println("Retrieve data of " + tablePrefix + i);
while (resSet.next()) {
resRow = new StringBuilder();
for (int col = 1; col <= metaData.getColumnCount(); col++) {
resRow.append(metaData.getColumnName(col)).append("=").append(resSet.getObject(col))
.append(" ");
}
System.out.println(resRow.toString());
rows++;
}
try {
if (resSet != null)
resSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.printf("Successfully executed query: %s;\nTotal rows returned: %d\n", sql.toString(), rows);
}
} catch (SQLException e) {
e.printStackTrace();
System.out.printf("Failed to execute query: %s\n", sql.toString());
System.exit(4);
} catch (Exception e) {
e.printStackTrace();
System.exit(4);
}
System.out.println("Query completed!");
}
}
......@@ -5,7 +5,7 @@ import com.taosdata.jdbc.TSDBDriver;
import java.sql.*;
import java.util.Properties;
public class JDBCConnectorChecker {
public class JdbcChecker {
private static String host;
private static String dbName = "test";
private static String tbName = "weather";
......@@ -157,7 +157,7 @@ public class JDBCConnectorChecker {
return;
}
JDBCConnectorChecker checker = new JDBCConnectorChecker();
JdbcChecker checker = new JdbcChecker();
checker.init();
checker.createDatabase();
checker.useDatabase();
......
......@@ -25,6 +25,7 @@ public class JdbcTaosdemo {
}
public static void main(String[] args) {
// parse config from args
JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args);
boolean isHelp = Arrays.asList(args).contains("--help");
......@@ -38,27 +39,51 @@ public class JdbcTaosdemo {
}
JdbcTaosdemo taosdemo = new JdbcTaosdemo(config);
// establish connection
taosdemo.init();
// drop database
taosdemo.dropDatabase();
// create database
taosdemo.createDatabase();
// use db
taosdemo.useDatabase();
// create super table
taosdemo.createSuperTable();
// create sub tables
taosdemo.createTableMultiThreads();
boolean infinite = Arrays.asList(args).contains("--infinite");
if (infinite) {
logger.info("!!! Infinite Insert Mode Started. !!!!");
logger.info("!!! Infinite Insert Mode Started. !!!");
taosdemo.insertInfinite();
} else {
// insert into table
taosdemo.insertMultiThreads();
// single table select
// select from sub table
taosdemo.selectFromTableLimit();
taosdemo.selectCountFromTable();
taosdemo.selectAvgMinMaxFromTable();
// super table select
// select last from
taosdemo.selectLastFromTable();
// select from super table
taosdemo.selectFromSuperTableLimit();
taosdemo.selectCountFromSuperTable();
taosdemo.selectAvgMinMaxFromSuperTable();
//select avg ,max from stb where tag
taosdemo.selectAvgMinMaxFromSuperTableWhereTag();
//select last from stb where location = ''
taosdemo.selectLastFromSuperTableWhere();
// select group by
taosdemo.selectGroupBy();
// select like
taosdemo.selectLike();
// select where ts >= ts<=
taosdemo.selectLastOneHour();
taosdemo.selectLastOneDay();
taosdemo.selectLastOneWeek();
taosdemo.selectLastOneMonth();
taosdemo.selectLastOneYear();
// drop super table
if (config.isDeleteTable())
taosdemo.dropSuperTable();
......@@ -196,6 +221,11 @@ public class JdbcTaosdemo {
executeQuery(sql);
}
private void selectLastFromTable() {
String sql = SqlSpeller.selectLastFromTableSQL(config.getDbName(), config.getTbPrefix(), 1);
executeQuery(sql);
}
private void selectFromSuperTableLimit() {
String sql = SqlSpeller.selectFromSuperTableLimitSQL(config.getDbName(), config.getStbName(), 10, 0);
executeQuery(sql);
......@@ -211,6 +241,52 @@ public class JdbcTaosdemo {
executeQuery(sql);
}
private void selectAvgMinMaxFromSuperTableWhereTag() {
String sql = SqlSpeller.selectAvgMinMaxFromSuperTableWhere("current", config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastFromSuperTableWhere() {
String sql = SqlSpeller.selectLastFromSuperTableWhere("current", config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectGroupBy() {
String sql = SqlSpeller.selectGroupBy("current", config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLike() {
String sql = SqlSpeller.selectLike(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastOneHour() {
String sql = SqlSpeller.selectLastOneHour(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastOneDay() {
String sql = SqlSpeller.selectLastOneDay(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastOneWeek() {
String sql = SqlSpeller.selectLastOneWeek(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastOneMonth() {
String sql = SqlSpeller.selectLastOneMonth(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void selectLastOneYear() {
String sql = SqlSpeller.selectLastOneYear(config.getDbName(), config.getStbName());
executeQuery(sql);
}
private void close() {
try {
if (connection != null) {
......@@ -241,6 +317,7 @@ public class JdbcTaosdemo {
long end = System.currentTimeMillis();
printSql(sql, execute, (end - start));
} catch (SQLException e) {
logger.error("ERROR execute SQL ===> " + sql);
logger.error(e.getMessage());
e.printStackTrace();
}
......@@ -258,6 +335,7 @@ public class JdbcTaosdemo {
printSql(sql, true, (end - start));
printResult(resultSet);
} catch (SQLException e) {
logger.error("ERROR execute SQL ===> " + sql);
logger.error(e.getMessage());
e.printStackTrace();
}
......
......@@ -14,9 +14,9 @@ public final class JdbcTaosdemoConfig {
//Destination database. Default is 'test'
private String dbName = "test";
//keep
private int keep = 365 * 20;
private int keep = 3650;
//days
private int days = 30;
private int days = 10;
//Super table Name. Default is 'meters'
private String stbName = "meters";
......@@ -35,7 +35,7 @@ public final class JdbcTaosdemoConfig {
private boolean deleteTable = false;
public static void printHelp() {
System.out.println("Usage: java -jar JDBCConnectorChecker.jar [OPTION...]");
System.out.println("Usage: java -jar JdbcTaosDemo.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'");
......
......@@ -3,44 +3,49 @@ package com.taosdata.example.jdbcTaosdemo.task;
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;
import java.time.Duration;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
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;
private final int startTbIndex;
private final int tableNumber;
private final int recordsNumber;
private final int recordsNumberPerTable;
public InsertTableTask(JdbcTaosdemoConfig config, int startIndex, int tableNumber, int recordsNumber) {
public InsertTableTask(JdbcTaosdemoConfig config, int startTbIndex, int tableNumber, int recordsNumberPerTable) {
this.config = config;
this.startIndex = startIndex;
this.startTbIndex = startTbIndex;
this.tableNumber = tableNumber;
this.recordsNumber = recordsNumber;
this.recordsNumberPerTable = recordsNumberPerTable;
}
@Override
public void run() {
try {
Connection connection = ConnectionFactory.build(config);
int keep = config.getKeep();
Instant end = Instant.now();
Instant start = end.minus(Duration.ofDays(keep - 1));
long timeGap = ChronoUnit.MILLIS.between(start, end) / (recordsNumberPerTable - 1);
// iterate insert
for (int j = 0; j < recordsNumber; j++) {
long ts = beginTimestamp.getAndIncrement();
for (int j = 0; j < recordsNumberPerTable; j++) {
long ts = start.toEpochMilli() + (j * timeGap);
// insert data into echo table
for (int i = startIndex; i < startIndex + tableNumber; i++) {
for (int i = startTbIndex; i < startTbIndex + tableNumber; i++) {
String sql = SqlSpeller.insertOneRowSQL(config.getDbName(), config.getTbPrefix(), i + 1, ts);
logger.info(Thread.currentThread().getName() + ">>> " + sql);
Statement statement = connection.createStatement();
statement.execute(sql);
statement.close();
logger.info(Thread.currentThread().getName() + ">>> " + sql);
}
}
connection.close();
......
......@@ -78,5 +78,49 @@ public class SqlSpeller {
return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + "";
}
public static String selectLastFromTableSQL(String dbName, String tbPrefix, int tbIndex) {
return "select last(*) from " + dbName + "." + tbPrefix + "" + tbIndex;
}
//select avg ,max from stb where tag
public static String selectAvgMinMaxFromSuperTableWhere(String field, String dbName, String stbName) {
return "select avg(" + field + "),min(" + field + "),max(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'";
}
//select last from stb where
public static String selectLastFromSuperTableWhere(String field, String dbName, String stbName) {
return "select last(" + field + ") from " + dbName + "." + stbName + " where location = '" + locations[random.nextInt(locations.length)] + "'";
}
public static String selectGroupBy(String field, String dbName, String stbName) {
return "select avg(" + field + ") from " + dbName + "." + stbName + " group by location";
}
public static String selectLike(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where location like 'S%'";
}
public static String selectLastOneHour(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where ts >= now - 1h";
}
public static String selectLastOneDay(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where ts >= now - 1d";
}
public static String selectLastOneWeek(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where ts >= now - 1w";
}
public static String selectLastOneMonth(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where ts >= now - 1M";
}
public static String selectLastOneYear(String dbName, String stbName) {
return "select * from " + dbName + "." + stbName + " where ts >= now - 1y";
}
// select group by
// select like
// select ts >= ts<=
}
\ No newline at end of file
package com.taosdata.example.jdbcTaosdemo.utils;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.util.Date;
public class TimeStampUtil {
private static final String datetimeFormat = "yyyy-MM-dd HH:mm:ss.SSS";
......@@ -21,14 +23,14 @@ public class TimeStampUtil {
return sdf.format(new Date(time));
}
public static void main(String[] args) {
final String startTime = "2005-01-01 00:00:00.000";
public static void main(String[] args) throws ParseException {
// Instant now = Instant.now();
// System.out.println(now);
// Instant years20Ago = now.minus(Duration.ofDays(365));
// System.out.println(years20Ago);
long start = TimeStampUtil.datetimeToLong(startTime);
System.out.println(start);
String datetime = TimeStampUtil.longToDatetime(1519833600000L);
System.out.println(datetime);
}
......
package com.taosdata.example.jdbcTaosdemo.utils;
import org.junit.Test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import static org.junit.Assert.*;
public class TimeStampUtilTest {
@Test
public void datetimeToLong() {
final String startTime = "2005-01-01 00:00:00.000";
long start = TimeStampUtil.datetimeToLong(startTime);
assertEquals(1104508800000l, start);
}
@Test
public void longToDatetime() {
String datetime = TimeStampUtil.longToDatetime(1510000000000L);
assertEquals("2017-11-07 04:26:40.000", datetime);
}
@Test
public void getStartDateTime() {
int keep = 365;
Instant end = Instant.now();
System.out.println(end.toString());
System.out.println(end.toEpochMilli());
Instant start = end.minus(Duration.ofDays(keep));
System.out.println(start.toString());
System.out.println(start.toEpochMilli());
int numberOfRecordsPerTable = 10;
long timeGap = ChronoUnit.MILLIS.between(start, end) / (numberOfRecordsPerTable - 1);
System.out.println(timeGap);
System.out.println("===========================");
for (int i = 0; i < numberOfRecordsPerTable; i++) {
long ts = start.toEpochMilli() + (i * timeGap);
System.out.println(i + " : " + ts);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册