Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
慢慢CG
TDengine
提交
53ccc8ff
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看板
提交
53ccc8ff
编写于
1月 21, 2021
作者:
Z
zyyang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change
上级
e6005b1e
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
142 addition
and
51 deletion
+142
-51
src/connector/jdbc/src/test/java/com/taosdata/jdbc/ImportTest.java
...ctor/jdbc/src/test/java/com/taosdata/jdbc/ImportTest.java
+75
-51
src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/TimeStampUtil.java
.../src/test/java/com/taosdata/jdbc/utils/TimeStampUtil.java
+67
-0
未找到文件。
src/connector/jdbc/src/test/java/com/taosdata/jdbc/ImportTest.java
浏览文件 @
53ccc8ff
package
com.taosdata.jdbc
;
package
com.taosdata.jdbc
;
import
org.junit.After
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.Test
;
...
@@ -9,84 +10,107 @@ import java.util.Properties;
...
@@ -9,84 +10,107 @@ import java.util.Properties;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
ImportTest
extends
BaseTest
{
public
class
ImportTest
{
Connection
connection
=
null
;
Connection
connection
;
Statement
statement
=
null
;
String
dbName
=
"test"
;
String
dbName
=
"test"
;
String
tName
=
"t0"
;
String
tName
=
"t0"
;
String
host
=
"localhost"
;
String
host
=
"127.0.0.1"
;
private
static
long
ts
;
@Before
@Before
public
void
createDatabase
()
throws
SQLException
{
public
void
createDatabase
()
{
try
{
try
{
Class
.
forName
(
"com.taosdata.jdbc.TSDBDriver"
);
Class
.
forName
(
"com.taosdata.jdbc.TSDBDriver"
);
Properties
properties
=
new
Properties
();
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"
);
connection
=
DriverManager
.
getConnection
(
"jdbc:TAOS://"
+
host
+
":0/"
,
properties
);
Statement
stmt
=
connection
.
createStatement
();
stmt
.
executeUpdate
(
"drop database if exists "
+
dbName
);
stmt
.
executeUpdate
(
"create database if not exists "
+
dbName
);
stmt
.
executeUpdate
(
"create table if not exists "
+
dbName
+
"."
+
tName
+
" (ts timestamp, k int, v int)"
);
ts
=
System
.
currentTimeMillis
();
stmt
.
close
();
}
catch
(
ClassNotFoundException
e
)
{
}
catch
(
ClassNotFoundException
e
)
{
return
;
return
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
TSDBDriver
.
PROPERTY_KEY_HOST
,
host
);
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"
);
connection
=
DriverManager
.
getConnection
(
"jdbc:TAOS://"
+
host
+
":0/"
,
properties
);
statement
=
connection
.
createStatement
();
statement
.
executeUpdate
(
"drop database if exists "
+
dbName
);
statement
.
executeUpdate
(
"create database if not exists "
+
dbName
);
statement
.
executeUpdate
(
"create table if not exists "
+
dbName
+
"."
+
tName
+
" (ts timestamp, k int, v int)"
);
}
}
@Test
@Test
public
void
insertData
()
throws
Exception
{
public
void
case001_
insertData
()
throws
Exception
{
long
ts
=
1496732686000
l
;
try
(
Statement
stmt
=
connection
.
createStatement
())
{
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
ts
++;
ts
++
;
int
row
=
stmt
.
executeUpdate
(
"import into "
+
dbName
+
"."
+
tName
+
" values ("
+
ts
+
", "
+
(
100
+
i
)
+
", "
+
i
+
")"
)
;
int
row
=
statement
.
executeUpdate
(
"insert into "
+
dbName
+
"."
+
tName
+
" values ("
+
ts
+
", "
+
(
100
+
i
)
+
", "
+
i
+
")"
);
System
.
out
.
println
(
"import into "
+
dbName
+
"."
+
tName
+
" values ("
+
ts
+
", "
+
(
100
+
i
)
+
", "
+
i
+
")\t"
+
row
);
System
.
out
.
println
(
"insert into "
+
dbName
+
"."
+
tName
+
" values ("
+
ts
+
", "
+
(
100
+
i
)
+
", "
+
i
+
")\t"
+
row
);
assertEquals
(
1
,
row
);
assertEquals
(
1
,
row
);
}
}
}
}
}
@Test
@Test
public
void
selectData
()
throws
Exception
{
public
void
case002_checkSum
()
{
insertData
();
Assert
.
assertEquals
(
50
,
select
());
String
sql
=
"select * from test.t0"
;
}
ResultSet
resSet
=
statement
.
executeQuery
(
sql
);
while
(
resSet
.
next
())
{
private
int
select
()
{
for
(
int
i
=
1
;
i
<=
resSet
.
getMetaData
().
getColumnCount
();
i
++)
{
int
count
=
0
;
System
.
out
.
printf
(
i
+
": "
+
resSet
.
getString
(
i
)
+
"\t"
);
try
(
Statement
stmt
=
connection
.
createStatement
())
{
String
sql
=
"select * from "
+
dbName
+
"."
+
tName
;
ResultSet
rs
=
stmt
.
executeQuery
(
sql
);
while
(
rs
.
next
())
{
for
(
int
i
=
1
;
i
<=
rs
.
getMetaData
().
getColumnCount
();
i
++)
{
System
.
out
.
printf
(
i
+
": "
+
rs
.
getString
(
i
)
+
"\t"
);
}
count
++;
}
}
rs
.
close
();
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
re
sSet
.
close
()
;
re
turn
count
;
}
}
@Test
@Test
public
void
importData
()
throws
Exception
{
public
void
case003_importData
()
{
// 避免时间重复
// 避免时间重复
long
ts
=
1496732686000
l
;
try
(
Statement
stmt
=
connection
.
createStatement
())
{
StringBuilder
sqlBuilder
=
new
StringBuilder
(
"import into "
).
append
(
dbName
).
append
(
"."
).
append
(
tName
).
append
(
" values "
);
StringBuilder
sqlBuilder
=
new
StringBuilder
(
"insert into "
).
append
(
dbName
).
append
(
"."
).
append
(
tName
).
append
(
" values "
);
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
int
a
=
i
/
5
;
for
(
int
i
=
0
;
i
<
50
;
i
++)
{
long
t
=
ts
+
a
;
int
a
=
i
/
5
;
sqlBuilder
.
append
(
"("
).
append
(
t
).
append
(
","
).
append
((
100
+
i
)).
append
(
","
).
append
(
i
).
append
(
") "
);
long
t
=
ts
+
a
;
}
sqlBuilder
.
append
(
"("
).
append
(
t
).
append
(
","
).
append
((
100
+
i
)).
append
(
","
).
append
(
i
).
append
(
") "
);
System
.
out
.
println
(
sqlBuilder
.
toString
());
int
rows
=
stmt
.
executeUpdate
(
sqlBuilder
.
toString
());
assertEquals
(
10
,
rows
);
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
System
.
out
.
println
(
sqlBuilder
.
toString
());
int
rows
=
statement
.
executeUpdate
(
sqlBuilder
.
toString
());
System
.
out
.
println
(
rows
);
assertEquals
(
10
,
rows
);
}
}
@After
@Test
public
void
close
()
throws
Exception
{
public
void
case004_checkSum
()
{
statement
.
executeUpdate
(
"drop database "
+
dbName
);
Assert
.
assertEquals
(
100
,
select
());
statement
.
close
();
}
connection
.
close
();
Thread
.
sleep
(
10
);
@After
public
void
close
()
{
try
{
if
(
connection
!=
null
)
{
Statement
statement
=
connection
.
createStatement
();
statement
.
executeUpdate
(
"drop database "
+
dbName
);
statement
.
close
();
connection
.
close
();
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
();
}
}
}
}
}
src/connector/jdbc/src/test/java/com/taosdata/jdbc/utils/TimeStampUtil.java
0 → 100644
浏览文件 @
53ccc8ff
package
com.taosdata.jdbc.utils
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
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
IllegalArgumentException
(
"invalid datetime string >>> "
+
dateTime
);
}
}
public
static
String
longToDatetime
(
long
time
)
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
datetimeFormat
);
return
sdf
.
format
(
new
Date
(
time
));
}
public
static
class
TimeTuple
{
public
Long
start
;
public
Long
end
;
public
Long
timeGap
;
TimeTuple
(
long
start
,
long
end
,
long
timeGap
)
{
this
.
start
=
start
;
this
.
end
=
end
;
this
.
timeGap
=
timeGap
;
}
}
public
static
TimeTuple
range
(
long
start
,
long
timeGap
,
long
size
)
{
long
now
=
System
.
currentTimeMillis
();
if
(
timeGap
<
1
)
timeGap
=
1
;
if
(
start
==
0
)
start
=
now
-
size
*
timeGap
;
// 如果size小于1异常
if
(
size
<
1
)
throw
new
IllegalArgumentException
(
"size less than 1."
);
// 如果timeGap为1,已经超长,需要前移start
if
(
start
+
size
>
now
)
{
start
=
now
-
size
;
return
new
TimeTuple
(
start
,
now
,
1
);
}
long
end
=
start
+
(
long
)
(
timeGap
*
size
);
if
(
end
>
now
)
{
//压缩timeGap
end
=
now
;
double
gap
=
(
end
-
start
)
/
(
size
*
1.0f
);
if
(
gap
<
1.0f
)
{
timeGap
=
1
;
start
=
end
-
size
;
}
else
{
timeGap
=
(
long
)
gap
;
end
=
start
+
(
long
)
(
timeGap
*
size
);
}
}
return
new
TimeTuple
(
start
,
end
,
timeGap
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录