Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
003e8d3f
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1181
Star
22014
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
003e8d3f
编写于
5月 24, 2023
作者:
W
wade zhang
提交者:
GitHub
5月 24, 2023
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #21456 from taosdata/enh/TD-24388
enh: stmt insert demo
上级
ab017bc7
61df1a1b
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
65 addition
and
39 deletion
+65
-39
docs/examples/java/src/main/java/com/taos/example/StmtInsertExample.java
...ava/src/main/java/com/taos/example/StmtInsertExample.java
+65
-39
未找到文件。
docs/examples/java/src/main/java/com/taos/example/StmtInsertExample.java
浏览文件 @
003e8d3f
...
...
@@ -6,39 +6,32 @@ import java.sql.Connection;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.text.SimpleDateFormat
;
import
java.time.LocalDateTime
;
import
java.time.ZoneOffset
;
import
java.time.format.DateTimeFormatter
;
import
java.util.ArrayList
;
import
java.util.
Arrays
;
import
java.util.
Comparator
;
import
java.util.List
;
import
java.util.Random
;
import
java.util.stream.Collectors
;
public
class
StmtInsertExample
{
private
static
ArrayList
<
Long
>
tsToLongArray
(
String
ts
)
{
ArrayList
<
Long
>
result
=
new
ArrayList
<>();
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
"yyyy-MM-dd HH:mm:ss.SSS"
);
LocalDateTime
localDateTime
=
LocalDateTime
.
parse
(
ts
,
formatter
);
result
.
add
(
localDateTime
.
toInstant
(
ZoneOffset
.
of
(
"+8"
)).
toEpochMilli
());
return
result
;
}
private
static
String
datePattern
=
"yyyy-MM-dd HH:mm:ss.SSS"
;
private
static
DateTimeFormatter
formatter
=
DateTimeFormatter
.
ofPattern
(
datePattern
);
private
static
<
T
>
ArrayList
<
T
>
toArray
(
T
v
)
{
ArrayList
<
T
>
result
=
new
ArrayList
<>();
result
.
add
(
v
);
return
result
;
private
static
List
<
String
>
getRawData
(
int
size
)
{
SimpleDateFormat
format
=
new
SimpleDateFormat
(
datePattern
);
List
<
String
>
result
=
new
ArrayList
<>();
long
current
=
System
.
currentTimeMillis
();
Random
random
=
new
Random
();
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
String
time
=
format
.
format
(
current
+
i
);
int
id
=
random
.
nextInt
(
10
);
result
.
add
(
"d"
+
id
+
","
+
time
+
",10.30000,219,0.31000,California.SanFrancisco,2"
);
}
private
static
List
<
String
>
getRawData
()
{
return
Arrays
.
asList
(
"d1001,2018-10-03 14:38:05.000,10.30000,219,0.31000,California.SanFrancisco,2"
,
"d1001,2018-10-03 14:38:15.000,12.60000,218,0.33000,California.SanFrancisco,2"
,
"d1001,2018-10-03 14:38:16.800,12.30000,221,0.31000,California.SanFrancisco,2"
,
"d1002,2018-10-03 14:38:16.650,10.30000,218,0.25000,California.SanFrancisco,3"
,
"d1003,2018-10-03 14:38:05.500,11.80000,221,0.28000,California.LosAngeles,2"
,
"d1003,2018-10-03 14:38:16.600,13.40000,223,0.29000,California.LosAngeles,2"
,
"d1004,2018-10-03 14:38:05.000,10.80000,223,0.29000,California.LosAngeles,3"
,
"d1004,2018-10-03 14:38:06.500,11.50000,221,0.35000,California.LosAngeles,3"
);
return
result
.
stream
()
.
sorted
(
Comparator
.
comparing
(
s
->
s
.
split
(
","
)[
0
])).
collect
(
Collectors
.
toList
());
}
private
static
Connection
getConnection
()
throws
SQLException
{
...
...
@@ -48,9 +41,9 @@ public class StmtInsertExample {
private
static
void
createTable
(
Connection
conn
)
throws
SQLException
{
try
(
Statement
stmt
=
conn
.
createStatement
())
{
stmt
.
execute
(
"CREATE DATABASE power KEEP 3650"
);
stmt
.
executeUpdate
(
"
USE
power"
);
stmt
.
execute
(
"CREATE STABLE meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) "
+
stmt
.
execute
(
"CREATE DATABASE
if not exists
power KEEP 3650"
);
stmt
.
executeUpdate
(
"
use
power"
);
stmt
.
execute
(
"CREATE STABLE
if not exists
meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) "
+
"TAGS (location BINARY(64), groupId INT)"
);
}
}
...
...
@@ -58,21 +51,54 @@ public class StmtInsertExample {
private
static
void
insertData
()
throws
SQLException
{
try
(
Connection
conn
=
getConnection
())
{
createTable
(
conn
);
String
psql
=
"INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)"
;
String
psql
=
"INSERT INTO ? USING
power.
meters TAGS(?, ?) VALUES(?, ?, ?, ?)"
;
try
(
TSDBPreparedStatement
pst
=
(
TSDBPreparedStatement
)
conn
.
prepareStatement
(
psql
))
{
for
(
String
line
:
getRawData
())
{
String
tableName
=
null
;
ArrayList
<
Long
>
ts
=
new
ArrayList
<>();
ArrayList
<
Float
>
current
=
new
ArrayList
<>();
ArrayList
<
Integer
>
voltage
=
new
ArrayList
<>();
ArrayList
<
Float
>
phase
=
new
ArrayList
<>();
for
(
String
line
:
getRawData
(
100000
))
{
String
[]
ps
=
line
.
split
(
","
);
if
(
tableName
==
null
)
{
// bind table name and tags
tableName
=
"power."
+
ps
[
0
];
pst
.
setTableName
(
ps
[
0
]);
pst
.
setTagString
(
0
,
ps
[
5
]);
pst
.
setTagInt
(
1
,
Integer
.
valueOf
(
ps
[
6
]));
// bind values
pst
.
setTimestamp
(
0
,
tsToLongArray
(
ps
[
1
]));
//ps[1] looks like: 2018-10-03 14:38:05.000
pst
.
setFloat
(
1
,
toArray
(
Float
.
valueOf
(
ps
[
2
])));
pst
.
setInt
(
2
,
toArray
(
Integer
.
valueOf
(
ps
[
3
])));
pst
.
setFloat
(
3
,
toArray
(
Float
.
valueOf
(
ps
[
4
])));
}
else
{
if
(!
tableName
.
equals
(
ps
[
0
]))
{
pst
.
setTimestamp
(
0
,
ts
);
pst
.
setFloat
(
1
,
current
);
pst
.
setInt
(
2
,
voltage
);
pst
.
setFloat
(
3
,
phase
);
pst
.
columnDataAddBatch
();
pst
.
columnDataExecuteBatch
();
// bind table name and tags
tableName
=
ps
[
0
];
pst
.
setTableName
(
ps
[
0
]);
pst
.
setTagString
(
0
,
ps
[
5
]);
pst
.
setTagInt
(
1
,
Integer
.
valueOf
(
ps
[
6
]));
ts
.
clear
();
current
.
clear
();
voltage
.
clear
();
phase
.
clear
();
}
}
// bind values
// ps[1] looks like: 2018-10-03 14:38:05.000
LocalDateTime
localDateTime
=
LocalDateTime
.
parse
(
ps
[
1
],
formatter
);
ts
.
add
(
localDateTime
.
toInstant
(
ZoneOffset
.
of
(
"+8"
)).
toEpochMilli
());
current
.
add
(
Float
.
valueOf
(
ps
[
2
]));
voltage
.
add
(
Integer
.
valueOf
(
ps
[
3
]));
phase
.
add
(
Float
.
valueOf
(
ps
[
4
]));
}
pst
.
setTimestamp
(
0
,
ts
);
pst
.
setFloat
(
1
,
current
);
pst
.
setInt
(
2
,
voltage
);
pst
.
setFloat
(
3
,
phase
);
pst
.
columnDataAddBatch
();
pst
.
columnDataExecuteBatch
();
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录