Skip to content

Commit

Permalink
Merge pull request #28423 from taosdata/feat/TD-32221-main
Browse files Browse the repository at this point in the history
java websocket use own protocal
  • Loading branch information
DuanKuanJun authored Oct 23, 2024
2 parents c7e4d41 + 343421f commit 2897a87
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 124 deletions.
2 changes: 1 addition & 1 deletion docs/examples/JDBC/JDBCDemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.3</version>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.locationtech.jts</groupId>
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/JDBC/connectionPools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.3</version>
<version>3.4.0</version>
</dependency>
<!-- druid -->
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/JDBC/consumer-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.3</version>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/JDBC/taosdemo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.3</version>
<version>3.4.0</version>
<!-- <scope>system</scope>-->
<!-- <systemPath>${project.basedir}/src/main/resources/lib/taos-jdbcdriver-2.0.15-dist.jar</systemPath>-->
</dependency>
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.3.3</version>
<version>3.4.0</version>
</dependency>
<!-- ANCHOR_END: dep-->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class SchemalessWsTest {
private static final String jsonDemo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";

public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS-RS://" + host + ":6041?user=root&password=taosdata&batchfetch=true";
try(Connection connection = DriverManager.getConnection(url)){
final String url = "jdbc:TAOS-WS://" + host + ":6041?user=root&password=taosdata";
try (Connection connection = DriverManager.getConnection(url)) {
init(connection);
AbstractConnection conn = connection.unwrap(AbstractConnection.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class WSConnectExample {
public static void main(String[] args) throws Exception {
// use
// String jdbcUrl =
// "jdbc:TAOS-RS://localhost:6041/dbName?user=root&password=taosdata&batchfetch=true";
// "jdbc:TAOS-WS://localhost:6041/dbName?user=root&password=taosdata";
// if you want to connect a specified database named "dbName".
String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata&batchfetch=true";
String jdbcUrl = "jdbc:TAOS-WS://localhost:6041?user=root&password=taosdata";
Properties connProps = new Properties();
connProps.setProperty(TSDBDriver.PROPERTY_KEY_ENABLE_AUTO_RECONNECT, "true");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class WSParameterBindingBasicDemo {

public static void main(String[] args) throws SQLException {

String jdbcUrl = "jdbc:TAOS-RS://" + host + ":6041/?batchfetch=true";
String jdbcUrl = "jdbc:TAOS-WS://" + host + ":6041";
try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
init(conn);

Expand All @@ -40,7 +40,7 @@ public static void main(String[] args) throws SQLException {
pstmt.setFloat(4, random.nextFloat());
pstmt.addBatch();
}
int [] exeResult = pstmt.executeBatch();
int[] exeResult = pstmt.executeBatch();
// you can check exeResult here
System.out.println("Successfully inserted " + exeResult.length + " rows to power.meters.");
}
Expand All @@ -60,7 +60,8 @@ private static void init(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE DATABASE IF NOT EXISTS power");
stmt.execute("USE power");
stmt.execute("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
stmt.execute(
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WSParameterBindingFullDemo {

public static void main(String[] args) throws SQLException {

String jdbcUrl = "jdbc:TAOS-RS://" + host + ":6041/?batchfetch=true";
String jdbcUrl = "jdbc:TAOS-WS://" + host + ":6041/";

try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {

Expand All @@ -51,8 +51,10 @@ public static void main(String[] args) throws SQLException {
stmtAll(conn);

} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert data using stmt, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
// handle any errors, please refer to the JDBC specifications for detailed
// exceptions info
System.out.println("Failed to insert data using stmt, ErrCode:" + ex.getErrorCode() + "; ErrMessage: "
+ ex.getMessage());
throw ex;
} catch (Exception ex) {
System.out.println("Failed to insert data using stmt, ErrMessage: " + ex.getMessage());
Expand Down Expand Up @@ -104,30 +106,29 @@ private static void stmtAll(Connection conn) throws SQLException {
pstmt.setTagBoolean(3, true);
pstmt.setTagString(4, "binary_value");
pstmt.setTagNString(5, "nchar_value");
pstmt.setTagVarbinary(6, new byte[]{(byte) 0x98, (byte) 0xf4, 0x6e});
pstmt.setTagGeometry(7, new byte[]{
pstmt.setTagVarbinary(6, new byte[] { (byte) 0x98, (byte) 0xf4, 0x6e });
pstmt.setTagGeometry(7, new byte[] {
0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x59,
0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x59, 0x40});
0x00, 0x00, 0x00, 0x59, 0x40 });

long current = System.currentTimeMillis();


pstmt.setTimestamp(1, new Timestamp(current));
pstmt.setInt(2, 1);
pstmt.setDouble(3, 1.1);
pstmt.setBoolean(4, true);
pstmt.setString(5, "binary_value");
pstmt.setNString(6, "nchar_value");
pstmt.setVarbinary(7, new byte[]{(byte) 0x98, (byte) 0xf4, 0x6e});
pstmt.setGeometry(8, new byte[]{
pstmt.setVarbinary(7, new byte[] { (byte) 0x98, (byte) 0xf4, 0x6e });
pstmt.setGeometry(8, new byte[] {
0x01, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x59,
0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x59, 0x40});
0x00, 0x00, 0x00, 0x59, 0x40 });
pstmt.addBatch();
pstmt.executeBatch();
System.out.println("Successfully inserted rows to example_all_type_stmt.ntb");
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/07-develop/01-connect/_connect_rust.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
```

:::note
对于 Rust 连接器, 连接方式的不同只体现在使用的特性不同。如果启用了 "ws" 特性,那么只有 Websocket 的实现会被编译进来。
对于 Rust 连接器, 连接方式的不同只体现在使用的特性不同。如果启用了 "ws" 特性,那么只有 WebSocket 的实现会被编译进来。

:::
26 changes: 12 additions & 14 deletions docs/zh/07-develop/01-connect/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TDengine 提供了丰富的应用程序开发接口,为了便于用户快速

1. 通过客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接,这种连接方式下文中简称 “原生连接”。
2. 通过 taosAdapter 组件提供的 REST API 建立与 taosd 的连接,这种连接方式下文中简称 “REST 连接”
3. 通过 taosAdapter 组件提供的 Websocket API 建立与 taosd 的连接,这种连接方式下文中简称 “Websocket 连接”
3. 通过 taosAdapter 组件提供的 WebSocket API 建立与 taosd 的连接,这种连接方式下文中简称 “WebSocket 连接”

![TDengine connection type](connection-type-zh.webp)

Expand All @@ -38,9 +38,9 @@ TDengine 提供了丰富的应用程序开发接口,为了便于用户快速
关键不同点在于:

1. 使用 原生连接,需要保证客户端的驱动程序 taosc 和服务端的 TDengine 版本配套。
2. 使用 REST 连接,用户无需安装客户端驱动程序 taosc,具有跨平台易用的优势,但是无法体验数据订阅和二进制数据类型等功能。另外与 原生连接 和 Websocket 连接相比,REST连接的性能最低。REST 接口是无状态的。在使用 REST 连接时,需要在 SQL 中指定表、超级表的数据库名称。
3. 使用 Websocket 连接,用户也无需安装客户端驱动程序 taosc。
4. 连接云服务实例,必须使用 REST 连接 或 Websocket 连接。
2. 使用 REST 连接,用户无需安装客户端驱动程序 taosc,具有跨平台易用的优势,但是无法体验数据订阅和二进制数据类型等功能。另外与 原生连接 和 WebSocket 连接相比,REST连接的性能最低。REST 接口是无状态的。在使用 REST 连接时,需要在 SQL 中指定表、超级表的数据库名称。
3. 使用 WebSocket 连接,用户也无需安装客户端驱动程序 taosc。
4. 连接云服务实例,必须使用 REST 连接 或 WebSocket 连接。

**推荐使用 WebSocket 连接**

Expand Down Expand Up @@ -126,7 +126,7 @@ TDengine 提供了丰富的应用程序开发接口,为了便于用户快速
```bash
pip3 install taos-ws-py
```
:::note 此安装包为 Websocket 连接器
:::note 此安装包为 WebSocket 连接器
- 同时安装 `taospy` 和 `taos-ws-py`
```bash
pip3 install taospy[ws]
Expand Down Expand Up @@ -182,7 +182,7 @@ taos = { version = "*"}
```

:::info
Rust 连接器通过不同的特性区分不同的连接方式。默认同时支持原生连接和 Websocket 连接,如果仅需要建立 Websocket 连接,可设置 `ws` 特性:
Rust 连接器通过不同的特性区分不同的连接方式。默认同时支持原生连接和 WebSocket 连接,如果仅需要建立 WebSocket 连接,可设置 `ws` 特性:

```toml
taos = { version = "*", default-features = false, features = ["ws"] }
Expand All @@ -201,7 +201,7 @@ taos = { version = "*", default-features = false, features = ["ws"] }
```
npm install @tdengine/websocket
```
:::note Node.js 目前只支持 Websocket 连接
:::note Node.js 目前只支持 WebSocket 连接
- **安装验证**
- 新建安装验证目录,例如:`~/tdengine-test`,下载 GitHub 上 [nodejsChecker.js 源代码](https://github.com/taosdata/TDengine/tree/main/docs/examples/node/websocketexample/nodejsChecker.js)到本地。
- 在命令行中执行以下命令。
Expand Down Expand Up @@ -271,12 +271,10 @@ dotnet add package TDengine.Connector
<TabItem label="Java" value="java">
Java 连接器建立连接的参数有 URL 和 Properties。
TDengine 的 JDBC URL 规范格式为:
`jdbc:[TAOS|TAOS-RS]://[host_name]:[port]/[database_name]?[user={user}|&password={password}|&charset={charset}|&cfgdir={config_dir}|&locale={locale}|&timezone={timezone}|&batchfetch={batchfetch}]`
`jdbc:[TAOS|TAOS-WS|TAOS-RS]://[host_name]:[port]/[database_name]?[user={user}|&password={password}|&charset={charset}|&cfgdir={config_dir}|&locale={locale}|&timezone={timezone}|&batchfetch={batchfetch}]`
URL 和 Properties 的详细参数说明和如何使用详见 [url 规范](../../reference/connector/java/#url-规范)
**注**:REST 连接中增加 `batchfetch` 参数并设置为 true,将开启 WebSocket 连接。
</TabItem>
<TabItem label="Python" value="python">
Python 连接器使用 `connect()` 方法来建立连接,下面是连接参数的具体说明:
Expand Down Expand Up @@ -387,8 +385,8 @@ DSN 的详细说明和如何使用详见 [连接功能](../../reference/connecto
- `reconnectIntervalMs`:重连间隔毫秒时间,默认为 2000。
</TabItem>
<TabItem label="C" value="c">
**Websocket 连接**
C/C++ 语言连接器 Websocket 连接方式使用 `ws_connect()` 函数用于建立与 TDengine 数据库的连接。其参数为 DSN 描述字符串,其基本结构如下:
**WebSocket 连接**
C/C++ 语言连接器 WebSocket 连接方式使用 `ws_connect()` 函数用于建立与 TDengine 数据库的连接。其参数为 DSN 描述字符串,其基本结构如下:

```text
<driver>[+<protocol>]://[[<username>:<password>@]<host>:<port>][/<database>][?<p1>=<v1>[&<p2>=<v2>]]
Expand Down Expand Up @@ -417,8 +415,8 @@ C/C++ 语言连接器原生连接方式使用 `taos_connect()` 函数用于建
</TabItem>
</Tabs>

### Websocket 连接
下面是各语言连接器建立 Websocket 连接代码样例。演示了如何使用 Websocket 连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。
### WebSocket 连接
下面是各语言连接器建立 WebSocket 连接代码样例。演示了如何使用 WebSocket 连接方式连接到 TDengine 数据库,并对连接设定一些参数。整个过程主要涉及到数据库连接的建立和异常处理。

<Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java">
Expand Down
16 changes: 8 additions & 8 deletions docs/zh/07-develop/02-sql.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ REST API:直接调用 `taosadapter` 提供的 REST API 接口,进行数据
</TabItem>
<TabItem label="Python" value="python">

```python title="Websocket 连接"
```python title="WebSocket 连接"
{{#include docs/examples/python/create_db_ws.py}}
```

Expand Down Expand Up @@ -69,7 +69,7 @@ REST API:直接调用 `taosadapter` 提供的 REST API 接口,进行数据
</TabItem>
<TabItem label="C" value="c">

```c title="Websocket 连接"
```c title="WebSocket 连接"
{{#include docs/examples/c-ws/create_db_demo.c:create_db_and_table}}
```

Expand Down Expand Up @@ -114,7 +114,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
</TabItem>
<TabItem label="Python" value="python">

```python title="Websocket 连接"
```python title="WebSocket 连接"
{{#include docs/examples/python/insert_ws.py}}
```

Expand Down Expand Up @@ -151,7 +151,7 @@ NOW 为系统内部函数,默认为客户端所在计算机当前时间。 NOW
</TabItem>
<TabItem label="C" value="c">

```c title="Websocket 连接"
```c title="WebSocket 连接"
{{#include docs/examples/c-ws/insert_data_demo.c:insert_data}}
```

Expand Down Expand Up @@ -189,7 +189,7 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \
</TabItem>
<TabItem label="Python" value="python">

```python title="Websocket 连接"
```python title="WebSocket 连接"
{{#include docs/examples/python/query_ws.py}}
```

Expand Down Expand Up @@ -230,7 +230,7 @@ rust 连接器还支持使用 **serde** 进行反序列化行为结构体的结
</TabItem>
<TabItem label="C" value="c">

```c title="Websocket 连接"
```c title="WebSocket 连接"
{{#include docs/examples/c-ws/query_data_demo.c:query_data}}
```

Expand Down Expand Up @@ -273,7 +273,7 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId
</TabItem>
<TabItem label="Python" value="python">

```python title="Websocket 连接"
```python title="WebSocket 连接"
{{#include docs/examples/python/reqid_ws.py}}
```

Expand Down Expand Up @@ -310,7 +310,7 @@ reqId 可用于请求链路追踪,reqId 就像分布式系统中的 traceId
</TabItem>
<TabItem label="C" value="c">

```c "Websocket 连接"
```c "WebSocket 连接"
{{#include docs/examples/c-ws/with_reqid_demo.c:with_reqid}}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/zh/07-develop/04-schemaless.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ st,t1=3,t2=4,t3=t3 c1=3i64,c6="passit" 1626006833640000000

:::

### Websocket 连接
### WebSocket 连接

<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/07-develop/05-stmt.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import TabItem from "@theme/TabItem";
- 执行批量插入操作,将这些数据行插入到对应的子表中。
3. 最后打印实际插入表中的行数。

## Websocket 连接
## WebSocket 连接
<Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java">
```java
Expand Down
Loading

0 comments on commit 2897a87

Please sign in to comment.