未验证 提交 01cf1885 编写于 作者: X Xiao Ping 提交者: GitHub

Merge pull request #8629 from taosdata/xiaoping/add_test_case

add jdbc batchinsert demo
......@@ -17,7 +17,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.34</version>
<version>2.0.35</version>
</dependency>
</dependencies>
......
package com.taosdata.example;
import java.sql.*;
import java.util.*;
public class BatchInsert {
private static final String host = "127.0.0.1";
private static final String user = "root";
private static final String password = "taosdata";
private static final String dbname = "test";
private static final String stbname = "stb";
private static final int tables= 100;
private static final int rows = 500;
private static final long ts = 1604877767000l;
private Connection conn;
private void init() {
// final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
final String url = "jdbc:TAOS-RS://" + host + ":6041/?user=" + user + "&password=" + password;
// get connection
try {
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
conn = DriverManager.getConnection(url, properties);
if (conn != null){
System.out.println("[ OK ] Connection established.");
}
Statement stmt = conn.createStatement();
stmt.execute("drop database if exists " + dbname);
stmt.execute("create database if not exists " + dbname);
stmt.execute("use " + dbname);
stmt.execute("create table " + dbname + "." + stbname + "(ts timestamp, col int) tags(id int)");
} catch (SQLException e) {
e.printStackTrace();
}
}
private String generateSql() {
StringBuilder sb = new StringBuilder();
Random rand = new Random();
sb.append("insert into ");
for (int i = 0; i < tables; i++) {
sb.append(dbname + ".tb" + i + " using " + dbname + "." + stbname + " tags(" + i + ") values");
for (int j = 0; j < rows; j++) {
sb.append("(");
sb.append(ts + j);
sb.append(",");
sb.append(rand.nextInt(1000));
sb.append(") ");
}
}
return sb.toString();
}
private void executeQuery(String sql) {
try (Statement stmt = conn.createStatement()) {
long start = System.currentTimeMillis();
stmt.execute(sql);
long end = System.currentTimeMillis();
System.out.println("insert " + tables * rows + " records, cost " + (end - start)+ "ms");
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
BatchInsert bi = new BatchInsert();
String sql = bi.generateSql();
bi.init();
bi.executeQuery(sql);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册