prepare test data and add test case

This commit is contained in:
dingbo 2021-11-23 17:34:44 +08:00
parent 617f8e7d5d
commit 93a3369eb6
3 changed files with 128 additions and 7 deletions

View File

@ -97,6 +97,18 @@
<artifactId>groovy-all</artifactId> <artifactId>groovy-all</artifactId>
<version>2.1.9</version> <version>2.1.9</version>
</dependency> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.34</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -6,18 +6,18 @@
"name": "mysqlreader", "name": "mysqlreader",
"parameter": { "parameter": {
"username": "root", "username": "root",
"password": "123456", "password": "passw0rd",
"column": [ "column": [
"*" "*"
], ],
"splitPk": "f1", "splitPk": "station",
"connection": [ "connection": [
{ {
"table": [ "table": [
"weather" "weather"
], ],
"jdbcUrl": [ "jdbcUrl": [
"jdbc:mysql://192.168.56.105:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8" "jdbc:mysql://127.0.0.1:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf8"
] ]
} }
] ]
@ -26,13 +26,25 @@
"writer": { "writer": {
"name": "tdenginewriter", "name": "tdenginewriter",
"parameter": { "parameter": {
"host": "192.168.56.105", "host": "127.0.0.1",
"port": 6030, "port": 6030,
"dbname": "test", "dbname": "test",
"user": "root", "user": "root",
"password": "taosdata", "password": "taosdata",
"table": "weather", "batchSize": 1000,
"batchSize": 1000 "stable": "weather",
"tagColumn": {
"station": 0
},
"fieldColumn": {
"latitude": 1,
"longtitude": 2,
"tmax": 4,
"tmin": 5
},
"timestampColumn":{
"date": 3
}
} }
} }
} }

View File

@ -1,10 +1,105 @@
package com.alibaba.datax.core; package com.alibaba.datax.core;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.sql.*;
import java.util.*;
import java.util.Date;
/**
* 测试从mysql到TD
*/
public class TestMysql2TDengine { public class TestMysql2TDengine {
@Test
public void genTestData() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost/mysql?" +
"user=root&password=passw0rd");
stmt = conn.createStatement();
stmt.execute("create database if not exists test");
stmt.execute("use test");
stmt.execute("drop table weather");
stmt.execute("CREATE TABLE IF NOT EXISTS weather(station varchar(100), latitude DOUBLE, longtitude DOUBLE, `date` DATETIME, tmax INT, tmin INT)");
pstmt = conn.prepareStatement("insert into weather(station, latitude, longtitude, `date`, tmax, tmin) values (?, ?, ?, ?, ?, ?)");
genRandomData(pstmt);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) {
} // ignore
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) {
} // ignore
stmt = null;
}
if (pstmt != null) {
pstmt.close();
}
}
}
private void genRandomData(PreparedStatement psmt) throws SQLException {
Random random = new Random();
Calendar calendar = Calendar.getInstance();
calendar.set(1990, 0, 1, 1, 0, 0);
List<String> stations = Arrays.asList("STA", "STB", "STC");
for (int i = 0; i < (10 * 100 * 24); i++) {
for (int j = 0; j < 3; j++) {
psmt.setString(1, stations.get(j));
psmt.setDouble(2, random.nextDouble() * 1000);
psmt.setDouble(3, random.nextDouble() * 1000);
psmt.setTimestamp(4, new java.sql.Timestamp(calendar.getTime().getTime()));
psmt.setInt(5, random.nextInt(100));
psmt.setInt(6, random.nextInt(100));
psmt.addBatch();
}
calendar.add(Calendar.MINUTE, 60);
if (i % 1000 == 0) {
psmt.executeBatch();
}
}
psmt.executeBatch();
}
@Test
public void prepareTDengine() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
Statement stmt = null;
try {
conn = DriverManager.getConnection("jdbc:TAOS://127.0.0.1:6030/log?user=root&password=taosdata");
stmt = conn.createStatement();
stmt.execute("create database if not exists test");
stmt.execute("drop stable if exists test.weather");
} finally {
if (stmt != null) {
stmt.close();
}
}
}
@Test @Test
public void test() { public void test() {
System.out.println(System.getProperty("java.library.path")); System.out.println(System.getProperty("java.library.path"));
@ -17,4 +112,6 @@ public class TestMysql2TDengine {
} }
} }
} }