Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
ba180921
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
ba180921
编写于
3月 26, 2022
作者:
B
Bo Ding
提交者:
GitHub
3月 26, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add java insert example (#11019)
* java insert example * sql upper case * java stmt example * java stmt exmaple
上级
92c0200e
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
168 addition
and
6 deletion
+168
-6
docs-cn/06-insert-data/01-sql-writing.mdx
docs-cn/06-insert-data/01-sql-writing.mdx
+2
-2
docs-cn/06-insert-data/_java_sql.mdx
docs-cn/06-insert-data/_java_sql.mdx
+3
-0
docs-cn/06-insert-data/_java_stmt.mdx
docs-cn/06-insert-data/_java_stmt.mdx
+3
-0
docs-examples/java/src/main/java/com/taos/example/RestInsertExample.java
...ava/src/main/java/com/taos/example/RestInsertExample.java
+74
-0
docs-examples/java/src/main/java/com/taos/example/RestInsertExmaple.java
...ava/src/main/java/com/taos/example/RestInsertExmaple.java
+0
-4
docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java
...ava/src/main/java/com/taos/example/StmtInsertExample.java
+86
-0
未找到文件。
docs-cn/06-insert-data/01-sql-writing.mdx
浏览文件 @
ba180921
...
...
@@ -84,7 +84,7 @@ INSERT INTO d1001 VALUES (1538548685000, 10.3, 219, 0.31) (1538548695000, 12.6,
:::note
1. 无论 RESTFful 方式建立连接还是本地驱动方式建立连接,以上示例代码都能正常工作。
2. 唯一需要注意的是:由于 RESTful 接口无状态, 不能使用 `use db` 语句来切换数据库。
2. 唯一需要注意的是:由于 RESTful 接口无状态, 不能使用 `use db` 语句来切换数据库
, 所以在上面示例中使用了`dbName.tbName`指定表名
。
:::
...
...
@@ -120,4 +120,4 @@ TDengine 也提供了支持参数绑定的 Prepare API,与 MySQL 一样,这
只有使用本地驱动方式连接连接,才能使用动态绑定工具。
:::
\ No newline at end of file
:::
docs-cn/06-insert-data/_java_sql.mdx
浏览文件 @
ba180921
```java
{{#include docs-examples/java/src/main/java/com/taos/example/RestInsertExample.java:insert}}
```
\ No newline at end of file
docs-cn/06-insert-data/_java_stmt.mdx
浏览文件 @
ba180921
```java
{{#include docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java:insert}}
```
docs-examples/java/src/main/java/com/taos/example/RestInsertExample.java
0 → 100644
浏览文件 @
ba180921
package
com.taos.example
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
RestInsertExample
{
private
static
Connection
getConnection
()
throws
SQLException
{
String
jdbcUrl
=
"jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata"
;
return
DriverManager
.
getConnection
(
jdbcUrl
);
}
private
static
List
<
String
>
getRawData
()
{
return
Arrays
.
asList
(
"d1001,2018-10-03 14:38:05.000,10.30000,219,0.31000,Beijing.Chaoyang,2"
,
"d1001,2018-10-03 14:38:15.000,12.60000,218,0.33000,Beijing.Chaoyang,2"
,
"d1001,2018-10-03 14:38:16.800,12.30000,221,0.31000,Beijing.Chaoyang,2"
,
"d1002,2018-10-03 14:38:16.650,10.30000,218,0.25000,Beijing.Chaoyang,3"
,
"d1003,2018-10-03 14:38:05.500,11.80000,221,0.28000,Beijing.Haidian,2"
,
"d1003,2018-10-03 14:38:16.600,13.40000,223,0.29000,Beijing.Haidian,2"
,
"d1004,2018-10-03 14:38:05.000,10.80000,223,0.29000,Beijing.Haidian,3"
,
"d1004,2018-10-03 14:38:06.500,11.50000,221,0.35000,Beijing.Haidian,3"
);
}
/**
* The generated SQL is:
* INSERT INTO power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 2) VALUES('2018-10-03 14:38:05.000',10.30000,219,0.31000)
* power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 2) VALUES('2018-10-03 14:38:15.000',12.60000,218,0.33000)
* power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 2) VALUES('2018-10-03 14:38:16.800',12.30000,221,0.31000)
* power.d1002 USING power.meters TAGS(Beijing.Chaoyang, 3) VALUES('2018-10-03 14:38:16.650',10.30000,218,0.25000)
* power.d1003 USING power.meters TAGS(Beijing.Haidian, 2) VALUES('2018-10-03 14:38:05.500',11.80000,221,0.28000)
* power.d1003 USING power.meters TAGS(Beijing.Haidian, 2) VALUES('2018-10-03 14:38:16.600',13.40000,223,0.29000)
* power.d1004 USING power.meters TAGS(Beijing.Haidian, 3) VALUES('2018-10-03 14:38:05.000',10.80000,223,0.29000)
* power.d1004 USING power.meters TAGS(Beijing.Haidian, 3) VALUES('2018-10-03 14:38:06.500',11.50000,221,0.35000)
*/
private
static
String
getSQL
()
{
StringBuilder
sb
=
new
StringBuilder
(
"INSERT INTO "
);
for
(
String
line
:
getRawData
())
{
String
[]
fields
=
line
.
split
(
","
);
sb
.
append
(
"power."
+
fields
[
0
]).
append
(
" USING power.meters TAGS("
)
.
append
(
fields
[
5
]).
append
(
", "
)
.
append
(
fields
[
6
])
.
append
(
") VALUES("
)
.
append
(
'\''
).
append
(
fields
[
1
]).
append
(
'\''
).
append
(
","
)
.
append
(
fields
[
2
]).
append
(
","
)
.
append
(
fields
[
3
]).
append
(
","
)
.
append
(
fields
[
4
]).
append
(
") "
);
}
return
sb
.
toString
();
}
public
static
void
insertData
()
throws
SQLException
{
try
(
Connection
conn
=
getConnection
())
{
try
(
Statement
stmt
=
conn
.
createStatement
())
{
stmt
.
execute
(
"CREATE DATABASE power KEEP 3650"
);
stmt
.
execute
(
"CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) "
+
"TAGS (location BINARY(64), groupId INT)"
);
String
sql
=
getSQL
();
int
rowCount
=
stmt
.
executeUpdate
(
sql
);
System
.
out
.
println
(
"rowCount="
+
rowCount
);
// rowCount=8
}
}
}
public
static
void
main
(
String
[]
args
)
throws
SQLException
{
insertData
();
}
}
docs-examples/java/src/main/java/com/taos/example/RestInsertExmaple.java
已删除
100644 → 0
浏览文件 @
92c0200e
package
com.taos.example
;
public
class
RestInsertExmaple
{
}
docs-examples/java/src/main/java/com/taos/example/StmtInsertExample.java
0 → 100644
浏览文件 @
ba180921
package
com.taos.example
;
import
com.taosdata.jdbc.TSDBPreparedStatement
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.time.LocalDateTime
;
import
java.time.ZoneOffset
;
import
java.time.format.DateTimeFormatter
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
public
class
StmtInsertExample
{
private
static
Connection
getConnection
()
throws
SQLException
{
String
jdbcUrl
=
"jdbc:TAOS://localhost:6030?user=root&password=taosdata"
;
return
DriverManager
.
getConnection
(
jdbcUrl
);
}
private
static
List
<
String
>
getRawData
()
{
return
Arrays
.
asList
(
"d1001,2018-10-03 14:38:05.000,10.30000,219,0.31000,Beijing.Chaoyang,2"
,
"d1001,2018-10-03 14:38:15.000,12.60000,218,0.33000,Beijing.Chaoyang,2"
,
"d1001,2018-10-03 14:38:16.800,12.30000,221,0.31000,Beijing.Chaoyang,2"
,
"d1002,2018-10-03 14:38:16.650,10.30000,218,0.25000,Beijing.Chaoyang,3"
,
"d1003,2018-10-03 14:38:05.500,11.80000,221,0.28000,Beijing.Haidian,2"
,
"d1003,2018-10-03 14:38:16.600,13.40000,223,0.29000,Beijing.Haidian,2"
,
"d1004,2018-10-03 14:38:05.000,10.80000,223,0.29000,Beijing.Haidian,3"
,
"d1004,2018-10-03 14:38:06.500,11.50000,221,0.35000,Beijing.Haidian,3"
);
}
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
<
T
>
ArrayList
<
T
>
toArray
(
T
v
)
{
ArrayList
<
T
>
result
=
new
ArrayList
<>();
result
.
add
(
v
);
return
result
;
}
// ANCHOR: insert
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) "
+
"TAGS (location BINARY(64), groupId INT)"
);
}
}
private
static
void
insertData
()
throws
SQLException
{
try
(
Connection
conn
=
getConnection
())
{
createTable
(
conn
);
String
psql
=
"INSERT INTO ? USING meters TAGS(?, ?) VALUES(?, ?, ?, ?)"
;
try
(
TSDBPreparedStatement
pst
=
(
TSDBPreparedStatement
)
conn
.
prepareStatement
(
psql
))
{
for
(
String
line
:
getRawData
())
{
String
[]
ps
=
line
.
split
(
","
);
// bind table name and tags
pst
.
setTableName
(
ps
[
0
]);
pst
.
setTagString
(
0
,
ps
[
5
]);
pst
.
setTagInt
(
1
,
Integer
.
valueOf
(
ps
[
6
]));
// bind values
pst
.
setTimestamp
(
0
,
tsToLongArray
(
ps
[
1
]));
pst
.
setFloat
(
1
,
toArray
(
Float
.
valueOf
(
ps
[
2
])));
pst
.
setInt
(
2
,
toArray
(
Integer
.
valueOf
(
ps
[
3
])));
pst
.
setFloat
(
3
,
toArray
(
Float
.
valueOf
(
ps
[
4
])));
pst
.
columnDataAddBatch
();
}
pst
.
columnDataExecuteBatch
();
}
}
}
// ANCHOR_END: insert
public
static
void
main
(
String
[]
args
)
throws
SQLException
{
insertData
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录