diff --git a/docs/zh/08-develop/01-connect/_connect_python.mdx b/docs/zh/08-develop/01-connect/_connect_python.mdx index b331f4648c..222c92fc0d 100644 --- a/docs/zh/08-develop/01-connect/_connect_python.mdx +++ b/docs/zh/08-develop/01-connect/_connect_python.mdx @@ -1,3 +1,14 @@ -```python title="原生连接" -{{#include docs/examples/python/connect_example.py}} +```text +[+]://[[:@]:][/][?=[&=]] +|------------|---|-----------|-----------|------|------|------------|-----------------------| +| protocol | | username | password | host | port | database | params | +- **protocol**: Display using websocket protocol to establish connection. +- **username/password**: Database's username and password. +- **host/port**: Declare host and port. eg. `localhost:6041` +- **database**: Optional, use to specify database name. +- **params**: Other parameters. Like cloud Token. +``` + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} ``` diff --git a/docs/zh/08-develop/01-connect/index.md b/docs/zh/08-develop/01-connect/index.md index c89a92dfb6..68f0a22d0f 100644 --- a/docs/zh/08-develop/01-connect/index.md +++ b/docs/zh/08-develop/01-connect/index.md @@ -264,36 +264,105 @@ phpize && ./configure --enable-swoole && make -j && make install 在执行这一步之前,请确保有一个正在运行的,且可以访问到的 TDengine,而且服务端的 FQDN 配置正确。以下示例代码,都假设 TDengine 安装在本机,且 FQDN(默认 localhost) 和 serverPort(默认 6030) 都使用默认配置。 - - - - - - - - - - - - - - - - - - - - - - - - - - - - +### Websocket 连接 + + + + + ```java + {{#include docs/examples/java/src/main/java/com/taos/example/WSConnectExample.java:main}} + ``` + + + + ```python + {{#include docs/examples/python/connect_websocket_examples.py:connect}} + ``` + + + + + + + + + ```js + {{#include docs/examples/node/websocketexample/sql_example.js:createConnect}} + ``` + + + + + + + + + + + + + +### 原生连接 + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### REST 连接 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + :::tip 如果建立连接失败,大部分情况下是 FQDN 或防火墙的配置不正确,详细的排查方法请看[《常见问题及反馈》](https://docs.taosdata.com/train-faq/faq)中的“遇到错误 Unable to establish connection, 我怎么办?” diff --git a/docs/zh/08-develop/05-stmt.md b/docs/zh/08-develop/05-stmt.md index f156e4675a..f613331614 100644 --- a/docs/zh/08-develop/05-stmt.md +++ b/docs/zh/08-develop/05-stmt.md @@ -9,7 +9,8 @@ import TabItem from "@theme/TabItem"; 通过参数绑定方式写入数据时,能避免SQL语法解析的资源消耗,从而显著提升写入性能。示例代码如下。 - +## Websocket + ```java @@ -65,4 +66,63 @@ public class WSParameterBindingBasicDemo { } ``` + + + ```python + {{#include docs/examples/python/connect_websocket_examples.py:connect}} + ``` + + + + + + + + + + ```js + {{#include docs/examples/node/websocketexample/sql_example.js:createConnect}} + ``` + + + + + + + + + + + + + + + +## 原生 + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/docs/zh/08-develop/07-tmq.md b/docs/zh/08-develop/07-tmq.md index 29ac760abf..39b760c3dc 100644 --- a/docs/zh/08-develop/07-tmq.md +++ b/docs/zh/08-develop/07-tmq.md @@ -9,11 +9,12 @@ import TabItem from "@theme/TabItem"; TDengine提供了类似Kafka的数据订阅功能。本章以 WebSocket 连接方式为例,介绍数据订阅的相关API以及使用方法。 + ## 创建主题 创建主题的示例代码如下。 - - +### Websocket + ```java @@ -22,8 +23,86 @@ Statement statement = connection.createStatement(); statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); ``` + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### 原生 + + + +```java +Connection connection = DriverManager.getConnection(url, properties); +Statement statement = connection.createStatement(); +statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); +``` + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + 上述代码将使用SQL“select ts, current, voltage, phase, groupId, location from meters”创建一个名为topic_meters的订阅。使用该订阅所获取的消息中的每条记录都由该查询语句所选择的列组成。 **注意** @@ -33,8 +112,8 @@ statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, c - 时间顺序限制:订阅查询只能按照时间正序查询数据。 ## 创建消费者 - - +### Websocket + ```java @@ -52,6 +131,95 @@ config.setProperty("value.deserializer.encoding", "UTF-8"); this.consumer = new TaosConsumer<(config); ``` + + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### 原生 + + + +```java +Properties config = new Properties(); +config.setProperty("td.connect.type", "ws"); +config.setProperty("bootstrap.servers", "localhost:6041"); +config.setProperty("auto.offset.reset", "latest"); +config.setProperty("msg.with.table.name", "true"); +config.setProperty("enable.auto.commit", "true"); +config.setProperty("auto.commit.interval.ms", "1000"); +config.setProperty("group.id", "group1"); +config.setProperty("client.id", "1"); +config.setProperty("value.deserializer", "com.taosdata.example.AbsConsumerLoop$ResultDeserializer"); +config.setProperty("value.deserializer.encoding", "UTF-8"); + +this.consumer = new TaosConsumer<(config); +``` + + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + @@ -66,8 +234,8 @@ this.consumer = new TaosConsumer<(config); ## 订阅消费数据 订阅消费数据的示例代码如下 - - +### Websocket + ```java @@ -79,14 +247,96 @@ while (!shutdown.get()) { } } ``` + + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### 原生 + + + +```java +while (!shutdown.get()) { + ConsumerRecords + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + poll 每次调用获取一个消息,一个消息中可能有多个记录,需要循环处理。 ## 指定订阅的 Offset - - +### Websocket + ```java @@ -105,14 +355,104 @@ void seek(TopicPartition partition, long offset) throws SQLException; void seekToBeginning(Collection + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### 原生 + + + +```java +// 获取订阅的 topicPartition +Set + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + ## 提交 Offset 当 `enable.auto.commit` 为 false 时,可以手动提交 offset。 - - +### Websocket + ```java @@ -123,11 +463,98 @@ void commitAsync(OffsetCommitCallback + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -## 取消订阅和关闭消费 +### 原生 + + - +```java +void commitSync() throws SQLException; +void commitSync(Map + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +## 取消订阅和关闭消费 +### Websocket + ```java @@ -137,4 +564,88 @@ consumer.unsubscribe(); consumer.close() ``` + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +### 原生 + + + +```java +// 取消订阅 +consumer.unsubscribe(); +// 关闭消费 +consumer.close() +``` + + + + +```python +{{#include docs/examples/python/connect_websocket_examples.py:connect}} +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file