Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
8e75b9e0
T
TDengine
项目概览
慢慢CG
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8e75b9e0
编写于
9月 25, 2020
作者:
S
Shengliang Guan
提交者:
GitHub
9月 25, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #3679 from taosdata/feature/TD-1572
update JdbcTaosdemo
上级
b9006a2e
56c8889e
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
194 addition
and
180 deletion
+194
-180
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java
.../java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java
+25
-63
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java
...sdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java
+51
-92
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/CreateTableTask.java
...m/taosdata/example/jdbcTaosdemo/task/CreateTableTask.java
+5
-7
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableDatetimeTask.java
...ta/example/jdbcTaosdemo/task/InsertTableDatetimeTask.java
+6
-7
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java
...m/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java
+12
-5
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/ConnectionFactory.java
...aosdata/example/jdbcTaosdemo/utils/ConnectionFactory.java
+32
-0
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java
...a/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java
+58
-0
tests/examples/JDBC/JDBCDemo/src/main/resources/log4j.properties
...xamples/JDBC/JDBCDemo/src/main/resources/log4j.properties
+5
-6
未找到文件。
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/JdbcTaosdemo.java
浏览文件 @
8e75b9e0
...
...
@@ -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
);
}
...
...
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/domain/JdbcTaosdemoConfig.java
浏览文件 @
8e75b9e0
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
=
tru
e
;
private
boolean
deleteTable
=
fals
e
;
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
(
"-
D
"
.
equals
(
args
[
i
])
&&
i
<
args
.
length
-
1
)
{
config
.
setDeleteTable
(
Boolean
.
parseBoolean
(
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
)
{
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
;
}
}
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/CreateTableTask.java
浏览文件 @
8e75b9e0
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
);
}
...
...
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableDatetimeTask.java
浏览文件 @
8e75b9e0
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
value
Cnt
=
100
;
for
(
long
ts
=
startDatetime
;
ts
<
finishedDatetime
;
ts
+=
valueC
nt
)
{
Connection
connection
=
ConnectionFactory
.
build
(
config
);
int
value
sCount
=
config
.
getNumberOfRecordsPerRequest
()
;
for
(
long
ts
=
startDatetime
;
ts
<
finishedDatetime
;
ts
+=
valuesCou
nt
)
{
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
();
...
...
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/task/InsertTableTask.java
浏览文件 @
8e75b9e0
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
++)
{
Connection
connection
=
ConnectionFactory
.
build
(
config
);
// iterate insert
for
(
int
j
=
0
;
j
<
recordsNumber
;
j
++)
{
String
sql
=
JdbcTaosdemo
.
insertSql
(
i
+
1
,
config
);
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
();
...
...
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/ConnectionFactory.java
0 → 100644
浏览文件 @
8e75b9e0
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
);
}
}
tests/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/jdbcTaosdemo/utils/SqlSpeller.java
0 → 100644
浏览文件 @
8e75b9e0
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
tests/examples/JDBC/JDBCDemo/src/main/resources/log4j.properties
浏览文件 @
8e75b9e0
### 设置###
#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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录