Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
959cf23b
T
TDengine
项目概览
taosdata
/
TDengine
接近 2 年 前同步成功
通知
1192
Star
22018
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看板
未验证
提交
959cf23b
编写于
8月 13, 2022
作者:
H
huolibo
提交者:
GitHub
8月 13, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: add tmq sample and doc (#16082)
* docs: modify jdbc version * docs: add tmq sample
上级
1531cf44
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
160 addition
and
63 deletion
+160
-63
docs/en/07-develop/_sub_java.mdx
docs/en/07-develop/_sub_java.mdx
+2
-0
docs/en/14-reference/03-connector/java.mdx
docs/en/14-reference/03-connector/java.mdx
+22
-15
docs/examples/java/pom.xml
docs/examples/java/pom.xml
+1
-1
docs/examples/java/src/main/java/com/taos/example/Meters.java
.../examples/java/src/main/java/com/taos/example/Meters.java
+62
-0
docs/examples/java/src/main/java/com/taos/example/MetersDeserializer.java
...va/src/main/java/com/taos/example/MetersDeserializer.java
+6
-0
docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java
...es/java/src/main/java/com/taos/example/SubscribeDemo.java
+56
-44
docs/zh/07-develop/_sub_java.mdx
docs/zh/07-develop/_sub_java.mdx
+2
-0
docs/zh/14-reference/03-connector/java.mdx
docs/zh/14-reference/03-connector/java.mdx
+9
-3
未找到文件。
docs/en/07-develop/_sub_java.mdx
浏览文件 @
959cf23b
```java
```java
{{#include docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/MetersDeserializer.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/Meters.java}}
```
```
:::note
:::note
For now Java connector doesn't provide asynchronous subscription, but `TimerTask` can be used to achieve similar purpose.
For now Java connector doesn't provide asynchronous subscription, but `TimerTask` can be used to achieve similar purpose.
...
...
docs/en/14-reference/03-connector/java.mdx
浏览文件 @
959cf23b
...
@@ -41,19 +41,20 @@ Please refer to [Version Support List](/reference/connector#version-support).
...
@@ -41,19 +41,20 @@ Please refer to [Version Support List](/reference/connector#version-support).
TDengine currently supports timestamp, number, character, Boolean type, and the corresponding type conversion with Java is as follows:
TDengine currently supports timestamp, number, character, Boolean type, and the corresponding type conversion with Java is as follows:
| TDengine DataType | JDBCType (driver version < 2.0.24) | JDBCType (driver version > = 2.0.24) |
| ----------------- | ---------------------------------- | ------------------------------------ |
| TDengine DataType | JDBCType |
| TIMESTAMP | java.lang.Long | java.sql.Timestamp |
| ----------------- | ---------------------------------- |
| INT | java.lang.Integer | java.lang.Integer |
| TIMESTAMP | java.sql.Timestamp |
| BIGINT | java.lang.Long | java.lang.Long |
| INT | java.lang.Integer |
| FLOAT | java.lang.Float | java.lang.Float |
| BIGINT | java.lang.Long |
| DOUBLE | java.lang.Double | java.lang.Double |
| FLOAT | java.lang.Float |
| SMALLINT | java.lang.Short | java.lang.Short |
| DOUBLE | java.lang.Double |
| TINYINT | java.lang.Byte | java.lang.Byte |
| SMALLINT | java.lang.Short |
| BOOL | java.lang.Boolean | java.lang.Boolean |
| TINYINT | java.lang.Byte |
| BINARY | java.lang.String | byte array |
| BOOL | java.lang.Boolean |
| NCHAR | java.lang.String | java.lang.String |
| BINARY | byte array |
| JSON | - | java.lang.String |
| NCHAR | java.lang.String |
| JSON | java.lang.String |
**Note**: Only TAG supports JSON types
**Note**: Only TAG supports JSON types
...
@@ -81,7 +82,7 @@ Add following dependency in the `pom.xml` file of your Maven project:
...
@@ -81,7 +82,7 @@ Add following dependency in the `pom.xml` file of your Maven project:
<dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<artifactId>taos-jdbcdriver</artifactId>
<version>
2.0.**
</version>
<version>
3.0.0
</version>
</dependency>
</dependency>
```
```
...
@@ -845,7 +846,13 @@ Please refer to: [JDBC example](https://github.com/taosdata/TDengine/tree/develo
...
@@ -845,7 +846,13 @@ Please refer to: [JDBC example](https://github.com/taosdata/TDengine/tree/develo
**Cause**: Currently, TDengine only supports 64-bit JDK.
**Cause**: Currently, TDengine only supports 64-bit JDK.
**Solution**: Reinstall the 64-bit JDK. 4.
**Solution**: Reinstall the 64-bit JDK.
4. java.lang.NoSuchMethodError: setByteArray
**Cause**: taos-jdbcdriver version 3.* only supports TDengine 3.0 or above.
**Solution**: connect TDengine 2.* using taos-jdbcdriver 2.* version.
For other questions, please refer to [FAQ](/train-faq/faq)
For other questions, please refer to [FAQ](/train-faq/faq)
...
...
docs/examples/java/pom.xml
浏览文件 @
959cf23b
...
@@ -21,7 +21,7 @@
...
@@ -21,7 +21,7 @@
<dependency>
<dependency>
<groupId>
com.taosdata.jdbc
</groupId>
<groupId>
com.taosdata.jdbc
</groupId>
<artifactId>
taos-jdbcdriver
</artifactId>
<artifactId>
taos-jdbcdriver
</artifactId>
<version>
2.0.38
</version>
<version>
3.0.0
</version>
</dependency>
</dependency>
<!-- ANCHOR_END: dep-->
<!-- ANCHOR_END: dep-->
<dependency>
<dependency>
...
...
docs/examples/java/src/main/java/com/taos/example/Meters.java
0 → 100644
浏览文件 @
959cf23b
package
com.taos.example
;
import
java.sql.Timestamp
;
public
class
Meters
{
private
Timestamp
ts
;
private
float
current
;
private
int
voltage
;
private
int
groupid
;
private
String
location
;
public
Timestamp
getTs
()
{
return
ts
;
}
public
void
setTs
(
Timestamp
ts
)
{
this
.
ts
=
ts
;
}
public
float
getCurrent
()
{
return
current
;
}
public
void
setCurrent
(
float
current
)
{
this
.
current
=
current
;
}
public
int
getVoltage
()
{
return
voltage
;
}
public
void
setVoltage
(
int
voltage
)
{
this
.
voltage
=
voltage
;
}
public
int
getGroupid
()
{
return
groupid
;
}
public
void
setGroupid
(
int
groupid
)
{
this
.
groupid
=
groupid
;
}
public
String
getLocation
()
{
return
location
;
}
public
void
setLocation
(
String
location
)
{
this
.
location
=
location
;
}
@Override
public
String
toString
()
{
return
"Meters{"
+
"ts="
+
ts
+
", current="
+
current
+
", voltage="
+
voltage
+
", groupid="
+
groupid
+
", location='"
+
location
+
'\''
+
'}'
;
}
}
docs/examples/java/src/main/java/com/taos/example/MetersDeserializer.java
0 → 100644
浏览文件 @
959cf23b
package
com.taos.example
;
import
com.taosdata.jdbc.tmq.ReferenceDeserializer
;
public
class
MetersDeserializer
extends
ReferenceDeserializer
<
Meters
>
{
}
\ No newline at end of file
docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java
浏览文件 @
959cf23b
package
com.taos.example
;
package
com.taos.example
;
import
com.taosdata.jdbc.TSDBConnection
;
import
com.taosdata.jdbc.tmq.ConsumerRecords
;
import
com.taosdata.jdbc.TSDBDriver
;
import
com.taosdata.jdbc.tmq.TMQConstants
;
import
com.taosdata.jdbc.TSDBResultSet
;
import
com.taosdata.jdbc.tmq.TaosConsumer
;
import
com.taosdata.jdbc.TSDBSubscribe
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.DriverManager
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.time.Duration
;
import
java.util.Collections
;
import
java.util.Properties
;
import
java.util.Properties
;
import
java.util.concurrent.TimeUnit
;
import
java.util.Timer
;
import
java.util.TimerTask
;
import
java.util.concurrent.atomic.AtomicBoolean
;
public
class
SubscribeDemo
{
public
class
SubscribeDemo
{
private
static
final
String
topic
=
"topic-meter-current-bg-10"
;
private
static
final
String
TOPIC
=
"tmq_topic"
;
private
static
final
String
sql
=
"select * from meters where current > 10"
;
private
static
final
String
DB_NAME
=
"meters"
;
private
static
final
AtomicBoolean
shutdown
=
new
AtomicBoolean
(
false
);
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
Connection
connection
=
null
;
Timer
timer
=
new
Timer
();
TSDBSubscribe
subscribe
=
null
;
timer
.
schedule
(
new
TimerTask
()
{
public
void
run
()
{
shutdown
.
set
(
true
);
}
},
3_000
);
try
{
try
{
// prepare
Class
.
forName
(
"com.taosdata.jdbc.TSDBDriver"
);
Class
.
forName
(
"com.taosdata.jdbc.TSDBDriver"
);
String
jdbcUrl
=
"jdbc:TAOS://127.0.0.1:6030/?user=root&password=taosdata"
;
Connection
connection
=
DriverManager
.
getConnection
(
jdbcUrl
);
try
(
Statement
statement
=
connection
.
createStatement
())
{
statement
.
executeUpdate
(
"drop topic if exists "
+
TOPIC
);
statement
.
executeUpdate
(
"drop database if exists "
+
DB_NAME
);
statement
.
executeUpdate
(
"create database "
+
DB_NAME
);
statement
.
executeUpdate
(
"use "
+
DB_NAME
);
statement
.
executeUpdate
(
"CREATE TABLE `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT) TAGS (`groupid` INT, `location` BINARY(16))"
);
statement
.
executeUpdate
(
"CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')"
);
statement
.
executeUpdate
(
"INSERT INTO `d0` values(now - 10s, 0.32, 116)"
);
statement
.
executeUpdate
(
"INSERT INTO `d0` values(now - 8s, NULL, NULL)"
);
statement
.
executeUpdate
(
"INSERT INTO `d1` USING `meters` TAGS(1, 'San Francisco') values(now - 9s, 10.1, 119)"
);
statement
.
executeUpdate
(
"INSERT INTO `d1` values (now-8s, 10, 120) (now - 6s, 10, 119) (now - 4s, 11.2, 118)"
);
// create topic
statement
.
executeUpdate
(
"create topic "
+
TOPIC
+
" as select * from meters"
);
}
// create consumer
Properties
properties
=
new
Properties
();
Properties
properties
=
new
Properties
();
properties
.
setProperty
(
TSDBDriver
.
PROPERTY_KEY_CHARSET
,
"UTF-8"
);
properties
.
setProperty
(
TMQConstants
.
BOOTSTRAP_SERVERS
,
"127.0.0.1:6030"
);
properties
.
setProperty
(
TSDBDriver
.
PROPERTY_KEY_TIME_ZONE
,
"UTC-8"
);
properties
.
setProperty
(
TMQConstants
.
MSG_WITH_TABLE_NAME
,
"true"
);
String
jdbcUrl
=
"jdbc:TAOS://127.0.0.1:6030/power?user=root&password=taosdata"
;
properties
.
setProperty
(
TMQConstants
.
ENABLE_AUTO_COMMIT
,
"true"
);
connection
=
DriverManager
.
getConnection
(
jdbcUrl
,
properties
);
properties
.
setProperty
(
TMQConstants
.
GROUP_ID
,
"test"
);
// create subscribe
properties
.
setProperty
(
TMQConstants
.
VALUE_DESERIALIZER
,
subscribe
=
((
TSDBConnection
)
connection
).
subscribe
(
topic
,
sql
,
true
);
"com.taosdata.jdbc.MetersDeserializer"
);
int
count
=
0
;
while
(
count
<
10
)
{
// poll data
// wait 1 second to avoid frequent calls to consume
try
(
TaosConsumer
<
Meters
>
consumer
=
new
TaosConsumer
<>(
properties
))
{
TimeUnit
.
SECONDS
.
sleep
(
1
);
consumer
.
subscribe
(
Collections
.
singletonList
(
TOPIC
));
// consume
while
(!
shutdown
.
get
())
{
TSDBResultSet
resultSet
=
subscribe
.
consume
();
ConsumerRecords
<
Meters
>
meters
=
consumer
.
poll
(
Duration
.
ofMillis
(
100
));
if
(
resultSet
==
null
)
{
for
(
Meters
meter
:
meters
)
{
continue
;
System
.
out
.
println
(
meter
);
}
ResultSetMetaData
metaData
=
resultSet
.
getMetaData
();
while
(
resultSet
.
next
())
{
int
columnCount
=
metaData
.
getColumnCount
();
for
(
int
i
=
1
;
i
<=
columnCount
;
i
++)
{
System
.
out
.
print
(
metaData
.
getColumnLabel
(
i
)
+
": "
+
resultSet
.
getString
(
i
)
+
"\t"
);
}
}
System
.
out
.
println
();
count
++;
}
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
ClassNotFoundException
|
SQL
Exception
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
finally
{
try
{
if
(
null
!=
subscribe
)
// close subscribe
subscribe
.
close
(
true
);
if
(
connection
!=
null
)
connection
.
close
();
}
catch
(
SQLException
throwable
)
{
throwable
.
printStackTrace
();
}
}
}
timer
.
cancel
();
}
}
}
}
\ No newline at end of file
docs/zh/07-develop/_sub_java.mdx
浏览文件 @
959cf23b
```java
```java
{{#include docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/SubscribeDemo.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/MetersDeserializer.java}}
{{#include docs/examples/java/src/main/java/com/taos/example/Meters.java}}
```
```
:::note
:::note
目前 Java 接口没有提供异步订阅模式,但用户程序可以通过创建 `TimerTask` 等方式达到同样的效果。
目前 Java 接口没有提供异步订阅模式,但用户程序可以通过创建 `TimerTask` 等方式达到同样的效果。
...
...
docs/zh/14-reference/03-connector/java.mdx
浏览文件 @
959cf23b
...
@@ -83,7 +83,7 @@ Maven 项目中,在 pom.xml 中添加以下依赖:
...
@@ -83,7 +83,7 @@ Maven 项目中,在 pom.xml 中添加以下依赖:
<dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<artifactId>taos-jdbcdriver</artifactId>
<version>
2.0.**
</version>
<version>
3.0.0
</version>
</dependency>
</dependency>
```
```
...
@@ -712,7 +712,7 @@ while(true) {
...
@@ -712,7 +712,7 @@ while(true) {
}
}
```
```
`poll`
方法返回一个结果集,其中包含从上次 `poll` 到目前为止的所有新数据。请务必
按需选择合理的调用 `poll` 的频率(如例子中的 `Duration.ofMillis(100)`),否则会给服务端造成不必要的压力。
`poll`
每次调用获取一个消息。请
按需选择合理的调用 `poll` 的频率(如例子中的 `Duration.ofMillis(100)`),否则会给服务端造成不必要的压力。
#### 关闭订阅
#### 关闭订阅
...
@@ -900,7 +900,13 @@ public static void main(String[] args) throws Exception {
...
@@ -900,7 +900,13 @@ public static void main(String[] args) throws Exception {
**解决方法**:重新安装 64 位 JDK。
**解决方法**:重新安装 64 位 JDK。
4. 其它问题请参考 [FAQ](../../../train-faq/faq)
4. java.lang.NoSuchMethodError: setByteArray
**原因**:taos-jdbcdriver 3.* 版本仅支持 TDengine 3.0 及以上版本。
**解决方法**: 使用 taos-jdbcdriver 2.* 版本连接 TDengine 2.* 版本。
其它问题请参考 [FAQ](../../../train-faq/faq)
## API 参考
## API 参考
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录