Merge branch 'feat/TD-30813-2' of https://github.com/taosdata/TDengine into feat/TD-30813-2

This commit is contained in:
Minglei Jin 2024-08-21 15:30:54 +08:00
commit f0bd7af9cf
292 changed files with 7275 additions and 4305 deletions

1
.gitignore vendored
View File

@ -134,3 +134,4 @@ tags
.clangd .clangd
*CMakeCache* *CMakeCache*
*CMakeFiles* *CMakeFiles*
.history/

View File

@ -69,7 +69,7 @@ REST connection supports all platforms that can run Java.
After an error is reported, the error message and error code can be obtained through SQLException. After an error is reported, the error message and error code can be obtained through SQLException.
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcBasicDemo.java:jdbc_exception}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcBasicDemo.java:jdbc_exception}}
``` ```
There are four types of error codes that the JDBC client library can report: There are four types of error codes that the JDBC client library can report:
@ -150,7 +150,7 @@ TDengine currently supports timestamp, number, character, Boolean type, and the
Due to historical reasons, the BINARY type data in TDengine is not truly binary data and is no longer recommended for use. Please use VARBINARY type instead. Due to historical reasons, the BINARY type data in TDengine is not truly binary data and is no longer recommended for use. Please use VARBINARY type instead.
GEOMETRY type is binary data in little endian byte order, which complies with the WKB specification. For detailed information, please refer to [Data Type](../../taos-sql/data-type/) GEOMETRY type is binary data in little endian byte order, which complies with the WKB specification. For detailed information, please refer to [Data Type](../../taos-sql/data-type/)
For WKB specifications, please refer to [Well Known Binary (WKB)](https://libgeos.org/specifications/wkb/) For WKB specifications, please refer to [Well Known Binary (WKB)](https://libgeos.org/specifications/wkb/)
For Java connector, the jts library can be used to easily create GEOMETRY type objects, serialize them, and write them to TDengine. Here is an example [Geometry example](https://github.com/taosdata/TDengine/blob/3.0/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/GeometryDemo.java) For Java connector, the jts library can be used to easily create GEOMETRY type objects, serialize them, and write them to TDengine. Here is an example [Geometry example](https://github.com/taosdata/TDengine/blob/3.0/docs/examples/java/src/main/java/com/taos/example/GeometryDemo.java)
## Installation Steps ## Installation Steps
@ -395,7 +395,7 @@ For example, if you specify the password as `taosdata` in the URL and specify th
### Create database and tables ### Create database and tables
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcBasicDemo.java:create_db_and_table}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcBasicDemo.java:create_db_and_table}}
``` ```
> **Note**: If you do not use `USE power` to specify the database, all subsequent operations on the table need to add the database name as a prefix, such as power.meters. > **Note**: If you do not use `USE power` to specify the database, all subsequent operations on the table need to add the database name as a prefix, such as power.meters.
@ -403,7 +403,7 @@ For example, if you specify the password as `taosdata` in the URL and specify th
### Insert data ### Insert data
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcBasicDemo.java:insert_data}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcBasicDemo.java:insert_data}}
``` ```
> NOW is an internal function. The default is the current time of the client's computer. > NOW is an internal function. The default is the current time of the client's computer.
@ -412,7 +412,7 @@ For example, if you specify the password as `taosdata` in the URL and specify th
### Querying data ### Querying data
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcBasicDemo.java:query_data}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcBasicDemo.java:query_data}}
``` ```
> The query is consistent with operating a relational database. When using subscripts to get the contents of the returned fields, you have to start from 1. However, we recommend using the field names to get the values of the fields in the result set. > The query is consistent with operating a relational database. When using subscripts to get the contents of the returned fields, you have to start from 1. However, we recommend using the field names to get the values of the fields in the result set.
@ -422,7 +422,7 @@ For example, if you specify the password as `taosdata` in the URL and specify th
<RequestId /> <RequestId />
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcBasicDemo.java:with_reqid}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcBasicDemo.java:with_reqid}}
``` ```
### Writing data via parameter binding ### Writing data via parameter binding
@ -440,20 +440,20 @@ TDengine has significantly improved the bind APIs to support data writing (INSER
<TabItem value="native" label="native connection"> <TabItem value="native" label="native connection">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingBasicDemo.java:para_bind}} {{#include docs/examples/java/src/main/java/com/taos/example/ParameterBindingBasicDemo.java:para_bind}}
``` ```
This is the [Detailed Example](https://github.com/taosdata/TDengine/blob/main/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ParameterBindingFullDemo.java) This is the [Detailed Example](https://github.com/taosdata/TDengine/blob/main/docs/examples/java/src/main/java/com/taos/example/ParameterBindingFullDemo.java)
</TabItem> </TabItem>
<TabItem value="ws" label="WebSocket connection"> <TabItem value="ws" label="WebSocket connection">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingBasicDemo.java:para_bind}} {{#include docs/examples/java/src/main/java/com/taos/example/WSParameterBindingBasicDemo.java:para_bind}}
``` ```
This is the [Detailed Example](https://github.com/taosdata/TDengine/blob/main/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WSParameterBindingFullDemo.java) This is the [Detailed Example](https://github.com/taosdata/TDengine/blob/main/docs/examples/java/src/main/java/com/taos/example/WSParameterBindingFullDemo.java)
</TabItem> </TabItem>
</Tabs> </Tabs>
@ -503,14 +503,14 @@ TDengine supports schemaless writing. It is compatible with InfluxDB's Line Prot
<TabItem value="native" label="native connection"> <TabItem value="native" label="native connection">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java:schemaless}} {{#include docs/examples/java/src/main/java/com/taos/example/SchemalessJniTest.java:schemaless}}
``` ```
</TabItem> </TabItem>
<TabItem value="ws" label="WebSocket connection"> <TabItem value="ws" label="WebSocket connection">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java:schemaless}} {{#include docs/examples/java/src/main/java/com/taos/example/SchemalessWsTest.java:schemaless}}
``` ```
</TabItem> </TabItem>
@ -531,7 +531,7 @@ The TDengine Java client library supports subscription functionality with the fo
#### Create a Topic #### Create a Topic
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopImp.java:create_topic}} {{#include docs/examples/java/src/main/java/com/taos/example/ConsumerLoopImp.java:create_topic}}
``` ```
The preceding example uses the SQL statement `SELECT ts, current, voltage, phase, groupid, location FROM meters` and creates a topic named `topic_meters`. The preceding example uses the SQL statement `SELECT ts, current, voltage, phase, groupid, location FROM meters` and creates a topic named `topic_meters`.
@ -540,7 +540,7 @@ The preceding example uses the SQL statement `SELECT ts, current, voltage, phase
#### Create a Consumer #### Create a Consumer
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java:create_consumer}} {{#include docs/examples/java/src/main/java/com/taos/example/AbsConsumerLoop.java:create_consumer}}
``` ```
- bootstrap.servers: `ip:port` where the TDengine server is located, or `ip:port` where the taosAdapter is located if WebSocket connection is used. - bootstrap.servers: `ip:port` where the TDengine server is located, or `ip:port` where the taosAdapter is located if WebSocket connection is used.
@ -561,7 +561,7 @@ For more information, see [Consumer Parameters](../../../develop/tmq/#create-a-c
#### Subscribe to consume data #### Subscribe to consume data
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/AbsConsumerLoop.java:poll_data}} {{#include docs/examples/java/src/main/java/com/taos/example/AbsConsumerLoop.java:poll_data}}
``` ```
The parameters of the subscribe method are defined as: a list of topics to subscribe, and it supports subscribing to multiple topics at the same time. The parameters of the subscribe method are defined as: a list of topics to subscribe, and it supports subscribing to multiple topics at the same time.
@ -588,7 +588,7 @@ void seekToEnd(Collection<TopicPartition> partitions) throws SQLException;
Example usage is as follows. Example usage is as follows.
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerOffsetSeek.java:consumer_seek}} {{#include docs/examples/java/src/main/java/com/taos/example/ConsumerOffsetSeek.java:consumer_seek}}
``` ```
#### Commit offset #### Commit offset
@ -622,14 +622,14 @@ For more information, see [Data Subscription](../../../develop/tmq).
In addition to the native connection, the Java client library also supports subscribing via websocket. In addition to the native connection, the Java client library also supports subscribing via websocket.
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java:consumer_demo}} {{#include docs/examples/java/src/main/java/com/taos/example/ConsumerLoopFull.java:consumer_demo}}
``` ```
</TabItem> </TabItem>
<TabItem value="ws" label="WebSocket connection"> <TabItem value="ws" label="WebSocket connection">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java:consumer_demo}} {{#include docs/examples/java/src/main/java/com/taos/example/WsConsumerLoopFull.java:consumer_demo}}
``` ```
</TabItem> </TabItem>
@ -644,7 +644,7 @@ In addition to the native connection, the Java client library also supports subs
Example usage is as follows. Example usage is as follows.
```java ```java
{{#include examples/JDBC/connectionPools/src/main/java/com/taosdata/example/HikariDemo.java:connection_pool}} {{#include docs/examples/java/src/main/java/com/taos/example/HikariDemo.java:connection_pool}}
``` ```
> getConnection(), you need to call the close() method after you finish using it. It doesn't close the connection. It just puts it back into the connection pool. > getConnection(), you need to call the close() method after you finish using it. It doesn't close the connection. It just puts it back into the connection pool.
@ -655,7 +655,7 @@ Example usage is as follows.
Example usage is as follows. Example usage is as follows.
```java ```java
{{#include examples/JDBC/connectionPools/src/main/java/com/taosdata/example/DruidDemo.java:connection_pool}} {{#include docs/examples/java/src/main/java/com/taos/example/DruidDemo.java:connection_pool}}
``` ```
> For more questions about using druid, please see [Official Instructions](https://github.com/alibaba/druid). > For more questions about using druid, please see [Official Instructions](https://github.com/alibaba/druid).

View File

@ -448,6 +448,7 @@ Response body:
```bash ```bash
curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "select * from demo.d1001" 192.168.0.1:6041/rest/sql curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "select * from demo.d1001" 192.168.0.1:6041/rest/sql
curl -L -H "Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04" -d "select * from demo.d1001" 192.168.0.1:6041/rest/sql
``` ```
Response body: Response body:
@ -499,6 +500,7 @@ Response body:
```bash ```bash
curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "create database demo" 192.168.0.1:6041/rest/sql curl -L -H "Authorization: Basic cm9vdDp0YW9zZGF0YQ==" -d "create database demo" 192.168.0.1:6041/rest/sql
curl -L -H "Authorization: Taosd /KfeAzX/f9na8qdtNZmtONryp201ma04bEl8LcvLUd7a8qdtNZmtONryp201ma04" -d "create database demo" 192.168.0.1:6041/rest/sql
``` ```
Response body: Response body:

View File

@ -69,7 +69,7 @@ This error indicates that the client could not connect to the server. Perform th
11. You can also use the TDengine CLI to diagnose network issues. For more information, see [Problem Diagnostics](https://docs.tdengine.com/operation/diagnose/). 11. You can also use the TDengine CLI to diagnose network issues. For more information, see [Problem Diagnostics](https://docs.tdengine.com/operation/diagnose/).
### 3. How can I resolve the "Unable to resolve FQDN" error? ### <a id='FQDN'>3. How can I resolve the "Unable to resolve FQDN" error? </a>
Clients and dnodes must be able to resolve the FQDN of each required node. You can confirm your configuration as follows: Clients and dnodes must be able to resolve the FQDN of each required node. You can confirm your configuration as follows:
@ -164,3 +164,7 @@ For more information, see [taosAdapter](https://docs.tdengine.com/reference/taos
OOM errors are thrown by the operating system when its memory, including swap, becomes insufficient and it needs to terminate processes to remain operational. Most OOM errors in TDengine occur for one of the following reasons: free memory is less than the value of `vm.min_free_kbytes` or free memory is less than the size of the request. If TDengine occupies reserved memory, an OOM error can occur even when free memory is sufficient. OOM errors are thrown by the operating system when its memory, including swap, becomes insufficient and it needs to terminate processes to remain operational. Most OOM errors in TDengine occur for one of the following reasons: free memory is less than the value of `vm.min_free_kbytes` or free memory is less than the size of the request. If TDengine occupies reserved memory, an OOM error can occur even when free memory is sufficient.
TDengine preallocates memory to each vnode. The number of vnodes per database is determined by the `vgroups` parameter, and the amount of memory per vnode is determined by the `buffer` parameter. To prevent OOM errors from occurring, ensure that you prepare sufficient memory on your hosts to support the number of vnodes that your deployment requires. Configure an appropriately sized swap space. If you continue to receive OOM errors, your SQL statements may be querying too much data for your system. TDengine Enterprise Edition includes optimized memory management that increases stability for enterprise customers. TDengine preallocates memory to each vnode. The number of vnodes per database is determined by the `vgroups` parameter, and the amount of memory per vnode is determined by the `buffer` parameter. To prevent OOM errors from occurring, ensure that you prepare sufficient memory on your hosts to support the number of vnodes that your deployment requires. Configure an appropriately sized swap space. If you continue to receive OOM errors, your SQL statements may be querying too much data for your system. TDengine Enterprise Edition includes optimized memory management that increases stability for enterprise customers.
### 14. How can I resolve the "some vnode/qnode/mnode(s) out of service" error?
The client has not configured FQDN for all servers. For example, the server has 3 nodes, while the client has only configured FQDN for 1 node. FQDN configuration refer to [How can I resolve the "Unable to resolve FQDN" error?](#FQDN)

View File

@ -12,11 +12,11 @@ int main() {
uint16_t port = 6030; // 0 means use the default port uint16_t port = 6030; // 0 means use the default port
TAOS *taos = taos_connect(host, user, passwd, db, port); TAOS *taos = taos_connect(host, user, passwd, db, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
printf("Connected to %s:%hu successfully.\n", host, port); fprintf(stdout, "Connected to %s:%hu successfully.\n", host, port);
/* put your code here for read and write */ /* put your code here for read and write */

View File

@ -33,7 +33,7 @@ static int DemoCreateDB() {
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_errstr(NULL)); taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
@ -43,14 +43,13 @@ static int DemoCreateDB() {
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, fprintf(stderr, "Failed to create database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(result));
taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
printf("Create database power successfully.\n"); fprintf(stdout, "Create database power successfully.\n");
// create table // create table
const char *sql = const char *sql =
@ -59,14 +58,13 @@ static int DemoCreateDB() {
result = taos_query(taos, sql); result = taos_query(taos, sql);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, fprintf(stderr, "Failed to create stable power.meters, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result));
taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
printf("Create stable power.meters successfully.\n"); fprintf(stdout, "Create stable power.meters successfully.\n");
// close & clean // close & clean
taos_close(taos); taos_close(taos);
@ -75,4 +73,6 @@ static int DemoCreateDB() {
// ANCHOR_END: create_db_and_table // ANCHOR_END: create_db_and_table
} }
int main(int argc, char *argv[]) { return DemoCreateDB(); } int main(int argc, char *argv[]) {
return DemoCreateDB();
}

View File

@ -23,50 +23,52 @@
#include "taos.h" #include "taos.h"
static int DemoInsertData() { static int DemoInsertData() {
// ANCHOR: insert_data // ANCHOR: insert_data
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_cleanup(); taos_errstr(NULL));
return -1; taos_cleanup();
} return -1;
}
// insert data, please make sure the database and table are already created // insert data, please make sure the database and table are already created
const char* sql = "INSERT INTO " \ const char *sql =
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " \ "INSERT INTO "
"VALUES " \ "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') "
"(NOW + 1a, 10.30000, 219, 0.31000) " \ "VALUES "
"(NOW + 2a, 12.60000, 218, 0.33000) " \ "(NOW + 1a, 10.30000, 219, 0.31000) "
"(NOW + 3a, 12.30000, 221, 0.31000) " \ "(NOW + 2a, 12.60000, 218, 0.33000) "
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " \ "(NOW + 3a, 12.30000, 221, 0.31000) "
"VALUES " \ "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') "
"(NOW + 1a, 10.30000, 218, 0.25000) "; "VALUES "
TAOS_RES *result = taos_query(taos, sql); "(NOW + 1a, 10.30000, 218, 0.25000) ";
code = taos_errno(result); TAOS_RES *result = taos_query(taos, sql);
if (code != 0) { code = taos_errno(result);
printf("Failed to insert data to power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); if (code != 0) {
fprintf(stderr, "Failed to insert data to power.meters, sql: %s, ErrCode: 0x%x, ErrMessage: %s\n.", sql, code, taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
taos_free_result(result);
// you can check affectedRows here
int rows = taos_affected_rows(result);
fprintf(stdout, "Successfully inserted %d rows into power.meters.\n", rows);
// close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return 0;
} // ANCHOR_END: insert_data
taos_free_result(result);
// you can check affectedRows here
int rows = taos_affected_rows(result);
printf("Successfully inserted %d rows into power.meters.\n", rows);
// close & clean
taos_close(taos);
taos_cleanup();
return 0;
// ANCHOR_END: insert_data
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

View File

@ -22,59 +22,56 @@
#include <string.h> #include <string.h>
#include "taos.h" #include "taos.h"
static int DemoQueryData() { static int DemoQueryData() {
// ANCHOR: query_data // ANCHOR: query_data
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_cleanup(); taos_errstr(NULL));
return -1; taos_cleanup();
} return -1;
}
// query data, please make sure the database and table are already created
const char *sql = "SELECT ts, current, location FROM power.meters limit 100";
TAOS_RES *result = taos_query(taos, sql);
code = taos_errno(result);
if (code != 0) {
fprintf(stderr, "Failed to query data from power.meters, sql: %s, ErrCode: 0x%x, ErrMessage: %s\n.", sql, code,
taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
TAOS_ROW row = NULL;
int rows = 0;
int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
// query data, please make sure the database and table are already created fprintf(stdout, "query successfully, got %d fields, the sql is: %s.\n", num_fields, sql);
const char* sql = "SELECT ts, current, location FROM power.meters limit 100";
TAOS_RES *result = taos_query(taos, sql); // fetch the records row by row
code = taos_errno(result); while ((row = taos_fetch_row(result))) {
if (code != 0) { // Add your data processing logic here
printf("Failed to query data from power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result));
rows++;
}
fprintf(stdout, "total rows: %d\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return 0;
} // ANCHOR_END: query_data
TAOS_ROW row = NULL;
int rows = 0;
int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
printf("fields: %d\n", num_fields);
printf("sql: %s, result:\n", sql);
// fetch the records row by row
while ((row = taos_fetch_row(result))) {
char temp[1024] = {0};
rows++;
taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp);
}
printf("total rows: %d\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos);
taos_cleanup();
return 0;
// ANCHOR_END: query_data
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

View File

@ -21,112 +21,119 @@
#include <string.h> #include <string.h>
#include "taos.h" #include "taos.h"
static int DemoSmlInsert() { static int DemoSmlInsert() {
// ANCHOR: schemaless // ANCHOR: schemaless
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_cleanup(); taos_errstr(NULL));
return -1; taos_cleanup();
} return -1;
}
// create database // create database
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); fprintf(stderr, "Failed to create database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
printf("Create database power successfully.\n");
// use database // use database
result = taos_query(taos, "USE power"); result = taos_query(taos, "USE power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to execute use power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); fprintf(stderr, "Failed to execute use power, ErrCode: 0x%x, ErrMessage: %s\n.", code, taos_errstr(result));
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
taos_free_result(result); taos_free_result(result);
// schemaless demo data // schemaless demo data
char * line_demo = "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639"; char *line_demo =
char * telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0"; "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 "
char * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}"; "1626006833639";
char *telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
char *json_demo =
"{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, "
"\"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
// influxdb line protocol // influxdb line protocol
char *lines[] = {line_demo}; char *lines[] = {line_demo};
result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to insert schemaless line data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); fprintf(stderr, "Failed to insert schemaless line data, data: %s, ErrCode: 0x%x, ErrMessage: %s\n.", line_demo, code,
taos_close(taos); taos_errstr(result));
taos_cleanup(); taos_close(taos);
return -1; taos_cleanup();
} return -1;
}
int rows = taos_affected_rows(result); int rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless line data successfully.\n", rows); fprintf(stdout, "Insert %d rows of schemaless line data successfully.\n", rows);
taos_free_result(result); taos_free_result(result);
// opentsdb telnet protocol // opentsdb telnet protocol
char *telnets[] = {telnet_demo}; char *telnets[] = {telnet_demo};
result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to insert schemaless telnet data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); fprintf(stderr, "Failed to insert schemaless telnet data, data: %s, ErrCode: 0x%x, ErrMessage: %s\n.", telnet_demo, code,
taos_close(taos); taos_errstr(result));
taos_cleanup(); taos_close(taos);
return -1; taos_cleanup();
} return -1;
}
rows = taos_affected_rows(result); rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless telnet data successfully.\n", rows); fprintf(stdout, "Insert %d rows of schemaless telnet data successfully.\n", rows);
taos_free_result(result); taos_free_result(result);
// opentsdb json protocol // opentsdb json protocol
char *jsons[1] = {0}; char *jsons[1] = {0};
// allocate memory for json data. can not use static memory. // allocate memory for json data. can not use static memory.
jsons[0] = malloc(1024); size_t size = 1024;
if (jsons[0] == NULL) { jsons[0] = malloc(size);
printf("Failed to allocate memory\n"); if (jsons[0] == NULL) {
taos_close(taos); fprintf(stderr, "Failed to allocate memory: %zu bytes.\n", size);
taos_cleanup(); taos_close(taos);
return -1; taos_cleanup();
} return -1;
(void)strncpy(jsons[0], json_demo, 1023); }
result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); (void)strncpy(jsons[0], json_demo, 1023);
code = taos_errno(result); result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED);
if (code != 0) { code = taos_errno(result);
if (code != 0) {
free(jsons[0]);
fprintf(stderr, "Failed to insert schemaless json data, Server: %s, ErrCode: 0x%x, ErrMessage: %s\n.", json_demo, code,
taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
free(jsons[0]); free(jsons[0]);
printf("Failed to insert schemaless json data, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result));
rows = taos_affected_rows(result);
fprintf(stdout, "Insert %d rows of schemaless json data successfully.\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return 0;
} // ANCHOR_END: schemaless
free(jsons[0]);
rows = taos_affected_rows(result);
printf("Insert %d rows of schemaless json data successfully.\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos);
taos_cleanup();
return 0;
// ANCHOR_END: schemaless
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

View File

@ -32,7 +32,7 @@ void executeSQL(TAOS *taos, const char *sql) {
TAOS_RES *res = taos_query(taos, sql); TAOS_RES *res = taos_query(taos, sql);
int code = taos_errno(res); int code = taos_errno(res);
if (code != 0) { if (code != 0) {
printf("%s\n", taos_errstr(res)); fprintf(stderr, "%s\n", taos_errstr(res));
taos_free_result(res); taos_free_result(res);
taos_close(taos); taos_close(taos);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -49,7 +49,7 @@ void executeSQL(TAOS *taos, const char *sql) {
*/ */
void checkErrorCode(TAOS_STMT *stmt, int code, const char *msg) { void checkErrorCode(TAOS_STMT *stmt, int code, const char *msg) {
if (code != 0) { if (code != 0) {
printf("%s. code: %d, error: %s\n", msg,code,taos_stmt_errstr(stmt)); fprintf(stderr, "%s. code: %d, error: %s\n", msg,code,taos_stmt_errstr(stmt));
taos_stmt_close(stmt); taos_stmt_close(stmt);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -74,7 +74,7 @@ void insertData(TAOS *taos) {
// init // init
TAOS_STMT *stmt = taos_stmt_init(taos); TAOS_STMT *stmt = taos_stmt_init(taos);
if (stmt == NULL) { if (stmt == NULL) {
printf("Failed to init taos_stmt, error: %s\n", taos_stmt_errstr(NULL)); fprintf(stderr, "Failed to init taos_stmt, error: %s\n", taos_stmt_errstr(NULL));
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// prepare // prepare
@ -159,7 +159,7 @@ void insertData(TAOS *taos) {
int affected = taos_stmt_affected_rows_once(stmt); int affected = taos_stmt_affected_rows_once(stmt);
total_affected += affected; total_affected += affected;
} }
printf("Successfully inserted %d rows to power.meters.\n", total_affected); fprintf(stdout, "Successfully inserted %d rows to power.meters.\n", total_affected);
taos_stmt_close(stmt); taos_stmt_close(stmt);
} }
@ -170,7 +170,7 @@ int main() {
uint16_t port = 6030; uint16_t port = 6030;
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View File

@ -26,6 +26,7 @@
volatile int thread_stop = 0; volatile int thread_stop = 0;
static int running = 1; static int running = 1;
static int count = 0;
const char* topic_name = "topic_meters"; const char* topic_name = "topic_meters";
typedef struct { typedef struct {
@ -40,13 +41,25 @@ typedef struct {
const char* auto_offset_reset; const char* auto_offset_reset;
} ConsumerConfig; } ConsumerConfig;
ConsumerConfig config = {
.enable_auto_commit = "true",
.auto_commit_interval_ms = "1000",
.group_id = "group1",
.client_id = "client1",
.td_connect_host = "localhost",
.td_connect_port = "6030",
.td_connect_user = "root",
.td_connect_pass = "taosdata",
.auto_offset_reset = "latest"
};
void* prepare_data(void* arg) { void* prepare_data(void* arg) {
const char *host = "localhost"; const char* host = "localhost";
const char *user = "root"; const char* user = "root";
const char *password = "taosdata"; const char* password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port); TAOS* pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { if (pConn == NULL) {
fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
@ -78,62 +91,46 @@ void* prepare_data(void* arg) {
} }
// ANCHOR: msg_process // ANCHOR: msg_process
static int32_t msg_process(TAOS_RES* msg) { int32_t msg_process(TAOS_RES* msg) {
char buf[1024]; // buf to store the row content
int32_t rows = 0; int32_t rows = 0;
const char* topicName = tmq_get_topic_name(msg); const char* topicName = tmq_get_topic_name(msg);
const char* dbName = tmq_get_db_name(msg); const char* dbName = tmq_get_db_name(msg);
int32_t vgroupId = tmq_get_vgroup_id(msg); int32_t vgroupId = tmq_get_vgroup_id(msg);
fprintf(stdout, "topic: %s\n", topicName); while (true) {
fprintf(stdout, "db: %s\n", dbName);
fprintf(stdout, "vgroup id: %d\n", vgroupId);
while (1) {
// get one row data from message // get one row data from message
TAOS_ROW row = taos_fetch_row(msg); TAOS_ROW row = taos_fetch_row(msg);
if (row == NULL) break; if (row == NULL) break;
// get the field information // Add your data processing logic here
TAOS_FIELD* fields = taos_fetch_fields(msg);
// get the number of fields
int32_t numOfFields = taos_field_count(msg);
// get the precision of the result
int32_t precision = taos_result_precision(msg);
rows++; rows++;
// print the row content
if (taos_print_row(buf, row, fields, numOfFields) < 0) {
fprintf(stderr, "Failed to print row\n");
break;
}
// print the precision and row content to the console
fprintf(stdout, "precision: %d, data: %s\n", precision, buf);
} }
return rows; return rows;
} }
// ANCHOR_END: msg_process // ANCHOR_END: msg_process
static int32_t init_env() { TAOS* init_env() {
const char *host = "localhost"; const char* host = "localhost";
const char *user = "root"; const char* user = "root";
const char *password = "taosdata"; const char* password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port); TAOS* pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { if (pConn == NULL) {
fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return NULL;
} }
TAOS_RES* pRes; TAOS_RES* pRes;
// drop database if exists // drop database if exists
fprintf(stdout, "Create database.\n");
pRes = taos_query(pConn, "DROP TOPIC IF EXISTS topic_meters"); pRes = taos_query(pConn, "DROP TOPIC IF EXISTS topic_meters");
code = taos_errno(pRes); code = taos_errno(pRes);
if (code != 0) { if (code != 0) {
fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
goto END;
} }
taos_free_result(pRes); taos_free_result(pRes);
@ -141,6 +138,7 @@ static int32_t init_env() {
code = taos_errno(pRes); code = taos_errno(pRes);
if (code != 0) { if (code != 0) {
fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
goto END;
} }
taos_free_result(pRes); taos_free_result(pRes);
@ -148,13 +146,12 @@ static int32_t init_env() {
pRes = taos_query(pConn, "CREATE DATABASE power PRECISION 'ms' WAL_RETENTION_PERIOD 3600"); pRes = taos_query(pConn, "CREATE DATABASE power PRECISION 'ms' WAL_RETENTION_PERIOD 3600");
code = taos_errno(pRes); code = taos_errno(pRes);
if (code != 0) { if (code != 0) {
fprintf(stderr, "Failed to create tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to create power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
goto END; goto END;
} }
taos_free_result(pRes); taos_free_result(pRes);
// create super table // create super table
fprintf(stdout, "Create super table.\n");
pRes = taos_query( pRes = taos_query(
pConn, pConn,
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS "
@ -164,56 +161,78 @@ static int32_t init_env() {
fprintf(stderr, "Failed to create super table meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to create super table meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
goto END; goto END;
} }
taos_free_result(pRes); taos_free_result(pRes);
taos_close(pConn);
return 0; return pConn;
END: END:
taos_free_result(pRes); taos_free_result(pRes);
taos_close(pConn); taos_close(pConn);
return -1; return NULL;
} }
int32_t create_topic() { void deinit_env(TAOS* pConn) {
fprintf(stdout, "Create topic.\n"); if (pConn)
TAOS_RES* pRes; taos_close(pConn);
const char *host = "localhost"; }
const char *user = "root";
const char *password = "taosdata"; int32_t create_topic(TAOS* pConn) {
uint16_t port = 6030; TAOS_RES* pRes;
int code = 0; int code = 0;
TAOS *pConn = taos_connect(host, user, password, NULL, port);
if (pConn == NULL) { if (!pConn) {
fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Invalid input parameter.\n");
taos_cleanup();
return -1; return -1;
} }
pRes = taos_query(pConn, "USE POWER"); pRes = taos_query(pConn, "USE power");
code = taos_errno(pRes); code = taos_errno(pRes);
if (taos_errno(pRes) != 0) { if (taos_errno(pRes) != 0) {
fprintf(stderr, "Failed to use tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to use power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
return -1; return -1;
} }
taos_free_result(pRes); taos_free_result(pRes);
pRes = taos_query( pRes = taos_query(pConn, "CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters");
pConn,
"CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters");
code = taos_errno(pRes); code = taos_errno(pRes);
if (code != 0) { if (code != 0) {
fprintf(stderr, "Failed to create topic topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); fprintf(stderr, "Failed to create topic topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
return -1; return -1;
} }
taos_free_result(pRes); taos_free_result(pRes);
return 0;
}
taos_close(pConn); int32_t drop_topic(TAOS* pConn) {
TAOS_RES* pRes;
int code = 0;
if (!pConn) {
fprintf(stderr, "Invalid input parameter.\n");
return -1;
}
pRes = taos_query(pConn, "USE power");
code = taos_errno(pRes);
if (taos_errno(pRes) != 0) {
fprintf(stderr, "Failed to use power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
return -1;
}
taos_free_result(pRes);
pRes = taos_query(pConn, "DROP TOPIC IF EXISTS topic_meters");
code = taos_errno(pRes);
if (code != 0) {
fprintf(stderr, "Failed to drop topic topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes));
return -1;
}
taos_free_result(pRes);
return 0; return 0;
} }
void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) { void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) {
fprintf(stdout, "tmq_commit_cb_print() code: %d, tmq: %p, param: %p\n", code, tmq, param); count +=1;
fprintf(stdout, "tmq_commit_cb_print() code: %d, tmq: %p, param: %p, count: %d.\n", code, tmq, param, count);
} }
// ANCHOR: create_consumer_1 // ANCHOR: create_consumer_1
@ -288,14 +307,14 @@ _end:
tmq_list_t* build_topic_list() { tmq_list_t* build_topic_list() {
// create a empty topic list // create a empty topic list
tmq_list_t* topicList = tmq_list_new(); tmq_list_t* topicList = tmq_list_new();
const char* topic_name = "topic_meters";
// append topic name to the list // append topic name to the list
int32_t code = tmq_list_append(topicList, topic_name); int32_t code = tmq_list_append(topicList, topic_name);
if (code) { if (code) {
// if failed, destroy the list and return NULL // if failed, destroy the list and return NULL
tmq_list_destroy(topicList); tmq_list_destroy(topicList);
fprintf(stderr, "Failed to create topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); fprintf(stderr, "Failed to create topic_list, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
return NULL; return NULL;
} }
// if success, return the list // if success, return the list
@ -314,8 +333,10 @@ void basic_consume_loop(tmq_t* tmq) {
TAOS_RES* tmqmsg = tmq_consumer_poll(tmq, timeout); TAOS_RES* tmqmsg = tmq_consumer_poll(tmq, timeout);
if (tmqmsg) { if (tmqmsg) {
msgCnt++; msgCnt++;
// process the message
// Add your data processing logic here
totalRows += msg_process(tmqmsg); totalRows += msg_process(tmqmsg);
// free the message // free the message
taos_free_result(tmqmsg); taos_free_result(tmqmsg);
} }
@ -326,7 +347,7 @@ void basic_consume_loop(tmq_t* tmq) {
} }
// print the result: total messages and total rows consumed // print the result: total messages and total rows consumed
fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows); fprintf(stdout, "%d msg consumed, include %d rows\n", msgCnt, totalRows);
} }
// ANCHOR_END: basic_consume_loop // ANCHOR_END: basic_consume_loop
@ -338,7 +359,8 @@ void consume_repeatly(tmq_t* tmq) {
// get the topic assignment // get the topic assignment
int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment); int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment);
if (code != 0 || pAssign == NULL || numOfAssignment == 0) { if (code != 0 || pAssign == NULL || numOfAssignment == 0) {
fprintf(stderr, "Failed to get assignment, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); fprintf(stderr, "Failed to get assignment, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
return; return;
} }
@ -348,11 +370,13 @@ void consume_repeatly(tmq_t* tmq) {
code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin);
if (code != 0) { if (code != 0) {
fprintf(stderr, "Failed to seek assignment %d to beginning %ld, ErrCode: 0x%x, ErrMessage: %s.\n", i, p->begin, code, tmq_err2str(code)); fprintf(stderr, "Failed to seek offset, topic: %s, groupId: %s, clientId: %s, vgId: %d, ErrCode: 0x%x, ErrMessage: %s.\n",
} else { topic_name, config.group_id, config.client_id, p->vgId, code, tmq_err2str(code));
fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); break;
} }
} }
if (code == 0)
fprintf(stdout, "Assignment seek to beginning successfully.\n");
// free the assignment array // free the assignment array
tmq_free_assignment(pAssign); tmq_free_assignment(pAssign);
@ -377,9 +401,9 @@ void manual_commit(tmq_t* tmq) {
totalRows += msg_process(tmqmsg); totalRows += msg_process(tmqmsg);
// commit the message // commit the message
int32_t code = tmq_commit_sync(tmq, tmqmsg); int32_t code = tmq_commit_sync(tmq, tmqmsg);
if (code) { if (code) {
fprintf(stderr, "Failed to commit message, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); fprintf(stderr, "Failed to commit offset, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
// free the message // free the message
taos_free_result(tmqmsg); taos_free_result(tmqmsg);
break; break;
@ -396,7 +420,7 @@ void manual_commit(tmq_t* tmq) {
} }
// print the result: total messages and total rows consumed // print the result: total messages and total rows consumed
fprintf(stderr, "%d msg consumed, include %d rows.\n", msgCnt, totalRows); fprintf(stdout, "%d msg consumed, include %d rows.\n", msgCnt, totalRows);
} }
// ANCHOR_END: manual_commit // ANCHOR_END: manual_commit
@ -404,11 +428,14 @@ int main(int argc, char* argv[]) {
int32_t code; int32_t code;
pthread_t thread_id; pthread_t thread_id;
if (init_env() < 0) { TAOS* pConn = init_env();
if (pConn == NULL) {
fprintf(stderr, "Failed to init env.\n");
return -1; return -1;
} }
if (create_topic() < 0) { if (create_topic(pConn) < 0) {
fprintf(stderr, "Failed to create topic.\n");
return -1; return -1;
} }
@ -417,25 +444,15 @@ int main(int argc, char* argv[]) {
return -1; return -1;
} }
ConsumerConfig config = {
.enable_auto_commit = "true",
.auto_commit_interval_ms = "1000",
.group_id = "group1",
.client_id = "client1",
.td_connect_host = "localhost",
.td_connect_port = "6030",
.td_connect_user = "root",
.td_connect_pass = "taosdata",
.auto_offset_reset = "latest"
};
// ANCHOR: create_consumer_2 // ANCHOR: create_consumer_2
tmq_t* tmq = build_consumer(&config); tmq_t* tmq = build_consumer(&config);
if (NULL == tmq) { if (NULL == tmq) {
fprintf(stderr, "Failed to create native consumer, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); fprintf(stderr, "Failed to create native consumer, host: %s, groupId: %s, , clientId: %s.\n",
config.td_connect_host, config.group_id, config.client_id);
return -1; return -1;
} else { } else {
fprintf(stdout, "Create consumer successfully, host: %s, groupId: %s, , clientId: %s.\n", config.td_connect_host, config.group_id, config.client_id); fprintf(stdout, "Create consumer successfully, host: %s, groupId: %s, clientId: %s.\n",
config.td_connect_host, config.group_id, config.client_id);
} }
// ANCHOR_END: create_consumer_2 // ANCHOR_END: create_consumer_2
@ -443,12 +460,14 @@ int main(int argc, char* argv[]) {
// ANCHOR: subscribe_3 // ANCHOR: subscribe_3
tmq_list_t* topic_list = build_topic_list(); tmq_list_t* topic_list = build_topic_list();
if (NULL == topic_list) { if (NULL == topic_list) {
fprintf(stderr, "Failed to create topic_list.\n"); fprintf(stderr, "Failed to create topic_list, topic: %s, groupId: %s, clientId: %s.\n",
topic_name, config.group_id, config.client_id);
return -1; return -1;
} }
if ((code = tmq_subscribe(tmq, topic_list))) { if ((code = tmq_subscribe(tmq, topic_list))) {
fprintf(stderr, "Failed to subscribe topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); fprintf(stderr, "Failed to subscribe topic_list, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
} else { } else {
fprintf(stdout, "Subscribe topics successfully.\n"); fprintf(stdout, "Subscribe topics successfully.\n");
} }
@ -466,22 +485,30 @@ int main(int argc, char* argv[]) {
// unsubscribe the topic // unsubscribe the topic
code = tmq_unsubscribe(tmq); code = tmq_unsubscribe(tmq);
if (code) { if (code) {
fprintf(stderr, "Failed to unsubscribe consumer, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); fprintf(stderr, "Failed to unsubscribe consumer, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
} else { } else {
fprintf(stderr, "Consumer unsubscribed successfully.\n"); fprintf(stdout, "Consumer unsubscribed successfully.\n");
} }
// close the consumer // close the consumer
code = tmq_consumer_close(tmq); code = tmq_consumer_close(tmq);
if (code) { if (code) {
fprintf(stderr, "Failed to close consumer: %s.\n", tmq_err2str(code)); fprintf(stderr, "Failed to close consumer, topic: %s, groupId: %s, clientId: %s, ErrCode: 0x%x, ErrMessage: %s.\n",
topic_name, config.group_id, config.client_id, code, tmq_err2str(code));
} else { } else {
fprintf(stderr, "Consumer closed successfully.\n"); fprintf(stdout, "Consumer closed successfully.\n");
} }
// ANCHOR_END: unsubscribe_and_close // ANCHOR_END: unsubscribe_and_close
thread_stop = 1; thread_stop = 1;
pthread_join(thread_id, NULL); pthread_join(thread_id, NULL);
if (drop_topic(pConn) < 0) {
fprintf(stderr, "Failed to drop topic.\n");
return -1;
}
deinit_env(pConn);
return 0; return 0;
} }

View File

@ -23,59 +23,56 @@
#include "taos.h" #include "taos.h"
static int DemoWithReqId() { static int DemoWithReqId() {
// ANCHOR: with_reqid // ANCHOR: with_reqid
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); fprintf(stderr, "Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL));
taos_cleanup(); taos_cleanup();
return -1; return -1;
} }
const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; const char *sql = "SELECT ts, current, location FROM power.meters limit 1";
// query data with reqid // query data with reqid
long reqid = 3L; long reqid = 3L;
TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid);
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to execute sql with reqId: %ld, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); fprintf(stderr, "Failed to execute sql with reqId: %ld, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, code, taos_errstr(result));
taos_close(taos);
taos_cleanup();
return -1;
}
TAOS_ROW row = NULL;
int rows = 0;
int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
fprintf(stdout, "query successfully, got %d fields, the sql is: %s.\n", num_fields, sql);
// fetch the records row by row
while ((row = taos_fetch_row(result))) {
// Add your data processing logic here
rows++;
}
fprintf(stdout, "total rows: %d\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return 0;
} // ANCHOR_END: with_reqid
TAOS_ROW row = NULL;
int rows = 0;
int num_fields = taos_field_count(result);
TAOS_FIELD *fields = taos_fetch_fields(result);
printf("fields: %d\n", num_fields);
printf("sql: %s, result:\n", sql);
// fetch the records row by row
while ((row = taos_fetch_row(result))) {
char temp[1024] = {0};
rows++;
taos_print_row(temp, row, fields, num_fields);
printf("%s\n", temp);
}
printf("total rows: %d\n", rows);
taos_free_result(result);
// close & clean
taos_close(taos);
taos_cleanup();
return 0;
// ANCHOR_END: with_reqid
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
return DemoWithReqId(); return DemoWithReqId();
} }

View File

@ -42,16 +42,16 @@ namespace TDengineExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to insert data with schemaless; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to insert data with schemaless, ErrCode: " + e.Code + ", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to insert data with schemaless; Err:" + e.Message); Console.WriteLine("Failed to insert data with schemaless, ErrMessage:" + e.Message);
throw; throw;
} }
} }
// ANCHOR_END: main // ANCHOR_END: main
} }
} }

View File

@ -62,16 +62,16 @@ namespace TDengineExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to insert to table meters using stmt, ErrCode: " + e.Code + ", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to insert to table meters using stmt, ErrMessage: " + e.Message);
throw; throw;
} }
} }
// ANCHOR_END: main // ANCHOR_END: main
} }
} }

View File

@ -6,6 +6,11 @@ namespace TMQExample
{ {
internal class SubscribeDemo internal class SubscribeDemo
{ {
private static string _host = "";
private static string _groupId = "";
private static string _clientId = "";
private static string _topic = "";
public static void Main(string[] args) public static void Main(string[] args)
{ {
try try
@ -64,9 +69,9 @@ namespace TMQExample
{ {
// ANCHOR: create_consumer // ANCHOR: create_consumer
// consumer config // consumer config
var host = "127.0.0.1"; _host = "127.0.0.1";
var groupId = "group1"; _groupId = "group1";
var clientId = "client1"; _clientId = "client1";
var cfg = new Dictionary<string, string>() var cfg = new Dictionary<string, string>()
{ {
{ "td.connect.port", "6030" }, { "td.connect.port", "6030" },
@ -74,9 +79,9 @@ namespace TMQExample
{ "msg.with.table.name", "true" }, { "msg.with.table.name", "true" },
{ "enable.auto.commit", "true" }, { "enable.auto.commit", "true" },
{ "auto.commit.interval.ms", "1000" }, { "auto.commit.interval.ms", "1000" },
{ "group.id", groupId }, { "group.id", _groupId },
{ "client.id", clientId }, { "client.id", _clientId },
{ "td.connect.ip", host }, { "td.connect.ip", _host },
{ "td.connect.user", "root" }, { "td.connect.user", "root" },
{ "td.connect.pass", "taosdata" }, { "td.connect.pass", "taosdata" },
}; };
@ -85,20 +90,33 @@ namespace TMQExample
{ {
// create consumer // create consumer
consumer = new ConsumerBuilder<Dictionary<string, object>>(cfg).Build(); consumer = new ConsumerBuilder<Dictionary<string, object>>(cfg).Build();
Console.WriteLine("Create consumer successfully, host: " + host + ", groupId: " + groupId + Console.WriteLine(
", clientId: " + clientId); $"Create consumer successfully, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}");
} }
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to create native consumer, host : " + host + "; ErrCode:" + e.Code + Console.WriteLine(
"; ErrMessage: " + e.Error); $"Failed to create native consumer, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to create native consumer, host : " + host + "; ErrMessage: " + e.Message); Console.WriteLine(
$"Failed to create native consumer, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
@ -109,11 +127,12 @@ namespace TMQExample
static void Consume(IConsumer<Dictionary<string, object>> consumer) static void Consume(IConsumer<Dictionary<string, object>> consumer)
{ {
// ANCHOR: subscribe // ANCHOR: subscribe
_topic = "topic_meters";
try try
{ {
// subscribe // subscribe
consumer.Subscribe(new List<string>() { "topic_meters" }); consumer.Subscribe(new List<string>() { _topic });
Console.WriteLine("subscribe topics successfully"); Console.WriteLine("Subscribe topics successfully");
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
{ {
// consume message with using block to ensure the result is disposed // consume message with using block to ensure the result is disposed
@ -133,13 +152,24 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to poll data; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to poll data, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to poll data; ErrMessage:" + e.Message); Console.WriteLine(
$"Failed to poll data, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
// ANCHOR_END: subscribe // ANCHOR_END: subscribe
@ -152,24 +182,38 @@ namespace TMQExample
{ {
// get assignment // get assignment
var assignment = consumer.Assignment; var assignment = consumer.Assignment;
Console.WriteLine($"now assignment: {assignment}"); Console.WriteLine($"Now assignment: {assignment}");
// seek to the beginning // seek to the beginning
foreach (var topicPartition in assignment) foreach (var topicPartition in assignment)
{ {
consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0)); consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0));
} }
Console.WriteLine("Assignment seek to beginning successfully"); Console.WriteLine("Assignment seek to beginning successfully");
} }
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to seek offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: 0, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Seek example failed; ErrMessage: " + e.Message); Console.WriteLine(
$"Failed to seek offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: 0, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
// ANCHOR_END: seek // ANCHOR_END: seek
@ -180,6 +224,7 @@ namespace TMQExample
// ANCHOR: commit_offset // ANCHOR: commit_offset
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
TopicPartitionOffset topicPartitionOffset = null;
try try
{ {
// consume message with using block to ensure the result is disposed // consume message with using block to ensure the result is disposed
@ -187,9 +232,10 @@ namespace TMQExample
{ {
if (cr == null) continue; if (cr == null) continue;
// commit offset // commit offset
topicPartitionOffset = cr.TopicPartitionOffset;
consumer.Commit(new List<TopicPartitionOffset> consumer.Commit(new List<TopicPartitionOffset>
{ {
cr.TopicPartitionOffset, topicPartitionOffset,
}); });
Console.WriteLine("Commit offset manually successfully."); Console.WriteLine("Commit offset manually successfully.");
} }
@ -197,13 +243,26 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to execute consumer functions. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to commit offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: {topicPartitionOffset}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to execute consumer functions. ErrMessage:" + e.Message); Console.WriteLine(
$"Failed to commit offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: {topicPartitionOffset}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
} }
@ -221,13 +280,24 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to unsubscribe consumer. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to unsubscribe consumer, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to unsubscribe consumer. Err: " + e.Message); Console.WriteLine(
$"Failed to execute commit example, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
finally finally

View File

@ -16,10 +16,10 @@ namespace Examples
var builder = new ConnectionStringBuilder(connectionString); var builder = new ConnectionStringBuilder(connectionString);
using (var client = DbDriver.Open(builder)) using (var client = DbDriver.Open(builder))
{ {
CreateDatabaseAndTable(client,connectionString); CreateDatabaseAndTable(client, connectionString);
InsertData(client,connectionString); InsertData(client, connectionString);
QueryData(client,connectionString); QueryData(client, connectionString);
QueryWithReqId(client,connectionString); QueryWithReqId(client, connectionString);
} }
} }
catch (TDengineError e) catch (TDengineError e)
@ -52,62 +52,67 @@ namespace Examples
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to create db and table,url:" + connectionString +"; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to create database power or stable meters, ErrCode: " + e.Code +
", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to create db and table, url:" + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to create database power or stable meters, ErrMessage: " + e.Message);
throw; throw;
} }
// ANCHOR_END: create_db_and_table // ANCHOR_END: create_db_and_table
} }
private static void InsertData(ITDengineClient client,string connectionString) private static void InsertData(ITDengineClient client, string connectionString)
{ {
// ANCHOR: insert_data // ANCHOR: insert_data
// insert data, please make sure the database and table are created before
var insertQuery = "INSERT INTO " +
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) ";
try try
{ {
// insert data, please make sure the database and table are created before
var insertQuery = "INSERT INTO " +
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) ";
var affectedRows = client.Exec(insertQuery); var affectedRows = client.Exec(insertQuery);
Console.WriteLine("Successfully inserted " + affectedRows + " rows to power.meters."); Console.WriteLine("Successfully inserted " + affectedRows + " rows to power.meters.");
} }
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to insert data to power.meters, url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to insert data to power.meters, sql: " + insertQuery + ", ErrCode: " +
e.Code + ", ErrMessage: " +
e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to insert data to power.meters, url:" + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to insert data to power.meters, sql: " + insertQuery + ", ErrMessage: " +
e.Message);
throw; throw;
} }
// ANCHOR_END: insert_data // ANCHOR_END: insert_data
} }
private static void QueryData(ITDengineClient client,string connectionString) private static void QueryData(ITDengineClient client, string connectionString)
{ {
// ANCHOR: select_data // ANCHOR: select_data
// query data, make sure the database and table are created before
var query = "SELECT ts, current, location FROM power.meters limit 100";
try try
{ {
// query data, make sure the database and table are created before
var query = "SELECT ts, current, location FROM power.meters limit 100";
using (var rows = client.Query(query)) using (var rows = client.Query(query))
{ {
while (rows.Read()) while (rows.Read())
{ {
// Add your data processing logic here
var ts = (DateTime)rows.GetValue(0); var ts = (DateTime)rows.GetValue(0);
var current = (float)rows.GetValue(1); var current = (float)rows.GetValue(1);
var location = Encoding.UTF8.GetString((byte[])rows.GetValue(2)); var location = Encoding.UTF8.GetString((byte[])rows.GetValue(2));
@ -119,28 +124,30 @@ namespace Examples
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to query data from power.meters, url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to query data from power.meters, sql: " + query + ", ErrCode: " + e.Code +
", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to query data from power.meters, url:" + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine(
"Failed to query data from power.meters, sql: " + query + ", ErrMessage: " + e.Message);
throw; throw;
} }
// ANCHOR_END: select_data // ANCHOR_END: select_data
} }
private static void QueryWithReqId(ITDengineClient client,string connectionString) private static void QueryWithReqId(ITDengineClient client, string connectionString)
{ {
// ANCHOR: query_id // ANCHOR: query_id
var reqId = (long)3; var reqId = (long)3;
// query data
var query = "SELECT ts, current, location FROM power.meters limit 1";
try try
{ {
// query data
var query = "SELECT ts, current, location FROM power.meters limit 1";
// query with request id 3 // query with request id 3
using (var rows = client.Query(query,reqId)) using (var rows = client.Query(query, reqId))
{ {
while (rows.Read()) while (rows.Read())
{ {
@ -155,13 +162,15 @@ namespace Examples
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", url:" + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", sql: " + query + ", ErrCode: " +
e.Code + ", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", url:" + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to execute sql with reqId: " + reqId + ", sql: " + query + ", ErrMessage: " +
e.Message);
throw; throw;
} }
// ANCHOR_END: query_id // ANCHOR_END: query_id

View File

@ -63,16 +63,16 @@ namespace Examples
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine("Failed to insert to table meters using stmt, ErrCode: " + e.Code + ", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to insert to table meters using stmt, url: " + connectionString + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to insert to table meters using stmt, ErrMessage: " + e.Message);
throw; throw;
} }
} }
// ANCHOR_END: main // ANCHOR_END: main
} }
} }

View File

@ -44,17 +44,17 @@ namespace TDengineExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + e.Code + Console.WriteLine("Failed to insert data with schemaless, ErrCode: " + e.Code +
"; ErrMessage: " + e.Error); ", ErrMessage: " + e.Error);
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + e.Message); Console.WriteLine("Failed to insert data with schemaless, ErrMessage: " + e.Message);
throw; throw;
} }
} }
// ANCHOR_END: main // ANCHOR_END: main
} }
} }

View File

@ -6,6 +6,11 @@ namespace TMQExample
{ {
internal class SubscribeDemo internal class SubscribeDemo
{ {
private static string _host = "";
private static string _groupId = "";
private static string _clientId = "";
private static string _topic = "";
public static void Main(string[] args) public static void Main(string[] args)
{ {
try try
@ -68,9 +73,9 @@ namespace TMQExample
{ {
// ANCHOR: create_consumer // ANCHOR: create_consumer
// consumer config // consumer config
var host = "127.0.0.1"; _host = "127.0.0.1";
var groupId = "group1"; _groupId = "group1";
var clientId = "client1"; _clientId = "client1";
var cfg = new Dictionary<string, string>() var cfg = new Dictionary<string, string>()
{ {
{ "td.connect.type", "WebSocket" }, { "td.connect.type", "WebSocket" },
@ -79,9 +84,9 @@ namespace TMQExample
{ "msg.with.table.name", "true" }, { "msg.with.table.name", "true" },
{ "enable.auto.commit", "true" }, { "enable.auto.commit", "true" },
{ "auto.commit.interval.ms", "1000" }, { "auto.commit.interval.ms", "1000" },
{ "group.id", groupId }, { "group.id", _groupId },
{ "client.id", clientId }, { "client.id", _clientId },
{ "td.connect.ip", host }, { "td.connect.ip", _host },
{ "td.connect.user", "root" }, { "td.connect.user", "root" },
{ "td.connect.pass", "taosdata" }, { "td.connect.pass", "taosdata" },
}; };
@ -90,20 +95,33 @@ namespace TMQExample
{ {
// create consumer // create consumer
consumer = new ConsumerBuilder<Dictionary<string, object>>(cfg).Build(); consumer = new ConsumerBuilder<Dictionary<string, object>>(cfg).Build();
Console.WriteLine("Create consumer successfully, host: " + host + ", groupId: " + groupId + Console.WriteLine(
", clientId: " + clientId); $"Create consumer successfully, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}");
} }
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to create websocket consumer, host : " + host + "; ErrCode:" + e.Code + Console.WriteLine(
"; ErrMessage: " + e.Error); $"Failed to create native consumer, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to create websocket consumer, host : " + host + "; ErrMessage: " + e.Message); Console.WriteLine(
$"Failed to create native consumer, " +
$"host: {_host}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
@ -114,10 +132,11 @@ namespace TMQExample
static void Consume(IConsumer<Dictionary<string, object>> consumer) static void Consume(IConsumer<Dictionary<string, object>> consumer)
{ {
// ANCHOR: subscribe // ANCHOR: subscribe
_topic = "topic_meters";
try try
{ {
// subscribe // subscribe
consumer.Subscribe(new List<string>() { "topic_meters" }); consumer.Subscribe(new List<string>() { _topic });
Console.WriteLine("Subscribe topics successfully"); Console.WriteLine("Subscribe topics successfully");
for (int i = 0; i < 50; i++) for (int i = 0; i < 50; i++)
{ {
@ -138,13 +157,23 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to poll data; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to poll data, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to poll data; ErrMessage:" + e.Message); Console.WriteLine($"Failed to poll data, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
// ANCHOR_END: subscribe // ANCHOR_END: subscribe
@ -163,18 +192,32 @@ namespace TMQExample
{ {
consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0)); consumer.Seek(new TopicPartitionOffset(topicPartition.Topic, topicPartition.Partition, 0));
} }
Console.WriteLine("Assignment seek to beginning successfully"); Console.WriteLine("Assignment seek to beginning successfully");
} }
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Seek example failed; ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to seek offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: 0, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Seek example failed; ErrMessage: " + e.Message); Console.WriteLine(
$"Failed to seek offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: 0, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
// ANCHOR_END: seek // ANCHOR_END: seek
@ -185,6 +228,7 @@ namespace TMQExample
// ANCHOR: commit_offset // ANCHOR: commit_offset
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
{ {
TopicPartitionOffset topicPartitionOffset = null;
try try
{ {
// consume message with using block to ensure the result is disposed // consume message with using block to ensure the result is disposed
@ -192,9 +236,10 @@ namespace TMQExample
{ {
if (cr == null) continue; if (cr == null) continue;
// commit offset // commit offset
topicPartitionOffset = cr.TopicPartitionOffset;
consumer.Commit(new List<TopicPartitionOffset> consumer.Commit(new List<TopicPartitionOffset>
{ {
cr.TopicPartitionOffset, topicPartitionOffset,
}); });
Console.WriteLine("Commit offset manually successfully."); Console.WriteLine("Commit offset manually successfully.");
} }
@ -202,13 +247,26 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to execute consumer functions. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to commit offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: {topicPartitionOffset}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to execute consumer functions. ErrMessage:" + e.Message); Console.WriteLine(
$"Failed to commit offset, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"offset: {topicPartitionOffset}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
} }
@ -226,13 +284,24 @@ namespace TMQExample
catch (TDengineError e) catch (TDengineError e)
{ {
// handle TDengine error // handle TDengine error
Console.WriteLine("Failed to unsubscribe consumer. ErrCode:" + e.Code + "; ErrMessage: " + e.Error); Console.WriteLine(
$"Failed to unsubscribe consumer, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrCode: {e.Code}, " +
$"ErrMessage: {e.Error}");
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
// handle other exceptions // handle other exceptions
Console.WriteLine("Failed to unsubscribe consumer. Err: " + e.Message); Console.WriteLine(
$"Failed to execute commit example, " +
$"topic: {_topic}, " +
$"groupId: {_groupId}, " +
$"clientId: {_clientId}, " +
$"ErrMessage: {e.Message}");
throw; throw;
} }
finally finally

View File

@ -1,58 +1,60 @@
package main package main
import ( import (
"context" "context"
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time" "time"
_ "github.com/taosdata/driver-go/v3/taosSql" _ "github.com/taosdata/driver-go/v3/taosSql"
) )
func main() { func main() {
taosDSN := "root:taosdata@tcp(localhost:6030)/" taosDSN := "root:taosdata@tcp(localhost:6030)/"
db, err := sql.Open("taosSql", taosDSN) db, err := sql.Open("taosSql", taosDSN)
if err != nil { if err != nil {
log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to connect to " + taosDSN + ", ErrMessage: " + err.Error())
} }
defer db.Close() defer db.Close()
initEnv(db) initEnv(db)
// ANCHOR: query_id // ANCHOR: query_id
// use context to set request id // use context to set request id
reqId := int64(3) reqId := int64(3)
ctx := context.WithValue(context.Background(), "taos_req_id", reqId) ctx := context.WithValue(context.Background(), "taos_req_id", reqId)
// execute query with context // execute query with context
rows, err := db.QueryContext(ctx, "SELECT ts, current, location FROM power.meters limit 1") querySql := "SELECT ts, current, location FROM power.meters limit 1"
if err != nil { rows, err := db.QueryContext(ctx, querySql)
log.Fatalf("Failed to execute sql with reqId: %d, url: %s; ErrMessage: %s\n", reqId, taosDSN, err.Error()) if err != nil {
} log.Fatalf("Failed to execute sql with reqId: %d, url: %s, sql: %s, ErrMessage: %s\n", reqId, taosDSN, querySql, err.Error())
for rows.Next() { }
var ( for rows.Next() {
ts time.Time // Add your data processing logic here
current float32 var (
location string ts time.Time
) current float32
err = rows.Scan(&ts, &current, &location) location string
if err != nil { )
log.Fatal("Scan error: ", err) err = rows.Scan(&ts, &current, &location)
} if err != nil {
fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location) log.Fatalf("Failed to scan data, reqId: %d, url:%s, sql: %s, ErrMessage: %s\n", reqId, taosDSN, querySql, err)
} }
// ANCHOR_END: query_id fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location)
} }
// ANCHOR_END: query_id
func initEnv(conn *sql.DB) { }
_, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { func initEnv(conn *sql.DB) {
log.Fatal("Create database error: ", err) _, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power")
} if err != nil {
_, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") log.Fatal("Create database power error: ", err)
if err != nil { }
log.Fatal("Create table error: ", err) _, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
} if err != nil {
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)") log.Fatal("Create stable meters error: ", err)
if err != nil { }
log.Fatal("Insert data error: ", err) _, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
} if err != nil {
} log.Fatal("Insert data to power.meters error: ", err)
}
}

View File

@ -20,26 +20,26 @@ func main() {
defer conn.Close() defer conn.Close()
_, err = conn.Exec("CREATE DATABASE IF NOT EXISTS power") _, err = conn.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { if err != nil {
log.Fatalln("Failed to create db host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create database power, ErrMessage: " + err.Error())
} }
_, err = conn.Exec("USE power") _, err = conn.Exec("USE power")
if err != nil { if err != nil {
log.Fatalln("Failed to use db host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to use database power, ErrMessage: " + err.Error())
} }
// insert influxdb line protocol // insert influxdb line protocol
err = conn.InfluxDBInsertLines([]string{lineDemo}, "ms") err = conn.InfluxDBInsertLines([]string{lineDemo}, "ms")
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data:" + lineDemo + ", ErrMessage: " + err.Error())
} }
// insert opentsdb telnet protocol // insert opentsdb telnet protocol
err = conn.OpenTSDBInsertTelnetLines([]string{telnetDemo}) err = conn.OpenTSDBInsertTelnetLines([]string{telnetDemo})
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data:" + telnetDemo + ", ErrMessage: " + err.Error())
} }
// insert opentsdb json protocol // insert opentsdb json protocol
err = conn.OpenTSDBInsertJsonPayload(jsonDemo) err = conn.OpenTSDBInsertJsonPayload(jsonDemo)
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data:" + jsonDemo + ", ErrMessage: " + err.Error())
} }
fmt.Println("Inserted data with schemaless successfully.") fmt.Println("Inserted data with schemaless successfully.")
} }

View File

@ -25,7 +25,7 @@ func main() {
defer db.Close() defer db.Close()
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { if err != nil {
log.Fatalln("Failed to create db host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create database power, ErrMessage: " + err.Error())
} }
s, err := schemaless.NewSchemaless(schemaless.NewConfig("ws://localhost:6041", 1, s, err := schemaless.NewSchemaless(schemaless.NewConfig("ws://localhost:6041", 1,
schemaless.SetDb("power"), schemaless.SetDb("power"),
@ -40,17 +40,17 @@ func main() {
// insert influxdb line protocol // insert influxdb line protocol
err = s.Insert(lineDemo, schemaless.InfluxDBLineProtocol, "ms", 0, common.GetReqID()) err = s.Insert(lineDemo, schemaless.InfluxDBLineProtocol, "ms", 0, common.GetReqID())
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data:" + lineDemo + ", ErrMessage: " + err.Error())
} }
// insert opentsdb telnet line protocol // insert opentsdb telnet line protocol
err = s.Insert(telnetDemo, schemaless.OpenTSDBTelnetLineProtocol, "ms", 0, common.GetReqID()) err = s.Insert(telnetDemo, schemaless.OpenTSDBTelnetLineProtocol, "ms", 0, common.GetReqID())
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data: " + telnetDemo + ", ErrMessage: " + err.Error())
} }
// insert opentsdb json format protocol // insert opentsdb json format protocol
err = s.Insert(jsonDemo, schemaless.OpenTSDBJsonFormatProtocol, "s", 0, common.GetReqID()) err = s.Insert(jsonDemo, schemaless.OpenTSDBJsonFormatProtocol, "s", 0, common.GetReqID())
if err != nil { if err != nil {
log.Fatalln("Failed to insert data with schemaless, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to insert data with schemaless, data: " + jsonDemo + ", ErrMessage: " + err.Error())
} }
fmt.Println("Inserted data with schemaless successfully.") fmt.Println("Inserted data with schemaless successfully.")
} }

View File

@ -1,85 +1,86 @@
package main package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time" "time"
_ "github.com/taosdata/driver-go/v3/taosSql" _ "github.com/taosdata/driver-go/v3/taosSql"
) )
func main() { func main() {
var taosDSN = "root:taosdata@tcp(localhost:6030)/" var taosDSN = "root:taosdata@tcp(localhost:6030)/"
db, err := sql.Open("taosSql", taosDSN) db, err := sql.Open("taosSql", taosDSN)
if err != nil { if err != nil {
log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to connect to " + taosDSN + ", ErrMessage: " + err.Error())
} }
defer db.Close() defer db.Close()
// ANCHOR: create_db_and_table // ANCHOR: create_db_and_table
// create database // create database
res, err := db.Exec("CREATE DATABASE IF NOT EXISTS power") res, err := db.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { if err != nil {
log.Fatalln("Failed to create db, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create database power, ErrMessage: " + err.Error())
} }
rowsAffected, err := res.RowsAffected() rowsAffected, err := res.RowsAffected()
if err != nil { if err != nil {
log.Fatalln("Failed to get create db rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to get create database rowsAffected, ErrMessage: " + err.Error())
} }
// you can check rowsAffected here // you can check rowsAffected here
fmt.Println("Create database power successfully, rowsAffected: ", rowsAffected) fmt.Println("Create database power successfully, rowsAffected: ", rowsAffected)
// create table // create table
res, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") res, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
if err != nil { if err != nil {
log.Fatalln("Failed to create db and table, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create stable meters, ErrMessage: " + err.Error())
} }
rowsAffected, err = res.RowsAffected() rowsAffected, err = res.RowsAffected()
if err != nil { if err != nil {
log.Fatalln("Failed to get create db rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to get create stable rowsAffected, ErrMessage: " + err.Error())
} }
// you can check rowsAffected here // you can check rowsAffected here
fmt.Println("Create stable power.meters successfully, rowsAffected:", rowsAffected) fmt.Println("Create stable power.meters successfully, rowsAffected:", rowsAffected)
// ANCHOR_END: create_db_and_table // ANCHOR_END: create_db_and_table
// ANCHOR: insert_data // ANCHOR: insert_data
// insert data, please make sure the database and table are created before // insert data, please make sure the database and table are created before
insertQuery := "INSERT INTO " + insertQuery := "INSERT INTO " +
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " + "power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
"VALUES " + "VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " + "(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " + "(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " + "(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " + "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " +
"VALUES " + "VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) " "(NOW + 1a, 10.30000, 218, 0.25000) "
res, err = db.Exec(insertQuery) res, err = db.Exec(insertQuery)
if err != nil { if err != nil {
log.Fatal("Failed to insert data to power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalf("Failed to insert data to power.meters, sql: %s, ErrMessage: %s\n", insertQuery, err.Error())
} }
rowsAffected, err = res.RowsAffected() rowsAffected, err = res.RowsAffected()
if err != nil { if err != nil {
log.Fatal("Failed to get insert rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalf("Failed to get insert rowsAffected, sql: %s, ErrMessage: %s\n", insertQuery, err.Error())
} }
// you can check affectedRows here // you can check affectedRows here
fmt.Printf("Successfully inserted %d rows to power.meters.\n", rowsAffected) fmt.Printf("Successfully inserted %d rows to power.meters.\n", rowsAffected)
// ANCHOR_END: insert_data // ANCHOR_END: insert_data
// ANCHOR: select_data // ANCHOR: select_data
// query data, make sure the database and table are created before // query data, make sure the database and table are created before
rows, err := db.Query("SELECT ts, current, location FROM power.meters limit 100") sql := "SELECT ts, current, location FROM power.meters limit 100"
if err != nil { rows, err := db.Query(sql)
log.Fatal("Failed to query data from power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error()) if err != nil {
} log.Fatalf("Failed to query data from power.meters, sql: %s, ErrMessage: %s\n", sql, err.Error())
for rows.Next() { }
var ( for rows.Next() {
ts time.Time // Add your data processing logic here
current float32 var (
location string ts time.Time
) current float32
err = rows.Scan(&ts, &current, &location) location string
if err != nil { )
log.Fatal("Failed to scan data, url:" + taosDSN + "; ErrMessage: " + err.Error()) err = rows.Scan(&ts, &current, &location)
} if err != nil {
// you can check data here log.Fatalf("Failed to scan data, sql: %s, ErrMessage: %s\n", sql, err)
fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location) }
} fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location)
// ANCHOR_END: select_data }
} // ANCHOR_END: select_data
}

View File

@ -23,22 +23,22 @@ func main() {
// prepare database and table // prepare database and table
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { if err != nil {
log.Fatalln("Failed to create db, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create database power, ErrMessage: " + err.Error())
} }
_, err = db.Exec("USE power") _, err = db.Exec("USE power")
if err != nil { if err != nil {
log.Fatalln("Failed to use db, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to use database power, ErrMessage: " + err.Error())
} }
_, err = db.Exec("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") _, err = db.Exec("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
if err != nil { if err != nil {
log.Fatalln("Failed to create table, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create stable meters, ErrMessage: " + err.Error())
} }
// prepare statement // prepare statement
sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)" sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"
stmt := db.Stmt() stmt := db.Stmt()
err = stmt.Prepare(sql) err = stmt.Prepare(sql)
if err != nil { if err != nil {
log.Fatalln("Failed to prepare sql, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to prepare sql, sql: " + sql + ", ErrMessage: " + err.Error())
} }
for i := 1; i <= numOfSubTable; i++ { for i := 1; i <= numOfSubTable; i++ {
tableName := fmt.Sprintf("d_bind_%d", i) tableName := fmt.Sprintf("d_bind_%d", i)
@ -46,7 +46,7 @@ func main() {
// set tableName and tags // set tableName and tags
err = stmt.SetTableNameWithTags(tableName, tags) err = stmt.SetTableNameWithTags(tableName, tags)
if err != nil { if err != nil {
log.Fatalln("Failed to set table name and tags, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to set table name and tags, tableName: " + tableName + "; ErrMessage: " + err.Error())
} }
// bind column data // bind column data
current := time.Now() current := time.Now()
@ -58,18 +58,18 @@ func main() {
AddFloat(rand.Float32()) AddFloat(rand.Float32())
err = stmt.BindRow(row) err = stmt.BindRow(row)
if err != nil { if err != nil {
log.Fatalln("Failed to bind params, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to bind params, ErrMessage: " + err.Error())
} }
} }
// add batch // add batch
err = stmt.AddBatch() err = stmt.AddBatch()
if err != nil { if err != nil {
log.Fatalln("Failed to add batch, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to add batch, ErrMessage: " + err.Error())
} }
// execute batch // execute batch
err = stmt.Execute() err = stmt.Execute()
if err != nil { if err != nil {
log.Fatalln("Failed to exec, host: " + host + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to exec, ErrMessage: " + err.Error())
} }
// get affected rows // get affected rows
affected := stmt.GetAffectedRows() affected := stmt.GetAffectedRows()

View File

@ -27,11 +27,11 @@ func main() {
// prepare database and table // prepare database and table
_, err = db.Exec("CREATE DATABASE IF NOT EXISTS power") _, err = db.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil { if err != nil {
log.Fatalln("Failed to create db, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create database power, ErrMessage: " + err.Error())
} }
_, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") _, err = db.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
if err != nil { if err != nil {
log.Fatalln("Failed to create table, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to create stable power.meters, ErrMessage: " + err.Error())
} }
config := stmt.NewConfig(fmt.Sprintf("ws://%s:6041", host), 0) config := stmt.NewConfig(fmt.Sprintf("ws://%s:6041", host), 0)
@ -49,11 +49,11 @@ func main() {
sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)" sql := "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"
stmt, err := connector.Init() stmt, err := connector.Init()
if err != nil { if err != nil {
log.Fatalln("Failed to init stmt, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatalln("Failed to init stmt, sql: " + sql + ", ErrMessage: " + err.Error())
} }
err = stmt.Prepare(sql) err = stmt.Prepare(sql)
if err != nil { if err != nil {
log.Fatal("Failed to prepare sql, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to prepare sql, sql: " + sql + ", ErrMessage: " + err.Error())
} }
for i := 1; i <= numOfSubTable; i++ { for i := 1; i <= numOfSubTable; i++ {
tableName := fmt.Sprintf("d_bind_%d", i) tableName := fmt.Sprintf("d_bind_%d", i)
@ -63,12 +63,12 @@ func main() {
// set tableName // set tableName
err = stmt.SetTableName(tableName) err = stmt.SetTableName(tableName)
if err != nil { if err != nil {
log.Fatal("Failed to set table name, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to set table name, tableName: " + tableName + "; ErrMessage: " + err.Error())
} }
// set tags // set tags
err = stmt.SetTags(tags, tagsType) err = stmt.SetTags(tags, tagsType)
if err != nil { if err != nil {
log.Fatal("Failed to set tags, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to set tags, ErrMessage: " + err.Error())
} }
// bind column data // bind column data
current := time.Now() current := time.Now()
@ -80,18 +80,18 @@ func main() {
columnData[3] = param.NewParam(1).AddFloat(rand.Float32()) columnData[3] = param.NewParam(1).AddFloat(rand.Float32())
err = stmt.BindParam(columnData, columnType) err = stmt.BindParam(columnData, columnType)
if err != nil { if err != nil {
log.Fatal("Failed to bind params, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to bind params, ErrMessage: " + err.Error())
} }
} }
// add batch // add batch
err = stmt.AddBatch() err = stmt.AddBatch()
if err != nil { if err != nil {
log.Fatal("Failed to add batch, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to add batch, ErrMessage: " + err.Error())
} }
// execute batch // execute batch
err = stmt.Exec() err = stmt.Exec()
if err != nil { if err != nil {
log.Fatal("Failed to exec, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to exec, ErrMessage: " + err.Error())
} }
// get affected rows // get affected rows
affected := stmt.GetAffectedRows() affected := stmt.GetAffectedRows()
@ -100,6 +100,6 @@ func main() {
} }
err = stmt.Close() err = stmt.Close()
if err != nil { if err != nil {
log.Fatal("Failed to close stmt, url: " + taosDSN + "; ErrMessage: " + err.Error()) log.Fatal("Failed to close stmt, ErrMessage: " + err.Error())
} }
} }

View File

@ -1,134 +1,179 @@
package main package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time" "time"
"github.com/taosdata/driver-go/v3/af/tmq" "github.com/taosdata/driver-go/v3/af/tmq"
tmqcommon "github.com/taosdata/driver-go/v3/common/tmq" tmqcommon "github.com/taosdata/driver-go/v3/common/tmq"
_ "github.com/taosdata/driver-go/v3/taosSql" _ "github.com/taosdata/driver-go/v3/taosSql"
) )
var done = make(chan struct{}) var done = make(chan struct{})
var groupID string
func main() { var clientID string
// init env var host string
taosDSN := "root:taosdata@tcp(127.0.0.1:6030)/" var topic string
conn, err := sql.Open("taosSql", taosDSN)
if err != nil { func main() {
log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) // init env
} taosDSN := "root:taosdata@tcp(127.0.0.1:6030)/"
defer func() { conn, err := sql.Open("taosSql", taosDSN)
conn.Close() if err != nil {
}() log.Fatalln("Failed to connect to " + taosDSN + ", ErrMessage: " + err.Error())
initEnv(conn) }
// ANCHOR: create_consumer defer func() {
// create consumer conn.Close()
groupID := "group1" }()
clientID := "client1" initEnv(conn)
host := "127.0.0.1" // ANCHOR: create_consumer
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{ // create consumer
"td.connect.user": "root", groupID = "group1"
"td.connect.pass": "taosdata", clientID = "client1"
"auto.offset.reset": "latest", host = "127.0.0.1"
"msg.with.table.name": "true", consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
"enable.auto.commit": "true", "td.connect.user": "root",
"auto.commit.interval.ms": "1000", "td.connect.pass": "taosdata",
"group.id": groupID, "auto.offset.reset": "latest",
"client.id": clientID, "msg.with.table.name": "true",
}) "enable.auto.commit": "true",
if err != nil { "auto.commit.interval.ms": "1000",
log.Fatalln("Failed to create native consumer, host : " + host + "; ErrMessage: " + err.Error()) "group.id": groupID,
} "client.id": clientID,
log.Println("Create consumer successfully, host: " + host + ", groupId: " + groupID + ", clientId: " + clientID) })
if err != nil {
// ANCHOR_END: create_consumer log.Fatalf(
// ANCHOR: subscribe "Failed to create native consumer, host: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
err = consumer.Subscribe("topic_meters", nil) host,
if err != nil { groupID,
log.Fatalln("Failed to subscribe, host : " + host + "; ErrMessage: " + err.Error()) clientID,
} err.Error(),
log.Println("Subscribe topics successfully") )
for i := 0; i < 50; i++ { }
ev := consumer.Poll(100) log.Printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s\n", host, groupID, clientID)
if ev != nil {
switch e := ev.(type) { // ANCHOR_END: create_consumer
case *tmqcommon.DataMessage: // ANCHOR: subscribe
// process your data here topic = "topic_meters"
fmt.Printf("data:%v\n", e) err = consumer.Subscribe(topic, nil)
// ANCHOR: commit_offset if err != nil {
// commit offset log.Fatalf(
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) "Failed to subscribe topic_meters, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
if err != nil { topic,
log.Fatalln("Failed to commit offset, host : " + host + "; ErrMessage: " + err.Error()) groupID,
} clientID,
log.Println("Commit offset manually successfully.") err.Error(),
// ANCHOR_END: commit_offset )
case tmqcommon.Error: }
fmt.Printf("%% Error: %v: %v\n", e.Code(), e) log.Println("Subscribe topics successfully")
log.Fatalln("Failed to poll data, host : " + host + "; ErrMessage: " + err.Error()) for i := 0; i < 50; i++ {
} ev := consumer.Poll(100)
} if ev != nil {
} switch e := ev.(type) {
// ANCHOR_END: subscribe case *tmqcommon.DataMessage:
// ANCHOR: seek // process your data here
// get assignment fmt.Printf("data:%v\n", e)
partitions, err := consumer.Assignment() // ANCHOR: commit_offset
if err != nil { // commit offset
log.Fatal("Failed to get assignment; ErrMessage: " + err.Error()) _, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
} if err != nil {
fmt.Println("Now assignment:", partitions) log.Fatalf(
for i := 0; i < len(partitions); i++ { "Failed to commit offset, topic: %s, groupId: %s, clientId: %s, offset %s, ErrMessage: %s\n",
// seek to the beginning topic,
err = consumer.Seek(tmqcommon.TopicPartition{ groupID,
Topic: partitions[i].Topic, clientID,
Partition: partitions[i].Partition, e.TopicPartition,
Offset: 0, err.Error(),
}, 0) )
if err != nil { }
log.Fatalln("Seek example failed; ErrMessage: " + err.Error()) log.Println("Commit offset manually successfully.")
} // ANCHOR_END: commit_offset
} case tmqcommon.Error:
fmt.Println("Assignment seek to beginning successfully") log.Fatalf("Failed to poll data, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n", topic, groupID, clientID, e.Error())
// ANCHOR_END: seek }
// ANCHOR: close }
// unsubscribe }
err = consumer.Unsubscribe() // ANCHOR_END: subscribe
if err != nil { // ANCHOR: seek
log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error()) // get assignment
} partitions, err := consumer.Assignment()
// close consumer if err != nil {
err = consumer.Close() log.Fatalf("Failed to get assignment, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n", topic, groupID, clientID, err.Error())
if err != nil { }
log.Fatal("Failed to close consumer. ErrMessage: " + err.Error()) fmt.Println("Now assignment:", partitions)
} for i := 0; i < len(partitions); i++ {
// ANCHOR_END: close // seek to the beginning
<-done err = consumer.Seek(tmqcommon.TopicPartition{
} Topic: partitions[i].Topic,
Partition: partitions[i].Partition,
func initEnv(conn *sql.DB) { Offset: 0,
_, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power") }, 0)
if err != nil { if err != nil {
log.Fatal("Failed to create database. ErrMessage: " + err.Error()) log.Fatalf(
} "Failed to execute seek offset, topic: %s, groupId: %s, clientId: %s, partition: %d, offset: %d, ErrMessage: %s\n",
_, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") topic,
if err != nil { groupID,
log.Fatal("Failed to create stable. ErrMessage: " + err.Error()) clientID,
} partitions[i].Partition,
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters") 0,
if err != nil { err.Error(),
log.Fatal("Failed to create topic. ErrMessage: " + err.Error()) )
} }
go func() { }
for i := 0; i < 10; i++ { fmt.Println("Assignment seek to beginning successfully")
time.Sleep(time.Second) // ANCHOR_END: seek
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)") // ANCHOR: close
if err != nil { // unsubscribe
log.Fatal("Failed to insert data. ErrMessage: " + err.Error()) err = consumer.Unsubscribe()
} if err != nil {
} log.Fatalf(
done <- struct{}{} "Failed to unsubscribe consumer, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
}() topic,
} groupID,
clientID,
err.Error(),
)
}
fmt.Println("Consumer unsubscribed successfully.")
// close consumer
err = consumer.Close()
if err != nil {
log.Fatalf(
"Failed to close consumer, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
topic,
groupID,
clientID,
err.Error(),
)
}
fmt.Println("Consumer closed successfully.")
// ANCHOR_END: close
<-done
}
func initEnv(conn *sql.DB) {
_, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil {
log.Fatal("Failed to create database, ErrMessage: " + err.Error())
}
_, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
if err != nil {
log.Fatal("Failed to create stable, ErrMessage: " + err.Error())
}
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters")
if err != nil {
log.Fatal("Failed to create topic, ErrMessage: " + err.Error())
}
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
if err != nil {
log.Fatal("Failed to insert data, ErrMessage: " + err.Error())
}
}
done <- struct{}{}
}()
}

View File

@ -1,139 +1,197 @@
package main package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"log" "log"
"time" "time"
"github.com/taosdata/driver-go/v3/common" "github.com/taosdata/driver-go/v3/common"
tmqcommon "github.com/taosdata/driver-go/v3/common/tmq" tmqcommon "github.com/taosdata/driver-go/v3/common/tmq"
_ "github.com/taosdata/driver-go/v3/taosWS" _ "github.com/taosdata/driver-go/v3/taosWS"
"github.com/taosdata/driver-go/v3/ws/tmq" "github.com/taosdata/driver-go/v3/ws/tmq"
) )
var done = make(chan struct{}) var done = make(chan struct{})
var groupID string
func main() { var clientID string
// init env var host string
taosDSN := "root:taosdata@ws(127.0.0.1:6041)/" var topic string
conn, err := sql.Open("taosWS", taosDSN)
if err != nil { func main() {
log.Fatalln("Failed to connect to " + taosDSN + "; ErrMessage: " + err.Error()) // init env
} taosDSN := "root:taosdata@ws(127.0.0.1:6041)/"
defer func() { conn, err := sql.Open("taosWS", taosDSN)
conn.Close() if err != nil {
}() log.Fatalln("Failed to connect to " + taosDSN + ", ErrMessage: " + err.Error())
initEnv(conn) }
// ANCHOR: create_consumer defer func() {
// create consumer conn.Close()
wsUrl := "ws://127.0.0.1:6041" }()
groupID := "group1" initEnv(conn)
clientID := "client1" // ANCHOR: create_consumer
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{ // create consumer
"ws.url": wsUrl, wsUrl := "ws://127.0.0.1:6041"
"ws.message.channelLen": uint(0), groupID = "group1"
"ws.message.timeout": common.DefaultMessageTimeout, clientID = "client1"
"ws.message.writeWait": common.DefaultWriteWait, host = "127.0.0.1"
"td.connect.user": "root", consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
"td.connect.pass": "taosdata", "ws.url": wsUrl,
"auto.offset.reset": "latest", "ws.message.channelLen": uint(0),
"msg.with.table.name": "true", "ws.message.timeout": common.DefaultMessageTimeout,
"enable.auto.commit": "true", "ws.message.writeWait": common.DefaultWriteWait,
"auto.commit.interval.ms": "1000", "td.connect.user": "root",
"group.id": groupID, "td.connect.pass": "taosdata",
"client.id": clientID, "auto.offset.reset": "latest",
}) "msg.with.table.name": "true",
if err != nil { "enable.auto.commit": "true",
log.Fatalln("Failed to create websocket consumer, host : " + wsUrl + "; ErrMessage: " + err.Error()) "auto.commit.interval.ms": "1000",
} "group.id": groupID,
log.Println("Create consumer successfully, host: " + wsUrl + ", groupId: " + groupID + ", clientId: " + clientID) "client.id": clientID,
})
// ANCHOR_END: create_consumer if err != nil {
// ANCHOR: subscribe log.Fatalf(
err = consumer.Subscribe("topic_meters", nil) "Failed to create websocket consumer, host: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
if err != nil { host,
log.Fatalln("Failed to subscribe, host : " + wsUrl + "; ErrMessage: " + err.Error()) groupID,
} clientID,
log.Println("Subscribe topics successfully") err.Error(),
for i := 0; i < 50; i++ { )
ev := consumer.Poll(100) }
if ev != nil { log.Printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s\n", host, groupID, clientID)
switch e := ev.(type) {
case *tmqcommon.DataMessage: // ANCHOR_END: create_consumer
// process your data here // ANCHOR: subscribe
fmt.Printf("data:%v\n", e) topic = "topic_meters"
// ANCHOR: commit_offset err = consumer.Subscribe(topic, nil)
// commit offset if err != nil {
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition}) log.Fatalf(
if err != nil { "Failed to subscribe topic_meters, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
log.Fatalln("Failed to commit offset, host : " + wsUrl + "; ErrMessage: " + err.Error()) topic,
} groupID,
log.Println("Commit offset manually successfully.") clientID,
// ANCHOR_END: commit_offset err.Error(),
case tmqcommon.Error: )
fmt.Printf("%% Error: %v: %v\n", e.Code(), e) }
log.Fatalln("Failed to poll data, host : " + wsUrl + "; ErrMessage: " + err.Error()) log.Println("Subscribe topics successfully")
} for i := 0; i < 50; i++ {
} ev := consumer.Poll(100)
} if ev != nil {
// ANCHOR_END: subscribe switch e := ev.(type) {
// ANCHOR: seek case *tmqcommon.DataMessage:
// get assignment // process your data here
partitions, err := consumer.Assignment() fmt.Printf("data:%v\n", e)
if err != nil { // ANCHOR: commit_offset
log.Fatal("Failed to get assignment; ErrMessage: " + err.Error()) // commit offset
} _, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
fmt.Println("Now assignment:", partitions) if err != nil {
for i := 0; i < len(partitions); i++ { log.Fatalf(
// seek to the beginning "Failed to commit offset, topic: %s, groupId: %s, clientId: %s, offset %s, ErrMessage: %s\n",
err = consumer.Seek(tmqcommon.TopicPartition{ topic,
Topic: partitions[i].Topic, groupID,
Partition: partitions[i].Partition, clientID,
Offset: 0, e.TopicPartition,
}, 0) err.Error(),
if err != nil { )
log.Fatalln("Seek example failed; ErrMessage: " + err.Error()) }
} log.Println("Commit offset manually successfully.")
} // ANCHOR_END: commit_offset
fmt.Println("Assignment seek to beginning successfully") case tmqcommon.Error:
// ANCHOR_END: seek log.Fatalf(
// ANCHOR: close "Failed to poll data, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
// unsubscribe topic,
err = consumer.Unsubscribe() groupID,
if err != nil { clientID,
log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error()) e.Error(),
} )
// close consumer }
err = consumer.Close() }
if err != nil { }
log.Fatal("Failed to close consumer. ErrMessage: " + err.Error()) // ANCHOR_END: subscribe
} // ANCHOR: seek
// ANCHOR_END: close // get assignment
<-done partitions, err := consumer.Assignment()
} if err != nil {
log.Fatalf(
func initEnv(conn *sql.DB) { "Failed to get assignment, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
_, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power") topic,
if err != nil { groupID,
log.Fatal("Failed to create database. ErrMessage: " + err.Error()) clientID,
} err.Error(),
_, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") )
if err != nil { }
log.Fatal("Failed to create stable. ErrMessage: " + err.Error()) fmt.Println("Now assignment:", partitions)
} for i := 0; i < len(partitions); i++ {
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters") // seek to the beginning
if err != nil { err = consumer.Seek(tmqcommon.TopicPartition{
log.Fatal("Failed to create topic. ErrMessage: " + err.Error()) Topic: partitions[i].Topic,
} Partition: partitions[i].Partition,
go func() { Offset: 0,
for i := 0; i < 10; i++ { }, 0)
time.Sleep(time.Second) if err != nil {
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)") log.Fatalf(
if err != nil { "Failed to seek offset, topic: %s, groupId: %s, clientId: %s, partition: %d, offset: %d, ErrMessage: %s\n",
log.Fatal("Failed to insert data. ErrMessage: " + err.Error()) topic,
} groupID,
} clientID,
done <- struct{}{} partitions[i].Partition,
}() 0,
} err.Error(),
)
}
}
fmt.Println("Assignment seek to beginning successfully")
// ANCHOR_END: seek
// ANCHOR: close
// unsubscribe
err = consumer.Unsubscribe()
if err != nil {
log.Fatalf(
"Failed to unsubscribe consumer, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
topic,
groupID,
clientID,
err.Error(),
)
}
fmt.Println("Consumer unsubscribed successfully.")
// close consumer
err = consumer.Close()
if err != nil {
log.Fatalf(
"Failed to close consumer, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
topic,
groupID,
clientID,
err.Error(),
)
}
fmt.Println("Consumer closed successfully.")
// ANCHOR_END: close
<-done
}
func initEnv(conn *sql.DB) {
_, err := conn.Exec("CREATE DATABASE IF NOT EXISTS power")
if err != nil {
log.Fatal("Failed to create database, ErrMessage: " + err.Error())
}
_, err = conn.Exec("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
if err != nil {
log.Fatal("Failed to create stable, ErrMessage: " + err.Error())
}
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters")
if err != nil {
log.Fatal("Failed to create topic, ErrMessage: " + err.Error())
}
go func() {
for i := 0; i < 10; i++ {
time.Sleep(time.Second)
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
if err != nil {
log.Fatal("Failed to insert data, ErrMessage: " + err.Error())
}
}
done <- struct{}{}
}()
}

View File

@ -25,6 +25,24 @@
<version>3.3.0</version> <version>3.3.0</version>
</dependency> </dependency>
<!-- ANCHOR_END: dep--> <!-- ANCHOR_END: dep-->
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.17</version>
</dependency>
<!-- HikariCP -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.locationtech.jts</groupId>
<artifactId>jts-core</artifactId>
<version>1.19.0</version>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.tmq.ConsumerRecord; import com.taosdata.jdbc.tmq.ConsumerRecord;
import com.taosdata.jdbc.tmq.ConsumerRecords; import com.taosdata.jdbc.tmq.ConsumerRecords;
@ -33,7 +33,7 @@ public abstract class AbsConsumerLoop {
config.setProperty("auto.commit.interval.ms", "1000"); config.setProperty("auto.commit.interval.ms", "1000");
config.setProperty("group.id", "group1"); config.setProperty("group.id", "group1");
config.setProperty("client.id", "client1"); config.setProperty("client.id", "client1");
config.setProperty("value.deserializer", "com.taosdata.example.AbsConsumerLoop$ResultDeserializer"); config.setProperty("value.deserializer", "com.taos.example.AbsConsumerLoop$ResultDeserializer");
config.setProperty("value.deserializer.encoding", "UTF-8"); config.setProperty("value.deserializer.encoding", "UTF-8");
try { try {
this.consumer = new TaosConsumer<>(config); this.consumer = new TaosConsumer<>(config);

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
@ -19,8 +19,10 @@ public class ConsumerLoopFull {
static private Connection connection; static private Connection connection;
static private Statement statement; static private Statement statement;
static private volatile boolean stopThread = false; static private volatile boolean stopThread = false;
static private String groupId = "group1";
static private String clientId = "clinet1";
public static TaosConsumer<ResultBean> getConsumer() throws SQLException { public static TaosConsumer<ResultBean> getConsumer() throws Exception {
// ANCHOR: create_consumer // ANCHOR: create_consumer
Properties config = new Properties(); Properties config = new Properties();
config.setProperty("td.connect.type", "jni"); config.setProperty("td.connect.type", "jni");
@ -30,33 +32,38 @@ public class ConsumerLoopFull {
config.setProperty("enable.auto.commit", "true"); config.setProperty("enable.auto.commit", "true");
config.setProperty("auto.commit.interval.ms", "1000"); config.setProperty("auto.commit.interval.ms", "1000");
config.setProperty("group.id", "group1"); config.setProperty("group.id", "group1");
config.setProperty("client.id", "1"); config.setProperty("client.id", "clinet1");
config.setProperty("td.connect.user", "root"); config.setProperty("td.connect.user", "root");
config.setProperty("td.connect.pass", "taosdata"); config.setProperty("td.connect.pass", "taosdata");
config.setProperty("value.deserializer", "com.taosdata.example.ConsumerLoopFull$ResultDeserializer"); config.setProperty("value.deserializer", "com.taos.example.ConsumerLoopFull$ResultDeserializer");
config.setProperty("value.deserializer.encoding", "UTF-8"); config.setProperty("value.deserializer.encoding", "UTF-8");
try { try {
TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config); TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config);
System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id")); System.out.printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s%n",
config.getProperty("bootstrap.servers"),
config.getProperty("group.id"),
config.getProperty("client.id"));
return consumer; return consumer;
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create native consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to create native consumer, host: " + config.getProperty("bootstrap.servers") // please refer to the JDBC specifications for detailed exceptions info
+ "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to create native consumer, host: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
throw new SQLException("Failed to create consumer", ex); config.getProperty("bootstrap.servers"),
config.getProperty("group.id"),
config.getProperty("client.id"),
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: create_consumer // ANCHOR_END: create_consumer
} }
public static void pollExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void pollExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: poll_data_code_piece // ANCHOR: poll_data_code_piece
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
// subscribe to the topics // subscribe to the topics
consumer.subscribe(topics); consumer.subscribe(topics);
System.out.println("Subscribe topics successfully."); System.out.println("Subscribe topics successfully.");
@ -65,27 +72,29 @@ public class ConsumerLoopFull {
ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100)); ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<ResultBean> record : records) { for (ConsumerRecord<ResultBean> record : records) {
ResultBean bean = record.value(); ResultBean bean = record.value();
// process the data here // Add your data processing logic here
System.out.println("data: " + JSON.toJSONString(bean)); System.out.println("data: " + JSON.toJSONString(bean));
} }
} }
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to poll data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to poll data", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to poll data, ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to poll data", ex); System.out.printf("Failed to poll data, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: poll_data_code_piece // ANCHOR_END: poll_data_code_piece
} }
public static void seekExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void seekExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: consumer_seek // ANCHOR: consumer_seek
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
// subscribe to the topics // subscribe to the topics
consumer.subscribe(topics); consumer.subscribe(topics);
System.out.println("Subscribe topics successfully."); System.out.println("Subscribe topics successfully.");
@ -100,13 +109,17 @@ public class ConsumerLoopFull {
consumer.seekToBeginning(assignment); consumer.seekToBeginning(assignment);
System.out.println("Assignment seek to beginning successfully."); System.out.println("Assignment seek to beginning successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("seek example failed", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("seek example failed", ex); System.out.printf("Failed to seek offset, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: consumer_seek // ANCHOR_END: consumer_seek
} }
@ -114,15 +127,14 @@ public class ConsumerLoopFull {
public static void commitExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void commitExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: commit_code_piece // ANCHOR: commit_code_piece
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
consumer.subscribe(topics); consumer.subscribe(topics);
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100)); ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<ResultBean> record : records) { for (ConsumerRecord<ResultBean> record : records) {
ResultBean bean = record.value(); ResultBean bean = record.value();
// process your data here // Add your data processing logic here
System.out.println("data: " + JSON.toJSONString(bean)); System.out.println("data: " + JSON.toJSONString(bean));
} }
if (!records.isEmpty()) { if (!records.isEmpty()) {
@ -131,13 +143,17 @@ public class ConsumerLoopFull {
System.out.println("Commit offset manually successfully."); System.out.println("Commit offset manually successfully.");
} }
} }
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to execute consumer functions. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to execute consumer functions", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to execute consumer functions. ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to execute consumer functions", ex); System.out.printf("Failed to commit offset, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: commit_code_piece // ANCHOR_END: commit_code_piece
} }
@ -150,13 +166,17 @@ public class ConsumerLoopFull {
// unsubscribe the consumer // unsubscribe the consumer
consumer.unsubscribe(); consumer.unsubscribe();
System.out.println("Consumer unsubscribed successfully."); System.out.println("Consumer unsubscribed successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to unsubscribe consumer", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to unsubscribe consumer. ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to unsubscribe consumer", ex); System.out.printf("Failed to unsubscribe consumer, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
finally { finally {
// close the consumer // close the consumer
@ -238,9 +258,14 @@ public class ConsumerLoopFull {
i++; i++;
Thread.sleep(1); Thread.sleep(1);
} }
} catch (SQLException ex) { } catch (Exception ex) {
System.out.println("Failed to insert data to power.meters, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to insert data to power.meters", ex); System.out.printf("Failed to insert data to power.meters, %sErrMessage: %s%n",
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
} }
@ -250,9 +275,14 @@ public class ConsumerLoopFull {
statement.executeUpdate("USE power"); statement.executeUpdate("USE power");
statement.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); statement.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters");
} catch (SQLException ex) { } catch (Exception ex) {
System.out.println("Failed to create db and table, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to create db and table", ex); System.out.printf("Failed to create db and table, %sErrMessage: %s%n",
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
} }
@ -311,7 +341,7 @@ public class ConsumerLoopFull {
try { try {
prepareData(); prepareData();
} catch (SQLException ex) { } catch (SQLException ex) {
System.out.println("Failed to prepare data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.println("Failed to prepare data, ErrCode:" + ex.getErrorCode() + ", ErrMessage: " + ex.getMessage());
return; return;
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage()); System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage());

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;

View File

@ -1,20 +1,14 @@
package com.taosdata.example; package com.taos.example;
import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.TSDBDriver;
import com.taosdata.jdbc.tmq.ConsumerRecords; import com.taosdata.jdbc.tmq.ConsumerRecords;
import com.taosdata.jdbc.tmq.TaosConsumer; import com.taosdata.jdbc.tmq.TaosConsumer;
import com.taosdata.jdbc.tmq.TopicPartition; import com.taosdata.jdbc.tmq.TopicPartition;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement;
import java.time.Duration; import java.time.Duration;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Scanner;
public class ConsumerOffsetSeek { public class ConsumerOffsetSeek {
@ -31,7 +25,7 @@ public class ConsumerOffsetSeek {
config.setProperty("auto.commit.interval.ms", "1000"); config.setProperty("auto.commit.interval.ms", "1000");
config.setProperty("group.id", "group1"); config.setProperty("group.id", "group1");
config.setProperty("client.id", "1"); config.setProperty("client.id", "1");
config.setProperty("value.deserializer", "com.taosdata.example.AbsConsumerLoop$ResultDeserializer"); config.setProperty("value.deserializer", "com.taos.example.AbsConsumerLoop$ResultDeserializer");
config.setProperty("value.deserializer.encoding", "UTF-8"); config.setProperty("value.deserializer.encoding", "UTF-8");
// ANCHOR: consumer_seek // ANCHOR: consumer_seek

View File

@ -0,0 +1,36 @@
package com.taosdata.example;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.Statement;
public class DruidDemo {
// ANCHOR: connection_pool
public static void main(String[] args) throws Exception {
String url = "jdbc:TAOS://127.0.0.1:6030/log";
DruidDataSource dataSource = new DruidDataSource();
// jdbc properties
dataSource.setDriverClassName("com.taosdata.jdbc.TSDBDriver");
dataSource.setUrl(url);
dataSource.setUsername("root");
dataSource.setPassword("taosdata");
// pool configurations
dataSource.setInitialSize(10);
dataSource.setMinIdle(10);
dataSource.setMaxActive(10);
dataSource.setMaxWait(30000);
dataSource.setValidationQuery("SELECT SERVER_VERSION()");
Connection connection = dataSource.getConnection(); // get connection
Statement statement = connection.createStatement(); // get statement
// query or insert
// ...
statement.close();
connection.close(); // put back to connection pool
dataSource.close();
}
// ANCHOR_END: connection_pool
}

View File

@ -0,0 +1,190 @@
package com.taos.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ByteOrderValues;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKBReader;
import org.locationtech.jts.io.WKBWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;
public class GeometryDemo {
private static String host = "localhost";
private static final String dbName = "test";
private static final String tbName = "weather";
private static final String user = "root";
private static final String password = "taosdata";
private Connection connection;
public static void main(String[] args) throws SQLException {
for (int i = 0; i < args.length; i++) {
if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1)
host = args[++i];
}
if (host == null) {
printHelp();
}
GeometryDemo demo = new GeometryDemo();
demo.init();
demo.createDatabase();
demo.useDatabase();
demo.dropTable();
demo.createTable();
demo.insert();
demo.stmtInsert();
demo.select();
demo.dropTable();
demo.close();
}
private void init() {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
try {
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
connection = DriverManager.getConnection(url, properties);
if (connection != null)
System.out.println("[ OK ] Connection established.");
} catch (SQLException e) {
e.printStackTrace();
}
}
private void createDatabase() {
String sql = "create database if not exists " + dbName;
execute(sql);
}
private void useDatabase() {
String sql = "use " + dbName;
execute(sql);
}
private void dropTable() {
final String sql = "drop table if exists " + dbName + "." + tbName + "";
execute(sql);
}
private void createTable() {
final String sql = "create table if not exists " + dbName + "." + tbName + " (ts timestamp, temperature float, humidity int, location geometry(50))";
execute(sql);
}
private void insert() {
final String sql = "insert into " + dbName + "." + tbName + " (ts, temperature, humidity, location) values(now, 20.5, 34, 'POINT(1 2)')";
execute(sql);
}
private void stmtInsert() throws SQLException {
TSDBPreparedStatement preparedStatement = (TSDBPreparedStatement) connection.prepareStatement("insert into " + dbName + "." + tbName + " values (?, ?, ?, ?)");
long current = System.currentTimeMillis();
ArrayList<Long> tsList = new ArrayList<>();
tsList.add(current);
tsList.add(current + 1);
preparedStatement.setTimestamp(0, tsList);
ArrayList<Float> tempList = new ArrayList<>();
tempList.add(20.1F);
tempList.add(21.2F);
preparedStatement.setFloat(1, tempList);
ArrayList<Integer> humList = new ArrayList<>();
humList.add(30);
humList.add(31);
preparedStatement.setInt(2, humList);
ArrayList<byte[]> list = new ArrayList<>();
GeometryFactory gf = new GeometryFactory();
Point p1 = gf.createPoint(new Coordinate(1,2));
p1.setSRID(1234);
// NOTE: TDengine current version only support 2D dimension and little endian byte order
WKBWriter w = new WKBWriter(2, ByteOrderValues.LITTLE_ENDIAN, true);
byte[] wkb = w.write(p1);
list.add(wkb);
Coordinate[] coordinates = { new Coordinate(10, 20),
new Coordinate(30, 40)};
LineString lineString = gf.createLineString(coordinates);
lineString.setSRID(2345);
byte[] wkb2 = w.write(lineString);
list.add(wkb2);
preparedStatement.setGeometry(3, list, 50);
preparedStatement.columnDataAddBatch();
preparedStatement.columnDataExecuteBatch();
}
private void select() {
final String sql = "select * from " + dbName + "." + tbName;
executeQuery(sql);
}
private void close() {
try {
if (connection != null) {
this.connection.close();
System.out.println("connection closed.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
private void executeQuery(String sql) {
long start = System.currentTimeMillis();
try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery(sql);
long end = System.currentTimeMillis();
printSql(sql, true, (end - start));
while (resultSet.next()){
byte[] result1 = resultSet.getBytes(4);
WKBReader reader = new WKBReader();
Geometry g1 = reader.read(result1);
System.out.println("GEO OBJ: " + g1 + ", SRID: " + g1.getSRID());
}
} catch (SQLException e) {
long end = System.currentTimeMillis();
printSql(sql, false, (end - start));
e.printStackTrace();
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
private void printSql(String sql, boolean succeed, long cost) {
System.out.println("[ " + (succeed ? "OK" : "ERROR!") + " ] time cost: " + cost + " ms, execute statement ====> " + sql);
}
private void execute(String sql) {
long start = System.currentTimeMillis();
try (Statement statement = connection.createStatement()) {
boolean execute = statement.execute(sql);
long end = System.currentTimeMillis();
printSql(sql, true, (end - start));
} catch (SQLException e) {
long end = System.currentTimeMillis();
printSql(sql, false, (end - start));
e.printStackTrace();
}
}
private static void printHelp() {
System.out.println("Usage: java -jar JDBCDemo.jar -host <hostname>");
System.exit(0);
}
}

View File

@ -0,0 +1,37 @@
package com.taosdata.example;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.Statement;
public class HikariDemo {
// ANCHOR: connection_pool
public static void main(String[] args) throws Exception {
HikariConfig config = new HikariConfig();
// jdbc properties
config.setJdbcUrl("jdbc:TAOS://127.0.0.1:6030/log");
config.setUsername("root");
config.setPassword("taosdata");
// connection pool configurations
config.setMinimumIdle(10); // minimum number of idle connection
config.setMaximumPoolSize(10); // maximum number of connection in the pool
config.setConnectionTimeout(30000); // maximum wait milliseconds for get connection from pool
config.setMaxLifetime(0); // maximum life time for each connection
config.setIdleTimeout(0); // max idle time for recycle idle connection
config.setConnectionTestQuery("SELECT SERVER_VERSION()"); // validation query
HikariDataSource dataSource = new HikariDataSource(config); // create datasource
Connection connection = dataSource.getConnection(); // get connection
Statement statement = connection.createStatement(); // get statement
// query or insert
// ...
statement.close();
connection.close(); // put back to connection pool
dataSource.close();
}
// ANCHOR_END: connection_pool
}

View File

@ -8,31 +8,33 @@ import java.util.Properties;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
public class JNIConnectExample { public class JNIConnectExample {
// ANCHOR: main // ANCHOR: main
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws Exception {
// use // use
// String jdbcUrl = "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata"; // String jdbcUrl =
// if you want to connect a specified database named "dbName". // "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata";
String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata"; // if you want to connect a specified database named "dbName".
Properties connProps = new Properties(); String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); Properties connProps = new Properties();
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)) { try (Connection conn = DriverManager.getConnection(jdbcUrl, connProps)) {
System.out.println("Connected to " + jdbcUrl + " successfully."); System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here // you can use the connection for execute SQL here
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to connect to %s, %sErrMessage: %s%n",
throw ex; jdbcUrl,
} catch (Exception ex){ ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
System.out.println("Failed to connect to " + jdbcUrl + "; ErrMessage: " + ex.getMessage()); ex.getMessage());
throw ex; // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
}
} }
// ANCHOR_END: main
} }
// ANCHOR_END: main
}

View File

@ -0,0 +1,125 @@
package com.taos.example;
import com.taosdata.jdbc.AbstractStatement;
import java.sql.*;
import java.util.Properties;
public class JdbcBasicDemo {
private static final String host = "localhost";
private static final String dbName = "test";
private static final String tbName = "weather";
private static final String user = "root";
private static final String password = "taosdata";
public static void main(String[] args) throws SQLException {
final String url = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
try (Connection connection = DriverManager.getConnection(url, properties)) {
if (connection != null) {
System.out.println("[ OK ] Connection established.");
} else {
System.out.println("[ ERR ] Connection can not be established.");
return;
}
Statement stmt = connection.createStatement();
// ANCHOR: create_db_and_table
// create database
stmt.executeUpdate("CREATE DATABASE IF NOT EXISTS power");
// use database
stmt.executeUpdate("USE power");
// create table
stmt.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
// ANCHOR_END: create_db_and_table
// ANCHOR: insert_data
// insert data
String insertQuery = "INSERT INTO " +
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) ";
int affectedRows = stmt.executeUpdate(insertQuery);
System.out.println("insert " + affectedRows + " rows.");
// ANCHOR_END: insert_data
// ANCHOR: query_data
// query data
ResultSet resultSet = stmt.executeQuery("SELECT * FROM meters");
Timestamp ts;
float current;
String location;
while (resultSet.next()) {
ts = resultSet.getTimestamp(1);
current = resultSet.getFloat(2);
location = resultSet.getString("location");
System.out.printf("%s, %f, %s\n", ts, current, location);
}
// ANCHOR_END: query_data
// ANCHOR: with_reqid
AbstractStatement aStmt = (AbstractStatement) connection.createStatement();
aStmt.execute("CREATE DATABASE IF NOT EXISTS power", 1L);
aStmt.executeUpdate("USE power", 2L);
try (ResultSet rs = aStmt.executeQuery("SELECT * FROM meters limit 1", 3L)) {
while (rs.next()) {
Timestamp timestamp = rs.getTimestamp(1);
System.out.println("timestamp = " + timestamp);
}
}
aStmt.close();
// ANCHOR_END: with_reqid
String sql = "SELECT * FROM meters limit 2;";
// ANCHOR: jdbc_exception
try (Statement statement = connection.createStatement();
// executeQuery
ResultSet tempResultSet = statement.executeQuery(sql)) {
// print result
printResult(tempResultSet);
} catch (Exception ex) {
// please refer to the JDBC specifications for detailed exceptions info
System.out.printf("Failed to execute statement, %sErrMessage: %s%n",
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
}
// ANCHOR_END: jdbc_exception
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Error Code: " + ex.getErrorCode());
System.out.println("Message: " + ex.getMessage());
}
}
private static void printResult(ResultSet resultSet) throws SQLException {
Util.printResult(resultSet);
}
}

View File

@ -1,6 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.AbstractStatement;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
@ -35,13 +33,13 @@ public class JdbcCreatDBDemo {
rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); rowsAffected = stmt.executeUpdate("CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
// you can check rowsAffected here // you can check rowsAffected here
System.out.println("Create stable power.meters successfully, rowsAffected: " + rowsAffected); System.out.println("Create stable power.meters successfully, rowsAffected: " + rowsAffected);
} catch (Exception ex) {
} catch (SQLException ex) { // please refer to the JDBC specifications for detailed exceptions info
// handle any errors, please refer to the JDBC specifications for detailed exceptions info System.out.printf("Failed to create database power or stable meters, %sErrMessage: %s%n",
System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
throw ex; ex.getMessage());
} catch (Exception ex){ // Print stack trace for context in examples. Use logging in production.
System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage()); ex.printStackTrace();
throw ex; throw ex;
} }
// ANCHOR_END: create_db_and_table // ANCHOR_END: create_db_and_table

View File

@ -0,0 +1,56 @@
package com.taos.example;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JdbcInsertDataDemo {
private static final String host = "localhost";
private static final String dbName = "test";
private static final String tbName = "weather";
private static final String user = "root";
private static final String password = "taosdata";
public static void main(String[] args) throws SQLException {
final String jdbcUrl = "jdbc:TAOS://" + host + ":6030/?user=" + user + "&password=" + password;
// get connection
Properties properties = new Properties();
properties.setProperty("charset", "UTF-8");
properties.setProperty("locale", "en_US.UTF-8");
properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting...");
// ANCHOR: insert_data
// insert data, please make sure the database and table are created before
String insertQuery = "INSERT INTO " +
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " +
"VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) ";
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement()) {
int affectedRows = stmt.executeUpdate(insertQuery);
// you can check affectedRows here
System.out.println("Successfully inserted " + affectedRows + " rows to power.meters.");
} catch (Exception ex) {
// please refer to the JDBC specifications for detailed exceptions info
System.out.printf("Failed to insert data to power.meters, sql: %s, %sErrMessage: %s%n",
insertQuery,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
}
// ANCHOR_END: insert_data
}
}

View File

@ -1,6 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.AbstractStatement;
import java.sql.*; import java.sql.*;
import java.util.Properties; import java.util.Properties;
@ -24,10 +22,11 @@ public class JdbcQueryDemo {
properties.setProperty("timezone", "UTC-8"); properties.setProperty("timezone", "UTC-8");
System.out.println("get connection starting..."); System.out.println("get connection starting...");
// ANCHOR: query_data // ANCHOR: query_data
String sql = "SELECT ts, current, location FROM power.meters limit 100";
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties); try (Connection connection = DriverManager.getConnection(jdbcUrl, properties);
Statement stmt = connection.createStatement(); Statement stmt = connection.createStatement();
// query data, make sure the database and table are created before // query data, make sure the database and table are created before
ResultSet resultSet = stmt.executeQuery("SELECT ts, current, location FROM power.meters limit 100")) { ResultSet resultSet = stmt.executeQuery(sql)) {
Timestamp ts; Timestamp ts;
float current; float current;
@ -41,19 +40,16 @@ public class JdbcQueryDemo {
// you can check data here // you can check data here
System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location); System.out.printf("ts: %s, current: %f, location: %s %n", ts, current, location);
} }
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to query data from power.meters, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to query data from power.meters, sql: %s, %sErrMessage: %s%n",
throw ex; sql,
} catch (Exception ex){ ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
System.out.println("Failed to query data from power.meters, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage()); ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
// ANCHOR_END: query_data // ANCHOR_END: query_data
} }
private static void printResult(ResultSet resultSet) throws SQLException {
Util.printResult(resultSet);
}
} }

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.AbstractStatement; import com.taosdata.jdbc.AbstractStatement;
@ -45,12 +45,13 @@ public class JdbcReqIdDemo {
} }
} }
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to execute sql with reqId: %s, %sErrMessage: %s%n", reqId,
throw ex; ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
} catch (Exception ex){ ex.getMessage());
System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage()); // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
// ANCHOR_END: with_reqid // ANCHOR_END: with_reqid

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.TSDBPreparedStatement; import com.taosdata.jdbc.TSDBPreparedStatement;
@ -68,12 +68,13 @@ public class ParameterBindingBasicDemo {
// you can check exeResult here // you can check exeResult here
System.out.println("Successfully inserted " + (numOfSubTable * numOfRow) + " rows to power.meters."); System.out.println("Successfully inserted " + (numOfSubTable * numOfRow) + " rows to power.meters.");
} }
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to insert to table meters using stmt, %sErrMessage: %s%n",
throw ex; ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
} catch (Exception ex){ ex.getMessage());
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage()); // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
} }

View File

@ -0,0 +1,325 @@
package com.taos.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
import com.taosdata.jdbc.utils.StringUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
// ANCHOR: para_bind
public class ParameterBindingFullDemo {
private static final String host = "127.0.0.1";
private static final Random random = new Random(System.currentTimeMillis());
private static final int BINARY_COLUMN_SIZE = 50;
private static final String[] schemaList = {
"create table stable1(ts timestamp, f1 tinyint, f2 smallint, f3 int, f4 bigint) tags(t1 tinyint, t2 smallint, t3 int, t4 bigint)",
"create table stable2(ts timestamp, f1 float, f2 double) tags(t1 float, t2 double)",
"create table stable3(ts timestamp, f1 bool) tags(t1 bool)",
"create table stable4(ts timestamp, f1 binary(" + BINARY_COLUMN_SIZE + ")) tags(t1 binary(" + BINARY_COLUMN_SIZE + "))",
"create table stable5(ts timestamp, f1 nchar(" + BINARY_COLUMN_SIZE + ")) tags(t1 nchar(" + BINARY_COLUMN_SIZE + "))",
"create table stable6(ts timestamp, f1 varbinary(" + BINARY_COLUMN_SIZE + ")) tags(t1 varbinary(" + BINARY_COLUMN_SIZE + "))",
"create table stable7(ts timestamp, f1 geometry(" + BINARY_COLUMN_SIZE + ")) tags(t1 geometry(" + BINARY_COLUMN_SIZE + "))",
};
private static final int numOfSubTable = 10, numOfRow = 10;
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:TAOS://" + host + ":6030/";
try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
init(conn);
bindInteger(conn);
bindFloat(conn);
bindBoolean(conn);
bindBytes(conn);
bindString(conn);
bindVarbinary(conn);
bindGeometry(conn);
clean(conn);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw ex;
} catch (Exception ex){
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage());
throw ex;
}
}
private static void init(Connection conn) throws SQLException {
clean(conn);
try (Statement stmt = conn.createStatement()) {
stmt.execute("create database if not exists test_parabind");
stmt.execute("use test_parabind");
for (int i = 0; i < schemaList.length; i++) {
stmt.execute(schemaList[i]);
}
}
}
private static void clean(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_parabind");
}
}
private static void bindInteger(Connection conn) throws SQLException {
String sql = "insert into ? using stable1 tags(?,?,?,?) values(?,?,?,?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t1_" + i);
// set tags
pstmt.setTagByte(0, Byte.parseByte(Integer.toString(random.nextInt(Byte.MAX_VALUE))));
pstmt.setTagShort(1, Short.parseShort(Integer.toString(random.nextInt(Short.MAX_VALUE))));
pstmt.setTagInt(2, random.nextInt(Integer.MAX_VALUE));
pstmt.setTagLong(3, random.nextLong());
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<Byte> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f1List.add(Byte.parseByte(Integer.toString(random.nextInt(Byte.MAX_VALUE))));
pstmt.setByte(1, f1List);
ArrayList<Short> f2List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f2List.add(Short.parseShort(Integer.toString(random.nextInt(Short.MAX_VALUE))));
pstmt.setShort(2, f2List);
ArrayList<Integer> f3List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f3List.add(random.nextInt(Integer.MAX_VALUE));
pstmt.setInt(3, f3List);
ArrayList<Long> f4List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f4List.add(random.nextLong());
pstmt.setLong(4, f4List);
// add column
pstmt.columnDataAddBatch();
}
// execute column
pstmt.columnDataExecuteBatch();
}
}
private static void bindFloat(Connection conn) throws SQLException {
String sql = "insert into ? using stable2 tags(?,?) values(?,?,?)";
TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class);
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t2_" + i);
// set tags
pstmt.setTagFloat(0, random.nextFloat());
pstmt.setTagDouble(1, random.nextDouble());
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<Float> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f1List.add(random.nextFloat());
pstmt.setFloat(1, f1List);
ArrayList<Double> f2List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f2List.add(random.nextDouble());
pstmt.setDouble(2, f2List);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
// close if no try-with-catch statement is used
pstmt.close();
}
private static void bindBoolean(Connection conn) throws SQLException {
String sql = "insert into ? using stable3 tags(?) values(?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t3_" + i);
// set tags
pstmt.setTagBoolean(0, random.nextBoolean());
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<Boolean> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++)
f1List.add(random.nextBoolean());
pstmt.setBoolean(1, f1List);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
}
}
private static void bindBytes(Connection conn) throws SQLException {
String sql = "insert into ? using stable4 tags(?) values(?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t4_" + i);
// set tags
pstmt.setTagString(0, new String("abc"));
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<String> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++) {
f1List.add(new String("abc"));
}
pstmt.setString(1, f1List, BINARY_COLUMN_SIZE);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
}
}
private static void bindString(Connection conn) throws SQLException {
String sql = "insert into ? using stable5 tags(?) values(?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t5_" + i);
// set tags
pstmt.setTagNString(0, "California.SanFrancisco");
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<String> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++) {
f1List.add("California.LosAngeles");
}
pstmt.setNString(1, f1List, BINARY_COLUMN_SIZE);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
}
}
private static void bindVarbinary(Connection conn) throws SQLException {
String sql = "insert into ? using stable6 tags(?) values(?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t6_" + i);
// set tags
byte[] bTag = new byte[]{0,2,3,4,5};
bTag[0] = (byte) i;
pstmt.setTagVarbinary(0, bTag);
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<byte[]> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++) {
byte[] v = new byte[]{0,2,3,4,5,6};
v[0] = (byte)j;
f1List.add(v);
}
pstmt.setVarbinary(1, f1List, BINARY_COLUMN_SIZE);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
}
}
private static void bindGeometry(Connection conn) throws SQLException {
String sql = "insert into ? using stable7 tags(?) values(?,?)";
try (TSDBPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSDBPreparedStatement.class)) {
byte[] g1 = StringUtils.hexToBytes("0101000000000000000000F03F0000000000000040");
byte[] g2 = StringUtils.hexToBytes("0102000020E610000002000000000000000000F03F000000000000004000000000000008400000000000001040");
List<byte[]> listGeo = new ArrayList<>();
listGeo.add(g1);
listGeo.add(g2);
for (int i = 1; i <= 2; i++) {
// set table name
pstmt.setTableName("t7_" + i);
// set tags
pstmt.setTagGeometry(0, listGeo.get(i - 1));
// set columns
ArrayList<Long> tsList = new ArrayList<>();
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++)
tsList.add(current + j);
pstmt.setTimestamp(0, tsList);
ArrayList<byte[]> f1List = new ArrayList<>();
for (int j = 0; j < numOfRow; j++) {
f1List.add(listGeo.get(i - 1));
}
pstmt.setGeometry(1, f1List, BINARY_COLUMN_SIZE);
// add column
pstmt.columnDataAddBatch();
}
// execute
pstmt.columnDataExecuteBatch();
}
}
}
// ANCHOR_END: para_bind

View File

@ -5,22 +5,24 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
public class RESTConnectExample { public class RESTConnectExample {
// ANCHOR: main // ANCHOR: main
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws Exception {
String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata"; String jdbcUrl = "jdbc:TAOS-RS://localhost:6041?user=root&password=taosdata";
try (Connection conn = DriverManager.getConnection(jdbcUrl)){ try (Connection conn = DriverManager.getConnection(jdbcUrl)) {
System.out.println("Connected to " + jdbcUrl + " successfully."); System.out.println("Connected to " + jdbcUrl + " successfully.");
// you can use the connection for execute SQL here // you can use the connection for execute SQL here
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to connect to %s, %sErrMessage: %s%n",
throw ex; jdbcUrl,
} catch (Exception ex){ ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
System.out.println("Failed to connect to " + jdbcUrl + "; ErrMessage: " + ex.getMessage()); ex.getMessage());
throw ex; // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
}
} }
// ANCHOR_END: main
} }
// ANCHOR_END: main
}

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.AbstractConnection; import com.taosdata.jdbc.AbstractConnection;
import com.taosdata.jdbc.enums.SchemalessProtocolType; import com.taosdata.jdbc.enums.SchemalessProtocolType;
@ -26,12 +26,13 @@ public class SchemalessJniTest {
conn.write(telnetDemo, SchemalessProtocolType.TELNET, SchemalessTimestampType.MILLI_SECONDS); conn.write(telnetDemo, SchemalessProtocolType.TELNET, SchemalessTimestampType.MILLI_SECONDS);
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED); conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
System.out.println("Inserted data with schemaless successfully."); System.out.println("Inserted data with schemaless successfully.");
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to insert data with schemaless, %sErrMessage: %s%n",
throw ex; ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
} catch (Exception ex){ ex.getMessage());
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + ex.getMessage()); // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
} }

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.AbstractConnection; import com.taosdata.jdbc.AbstractConnection;
import com.taosdata.jdbc.enums.SchemalessProtocolType; import com.taosdata.jdbc.enums.SchemalessProtocolType;
@ -26,12 +26,13 @@ public class SchemalessWsTest {
conn.write(telnetDemo, SchemalessProtocolType.TELNET, SchemalessTimestampType.MILLI_SECONDS); conn.write(telnetDemo, SchemalessProtocolType.TELNET, SchemalessTimestampType.MILLI_SECONDS);
conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.SECONDS); conn.write(jsonDemo, SchemalessProtocolType.JSON, SchemalessTimestampType.SECONDS);
System.out.println("Inserted data with schemaless successfully."); System.out.println("Inserted data with schemaless successfully.");
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to insert data with schemaless, %sErrMessage: %s%n",
throw ex; ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
} catch (Exception ex){ ex.getMessage());
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + ex.getMessage()); // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
} }

View File

@ -0,0 +1,25 @@
package com.taos.example;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class Util {
public static void printResult(ResultSet resultSet) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String columnLabel = metaData.getColumnLabel(i);
System.out.printf(" %s |", columnLabel);
}
System.out.println();
System.out.println("-------------------------------------------------------------");
while (resultSet.next()) {
for (int i = 1; i <= metaData.getColumnCount(); i++) {
String value = resultSet.getString(i);
System.out.printf("%s, ", value);
}
System.out.println();
}
}
}

View File

@ -9,7 +9,7 @@ import java.util.Properties;
public class WSConnectExample { public class WSConnectExample {
// ANCHOR: main // ANCHOR: main
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws Exception {
// use // use
// String jdbcUrl = // String jdbcUrl =
// "jdbc:TAOS-RS://localhost:6041/dbName?user=root&password=taosdata&batchfetch=true"; // "jdbc:TAOS-RS://localhost:6041/dbName?user=root&password=taosdata&batchfetch=true";
@ -25,14 +25,14 @@ public class WSConnectExample {
// you can use the connection for execute SQL here // you can use the connection for execute SQL here
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed
// exceptions info
System.out.println("Failed to connect to " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: "
+ ex.getMessage());
throw ex;
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to connect to " + jdbcUrl + "; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
System.out.printf("Failed to connect to %s, %sErrMessage: %s%n",
jdbcUrl,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
} }

View File

@ -1,10 +1,8 @@
package com.taosdata.example; package com.taos.example;
import com.taosdata.jdbc.TSDBPreparedStatement;
import com.taosdata.jdbc.ws.TSWSPreparedStatement; import com.taosdata.jdbc.ws.TSWSPreparedStatement;
import java.sql.*; import java.sql.*;
import java.util.ArrayList;
import java.util.Random; import java.util.Random;
// ANCHOR: para_bind // ANCHOR: para_bind
@ -47,12 +45,13 @@ public class WSParameterBindingBasicDemo {
System.out.println("Successfully inserted " + exeResult.length + " rows to power.meters."); System.out.println("Successfully inserted " + exeResult.length + " rows to power.meters.");
} }
} }
} catch (SQLException ex) { } catch (Exception ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to insert to table meters using stmt, %sErrMessage: %s%n",
throw ex; ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
} catch (Exception ex){ ex.getMessage());
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage()); // Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex; throw ex;
} }
} }

View File

@ -0,0 +1,180 @@
package com.taos.example;
import com.taosdata.jdbc.ws.TSWSPreparedStatement;
import java.sql.*;
import java.util.Random;
// ANCHOR: para_bind
public class WSParameterBindingFullDemo {
private static final String host = "127.0.0.1";
private static final Random random = new Random(System.currentTimeMillis());
private static final int BINARY_COLUMN_SIZE = 30;
private static final String[] schemaList = {
"create table stable1(ts timestamp, f1 tinyint, f2 smallint, f3 int, f4 bigint) tags(t1 tinyint, t2 smallint, t3 int, t4 bigint)",
"create table stable2(ts timestamp, f1 float, f2 double) tags(t1 float, t2 double)",
"create table stable3(ts timestamp, f1 bool) tags(t1 bool)",
"create table stable4(ts timestamp, f1 binary(" + BINARY_COLUMN_SIZE + ")) tags(t1 binary(" + BINARY_COLUMN_SIZE + "))",
"create table stable5(ts timestamp, f1 nchar(" + BINARY_COLUMN_SIZE + ")) tags(t1 nchar(" + BINARY_COLUMN_SIZE + "))"
};
private static final int numOfSubTable = 10, numOfRow = 10;
public static void main(String[] args) throws SQLException {
String jdbcUrl = "jdbc:TAOS-RS://" + host + ":6041/?batchfetch=true";
try (Connection conn = DriverManager.getConnection(jdbcUrl, "root", "taosdata")) {
init(conn);
bindInteger(conn);
bindFloat(conn);
bindBoolean(conn);
bindBytes(conn);
bindString(conn);
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw ex;
} catch (Exception ex){
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage());
throw ex;
}
}
private static void init(Connection conn) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("drop database if exists test_ws_parabind");
stmt.execute("create database if not exists test_ws_parabind");
stmt.execute("use test_ws_parabind");
for (int i = 0; i < schemaList.length; i++) {
stmt.execute(schemaList[i]);
}
}
}
private static void bindInteger(Connection conn) throws SQLException {
String sql = "insert into ? using stable1 tags(?,?,?,?) values(?,?,?,?,?)";
try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t1_" + i);
// set tags
pstmt.setTagByte(1, Byte.parseByte(Integer.toString(random.nextInt(Byte.MAX_VALUE))));
pstmt.setTagShort(2, Short.parseShort(Integer.toString(random.nextInt(Short.MAX_VALUE))));
pstmt.setTagInt(3, random.nextInt(Integer.MAX_VALUE));
pstmt.setTagLong(4, random.nextLong());
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(1, new Timestamp(current + j));
pstmt.setByte(2, Byte.parseByte(Integer.toString(random.nextInt(Byte.MAX_VALUE))));
pstmt.setShort(3, Short.parseShort(Integer.toString(random.nextInt(Short.MAX_VALUE))));
pstmt.setInt(4, random.nextInt(Integer.MAX_VALUE));
pstmt.setLong(5, random.nextLong());
pstmt.addBatch();
}
pstmt.executeBatch();
}
}
}
private static void bindFloat(Connection conn) throws SQLException {
String sql = "insert into ? using stable2 tags(?,?) values(?,?,?)";
try(TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t2_" + i);
// set tags
pstmt.setTagFloat(1, random.nextFloat());
pstmt.setTagDouble(2, random.nextDouble());
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(1, new Timestamp(current + j));
pstmt.setFloat(2, random.nextFloat());
pstmt.setDouble(3, random.nextDouble());
pstmt.addBatch();
}
pstmt.executeBatch();
}
}
}
private static void bindBoolean(Connection conn) throws SQLException {
String sql = "insert into ? using stable3 tags(?) values(?,?)";
try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t3_" + i);
// set tags
pstmt.setTagBoolean(1, random.nextBoolean());
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(1, new Timestamp(current + j));
pstmt.setBoolean(2, random.nextBoolean());
pstmt.addBatch();
}
pstmt.executeBatch();
}
}
}
private static void bindBytes(Connection conn) throws SQLException {
String sql = "insert into ? using stable4 tags(?) values(?,?)";
try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t4_" + i);
// set tags
pstmt.setTagString(1, new String("abc"));
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(1, new Timestamp(current + j));
pstmt.setString(2, "abc");
pstmt.addBatch();
}
pstmt.executeBatch();
}
}
}
private static void bindString(Connection conn) throws SQLException {
String sql = "insert into ? using stable5 tags(?) values(?,?)";
try (TSWSPreparedStatement pstmt = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement.class)) {
for (int i = 1; i <= numOfSubTable; i++) {
// set table name
pstmt.setTableName("t5_" + i);
// set tags
pstmt.setTagNString(1, "California.SanFrancisco");
// set columns
long current = System.currentTimeMillis();
for (int j = 0; j < numOfRow; j++) {
pstmt.setTimestamp(0, new Timestamp(current + j));
pstmt.setNString(1, "California.SanFrancisco");
pstmt.addBatch();
}
pstmt.executeBatch();
}
}
}
}
// ANCHOR_END: para_bind

View File

@ -1,4 +1,4 @@
package com.taosdata.example; package com.taos.example;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
@ -6,19 +6,23 @@ import com.taosdata.jdbc.tmq.*;
import java.sql.*; import java.sql.*;
import java.time.Duration; import java.time.Duration;
import java.util.*; import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
// ANCHOR: consumer_demo // ANCHOR: consumer_demo
public class WsConsumerLoopFull { public class WsConsumerLoopFull {
static private Connection connection; static private Connection connection;
static private Statement statement; static private Statement statement;
static private volatile boolean stopThread = false; static private volatile boolean stopThread = false;
static private String groupId = "group1";
static private String clientId = "clinet1";
public static TaosConsumer<ResultBean> getConsumer() throws SQLException { public static TaosConsumer<ResultBean> getConsumer() throws Exception {
// ANCHOR: create_consumer // ANCHOR: create_consumer
Properties config = new Properties(); Properties config = new Properties();
config.setProperty("td.connect.type", "ws"); config.setProperty("td.connect.type", "ws");
@ -28,33 +32,38 @@ public class WsConsumerLoopFull {
config.setProperty("enable.auto.commit", "true"); config.setProperty("enable.auto.commit", "true");
config.setProperty("auto.commit.interval.ms", "1000"); config.setProperty("auto.commit.interval.ms", "1000");
config.setProperty("group.id", "group1"); config.setProperty("group.id", "group1");
config.setProperty("client.id", "1"); config.setProperty("client.id", "clinet1");
config.setProperty("td.connect.user", "root"); config.setProperty("td.connect.user", "root");
config.setProperty("td.connect.pass", "taosdata"); config.setProperty("td.connect.pass", "taosdata");
config.setProperty("value.deserializer", "com.taosdata.example.WsConsumerLoopFull$ResultDeserializer"); config.setProperty("value.deserializer", "com.taos.example.WsConsumerLoopFull$ResultDeserializer");
config.setProperty("value.deserializer.encoding", "UTF-8"); config.setProperty("value.deserializer.encoding", "UTF-8");
try { try {
TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config); TaosConsumer<ResultBean> consumer= new TaosConsumer<>(config);
System.out.println("Create consumer successfully, host: " + config.getProperty("bootstrap.servers") + ", groupId: " + config.getProperty("group.id") + ", clientId: " + config.getProperty("client.id")); System.out.printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s%n",
config.getProperty("bootstrap.servers"),
config.getProperty("group.id"),
config.getProperty("client.id"));
return consumer; return consumer;
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create websocket consumer, host: " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to create websocket consumer, host: " + config.getProperty("bootstrap.servers") // please refer to the JDBC specifications for detailed exceptions info
+ "; ErrMessage: " + ex.getMessage()); System.out.printf("Failed to create websocket consumer, host: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
throw new SQLException("Failed to create consumer", ex); config.getProperty("bootstrap.servers"),
config.getProperty("group.id"),
config.getProperty("client.id"),
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: create_consumer // ANCHOR_END: create_consumer
} }
public static void pollExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void pollExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: poll_data_code_piece // ANCHOR: poll_data_code_piece
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
// subscribe to the topics // subscribe to the topics
consumer.subscribe(topics); consumer.subscribe(topics);
System.out.println("Subscribe topics successfully."); System.out.println("Subscribe topics successfully.");
@ -63,27 +72,29 @@ public class WsConsumerLoopFull {
ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100)); ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<ResultBean> record : records) { for (ConsumerRecord<ResultBean> record : records) {
ResultBean bean = record.value(); ResultBean bean = record.value();
// process the data here // Add your data processing logic here
System.out.println("data: " + JSON.toJSONString(bean)); System.out.println("data: " + JSON.toJSONString(bean));
} }
} }
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to poll data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to poll data", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to poll data, ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to poll data", ex); System.out.printf("Failed to poll data, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: poll_data_code_piece // ANCHOR_END: poll_data_code_piece
} }
public static void seekExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void seekExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: consumer_seek // ANCHOR: consumer_seek
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
// subscribe to the topics // subscribe to the topics
consumer.subscribe(topics); consumer.subscribe(topics);
System.out.println("Subscribe topics successfully."); System.out.println("Subscribe topics successfully.");
@ -98,13 +109,17 @@ public class WsConsumerLoopFull {
consumer.seekToBeginning(assignment); consumer.seekToBeginning(assignment);
System.out.println("Assignment seek to beginning successfully."); System.out.println("Assignment seek to beginning successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("seek example failed", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("seek example failed", ex); System.out.printf("Failed to seek offset, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: consumer_seek // ANCHOR_END: consumer_seek
} }
@ -112,15 +127,14 @@ public class WsConsumerLoopFull {
public static void commitExample(TaosConsumer<ResultBean> consumer) throws SQLException { public static void commitExample(TaosConsumer<ResultBean> consumer) throws SQLException {
// ANCHOR: commit_code_piece // ANCHOR: commit_code_piece
List<String> topics = Collections.singletonList("topic_meters");
try { try {
List<String> topics = Collections.singletonList("topic_meters");
consumer.subscribe(topics); consumer.subscribe(topics);
for (int i = 0; i < 50; i++) { for (int i = 0; i < 50; i++) {
ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100)); ConsumerRecords<ResultBean> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<ResultBean> record : records) { for (ConsumerRecord<ResultBean> record : records) {
ResultBean bean = record.value(); ResultBean bean = record.value();
// process your data here // Add your data processing logic here
System.out.println("data: " + JSON.toJSONString(bean)); System.out.println("data: " + JSON.toJSONString(bean));
} }
if (!records.isEmpty()) { if (!records.isEmpty()) {
@ -129,13 +143,17 @@ public class WsConsumerLoopFull {
System.out.println("Commit offset manually successfully."); System.out.println("Commit offset manually successfully.");
} }
} }
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to execute consumer functions. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to execute consumer functions", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to execute consumer functions. ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to execute consumer functions", ex); System.out.printf("Failed to commit offset, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
// ANCHOR_END: commit_code_piece // ANCHOR_END: commit_code_piece
} }
@ -148,13 +166,17 @@ public class WsConsumerLoopFull {
// unsubscribe the consumer // unsubscribe the consumer
consumer.unsubscribe(); consumer.unsubscribe();
System.out.println("Consumer unsubscribed successfully."); System.out.println("Consumer unsubscribed successfully.");
} catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to unsubscribe consumer. ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to unsubscribe consumer", ex);
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to unsubscribe consumer. ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to unsubscribe consumer", ex); System.out.printf("Failed to unsubscribe consumer, topic: %s, groupId: %s, clientId: %s, %sErrMessage: %s%n",
topics.get(0),
groupId,
clientId,
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
finally { finally {
// close the consumer // close the consumer
@ -236,9 +258,14 @@ public class WsConsumerLoopFull {
i++; i++;
Thread.sleep(1); Thread.sleep(1);
} }
} catch (SQLException ex) { } catch (Exception ex) {
System.out.println("Failed to insert data to power.meters, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to insert data to power.meters", ex); System.out.printf("Failed to insert data to power.meters, %sErrMessage: %s%n",
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
} }
@ -248,9 +275,14 @@ public class WsConsumerLoopFull {
statement.executeUpdate("USE power"); statement.executeUpdate("USE power");
statement.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"); statement.executeUpdate("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))");
statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); statement.executeUpdate("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters");
} catch (SQLException ex) { } catch (Exception ex) {
System.out.println("Failed to create db and table, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); // please refer to the JDBC specifications for detailed exceptions info
throw new SQLException("Failed to create db and table", ex); System.out.printf("Failed to create db and table, %sErrMessage: %s%n",
ex instanceof SQLException ? "ErrCode: " + ((SQLException) ex).getErrorCode() + ", " : "",
ex.getMessage());
// Print stack trace for context in examples. Use logging in production.
ex.printStackTrace();
throw ex;
} }
} }
@ -309,7 +341,7 @@ public class WsConsumerLoopFull {
try { try {
prepareData(); prepareData();
} catch (SQLException ex) { } catch (SQLException ex) {
System.out.println("Failed to prepare data, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.println("Failed to prepare data, ErrCode:" + ex.getErrorCode() + ", ErrMessage: " + ex.getMessage());
return; return;
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage()); System.out.println("Failed to prepare data, ErrMessage: " + ex.getMessage());
@ -334,8 +366,7 @@ public class WsConsumerLoopFull {
consumer.unsubscribe(); consumer.unsubscribe();
unsubscribeExample(consumer); unsubscribeExample(consumer);
System.out.println("unsubscribeExample executed successfully."); System.out.println("unsubscribeExample executed successfully");
} catch (SQLException ex) { } catch (SQLException ex) {
System.out.println("Failed to poll data from topic_meters, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); System.out.println("Failed to poll data from topic_meters, ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
return; return;

View File

@ -1,9 +1,12 @@
package com.taosdata.example; package com.taos.example;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.taosdata.jdbc.TSDBDriver; import com.taosdata.jdbc.TSDBDriver;
import java.sql.*; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; import java.util.Properties;
import java.util.Scanner; import java.util.Scanner;

View File

@ -40,12 +40,12 @@ public class TestAll {
} }
@Test @Test
public void testJNIConnect() throws SQLException { public void testJNIConnect() throws Exception {
JNIConnectExample.main(args); JNIConnectExample.main(args);
} }
@Test @Test
public void testRestConnect() throws SQLException { public void testRestConnect() throws Exception {
RESTConnectExample.main(args); RESTConnectExample.main(args);
} }

View File

@ -28,7 +28,7 @@ async function test() {
console.log("Inserted data with schemaless successfully.") console.log("Inserted data with schemaless successfully.")
} }
catch (err) { catch (err) {
console.error("Failed to insert data with schemaless, url:"+ dsn +", ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to insert data with schemaless, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (wsRows) { if (wsRows) {
@ -40,4 +40,4 @@ async function test() {
taos.destroy(); taos.destroy();
} }
} }
test() test()

View File

@ -9,11 +9,11 @@ async function createConnect() {
conf.setUser('root'); conf.setUser('root');
conf.setPwd('taosdata'); conf.setPwd('taosdata');
conf.setDb('power'); conf.setDb('power');
conn = await taos.sqlConnect(conf); conn = await taos.sqlConnect(conf);
console.log("Connected to " + dsn + " successfully."); console.log("Connected to " + dsn + " successfully.");
return conn; return conn;
} catch (err) { } catch (err) {
console.log("Failed to connect to " + dns + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.log("Failed to connect to " + dsn + ", ErrCode: " + err.code + ", ErrMessage: " + err.message);
throw err; throw err;
} }
@ -29,13 +29,13 @@ async function createDbAndTable() {
await wsSql.exec('CREATE DATABASE IF NOT EXISTS power'); await wsSql.exec('CREATE DATABASE IF NOT EXISTS power');
console.log("Create database power successfully."); console.log("Create database power successfully.");
// create table // create table
await wsSql.exec('CREATE STABLE IF NOT EXISTS power.meters ' + await wsSql.exec('CREATE STABLE IF NOT EXISTS power.meters ' +
'(_ts timestamp, current float, voltage int, phase float) ' + '(_ts timestamp, current float, voltage int, phase float) ' +
'TAGS (location binary(64), groupId int);'); 'TAGS (location binary(64), groupId int);');
console.log("Create stable power.meters successfully"); console.log("Create stable power.meters successfully");
} catch (err) { } catch (err) {
console.error("Failed to create db and table, url:" + dns + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to create database power or stable meters, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} finally { } finally {
if (wsSql) { if (wsSql) {
await wsSql.close(); await wsSql.close();
@ -51,18 +51,18 @@ async function insertData() {
try { try {
wsSql = await createConnect(); wsSql = await createConnect();
let insertQuery = "INSERT INTO " + let insertQuery = "INSERT INTO " +
"power.d1001 USING power.meters (location, groupId) TAGS('California.SanFrancisco', 2) " + "power.d1001 USING power.meters (location, groupId) TAGS('California.SanFrancisco', 2) " +
"VALUES " + "VALUES " +
"(NOW + 1a, 10.30000, 219, 0.31000) " + "(NOW + 1a, 10.30000, 219, 0.31000) " +
"(NOW + 2a, 12.60000, 218, 0.33000) " + "(NOW + 2a, 12.60000, 218, 0.33000) " +
"(NOW + 3a, 12.30000, 221, 0.31000) " + "(NOW + 3a, 12.30000, 221, 0.31000) " +
"power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) " + "power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) " +
"VALUES " + "VALUES " +
"(NOW + 1a, 10.30000, 218, 0.25000) "; "(NOW + 1a, 10.30000, 218, 0.25000) ";
taosResult = await wsSql.exec(insertQuery); taosResult = await wsSql.exec(insertQuery);
console.log("Successfully inserted " + taosResult.getAffectRows() + " rows to power.meters."); console.log("Successfully inserted " + taosResult.getAffectRows() + " rows to power.meters.");
} catch (err) { } catch (err) {
console.error("Failed to insert data to power.meters, url:" + dsn + "; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to insert data to power.meters, sql: ${insertQuery}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} finally { } finally {
if (wsSql) { if (wsSql) {
await wsSql.close(); await wsSql.close();
@ -75,21 +75,22 @@ async function insertData() {
async function queryData() { async function queryData() {
let wsRows = null; let wsRows = null;
let wsSql = null; let wsSql = null;
let sql = 'SELECT ts, current, location FROM power.meters limit 100';
try { try {
wsSql = await createConnect(); wsSql = await createConnect();
wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100'); wsRows = await wsSql.query(sql);
while (await wsRows.next()) { while (await wsRows.next()) {
let row = wsRows.getData(); let row = wsRows.getData();
console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]); console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]);
} }
} }
catch (err) { catch (err) {
console.error("Failed to query data from power.meters, url:" + dsn + " ; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to query data from power.meters, sql: ${sql}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (wsRows) { if (wsRows) {
await wsRows.close(); await wsRows.close();
} }
if (wsSql) { if (wsSql) {
await wsSql.close(); await wsSql.close();
} }
@ -107,16 +108,16 @@ async function sqlWithReqid() {
wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100', reqId); wsRows = await wsSql.query('SELECT ts, current, location FROM power.meters limit 100', reqId);
while (await wsRows.next()) { while (await wsRows.next()) {
let row = wsRows.getData(); let row = wsRows.getData();
console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]); console.log('ts: ' + row[0] + ', current: ' + row[1] + ', location: ' + row[2]);
} }
} }
catch (err) { catch (err) {
console.error("Failed to execute sql with reqId: " + reqId + ", ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to query data from power.meters, reqId: ${reqId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (wsRows) { if (wsRows) {
await wsRows.close(); await wsRows.close();
} }
if (wsSql) { if (wsSql) {
await wsSql.close(); await wsSql.close();
} }
@ -129,7 +130,7 @@ async function test() {
await insertData(); await insertData();
await queryData(); await queryData();
await sqlWithReqid(); await sqlWithReqid();
taos.destroy(); taos.destroy();
} }
test() test()

View File

@ -3,7 +3,7 @@ const taos = require("@tdengine/websocket");
let db = 'power'; let db = 'power';
let stable = 'meters'; let stable = 'meters';
let numOfSubTable = 10; let numOfSubTable = 10;
let numOfRow = 10; let numOfRow = 10;
let dsn = 'ws://localhost:6041' let dsn = 'ws://localhost:6041'
function getRandomInt(min, max) { function getRandomInt(min, max) {
min = Math.ceil(min); min = Math.ceil(min);
@ -59,7 +59,7 @@ async function prepare() {
} }
} }
catch (err) { catch (err) {
console.error("Failed to insert to table meters using stmt, url:" + dsn + "ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to insert to table meters using stmt, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (stmt) { if (stmt) {

View File

@ -3,12 +3,16 @@ const taos = require("@tdengine/websocket");
// ANCHOR: create_consumer // ANCHOR: create_consumer
const db = 'power'; const db = 'power';
const stable = 'meters'; const stable = 'meters';
const topics = ['power_meters_topic'];
const url = 'ws://localhost:6041'; const url = 'ws://localhost:6041';
const topic = 'topic_meters'
const topics = [topic];
const groupId = "group1";
const clientId = "client1";
async function createConsumer() { async function createConsumer() {
let groupId = "group1"; let groupId = "group1";
let clientId = "1"; let clientId = "client1";
let configMap = new Map([ let configMap = new Map([
[taos.TMQConstants.GROUP_ID, groupId], [taos.TMQConstants.GROUP_ID, groupId],
[taos.TMQConstants.CLIENT_ID, clientId], [taos.TMQConstants.CLIENT_ID, clientId],
@ -23,11 +27,11 @@ async function createConsumer() {
conn = await taos.tmqConnect(configMap); conn = await taos.tmqConnect(configMap);
console.log(`Create consumer successfully, host: ${url}, groupId: ${groupId}, clientId: ${clientId}`) console.log(`Create consumer successfully, host: ${url}, groupId: ${groupId}, clientId: ${clientId}`)
return conn; return conn;
}catch (err) { } catch (err) {
console.log("Failed to create websocket consumer, ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to create websocket consumer, topic: ${topic}, groupId: ${groupId}, clientId: ${clientId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
throw err; throw err;
} }
} }
// ANCHOR_END: create_consumer // ANCHOR_END: create_consumer
@ -38,7 +42,7 @@ async function prepare() {
conf.setDb('power'); conf.setDb('power');
const createDB = `CREATE DATABASE IF NOT EXISTS ${db}`; const createDB = `CREATE DATABASE IF NOT EXISTS ${db}`;
const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`;
let wsSql = await taos.sqlConnect(conf); let wsSql = await taos.sqlConnect(conf);
await wsSql.exec(createDB); await wsSql.exec(createDB);
await wsSql.exec(createStable); await wsSql.exec(createStable);
@ -60,13 +64,14 @@ async function subscribe(consumer) {
for (let i = 0; i < 50; i++) { for (let i = 0; i < 50; i++) {
let res = await consumer.poll(100); let res = await consumer.poll(100);
for (let [key, value] of res) { for (let [key, value] of res) {
// Add your data processing logic here
console.log(`data: ${key} ${value}`); console.log(`data: ${key} ${value}`);
} }
consumer.commit(); await consumer.commit();
console.log("Commit offset manually successfully."); console.log("Commit offset manually successfully.");
} }
} catch (err) { } catch (err) {
console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to poll data, topic: ${topic}, groupId: ${groupId}, clientId: ${clientId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
throw err; throw err;
} }
// ANCHOR_END: commit // ANCHOR_END: commit
@ -77,17 +82,18 @@ async function test() {
let consumer = null; let consumer = null;
try { try {
await prepare(); await prepare();
let consumer = await createConsumer() consumer = await createConsumer()
await subscribe(consumer) await subscribe(consumer)
await consumer.unsubscribe(); await consumer.unsubscribe();
console.log("Consumer unsubscribed successfully."); console.log("Consumer unsubscribed successfully.");
} }
catch (err) { catch (err) {
console.error("Failed to unsubscribe consume, ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to unsubscribe consumer, topic: ${topic}, groupId: ${groupId}, clientId: ${clientId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (consumer) { if (consumer) {
await consumer.close(); await consumer.close();
console.log("Consumer closed successfully.");
} }
taos.destroy(); taos.destroy();
} }

View File

@ -2,7 +2,11 @@ const taos = require("@tdengine/websocket");
const db = 'power'; const db = 'power';
const stable = 'meters'; const stable = 'meters';
const topics = ['power_meters_topic']; const topic = 'topic_meters'
const topics = [topic];
const groupId = "group1";
const clientId = "client1";
// ANCHOR: create_consumer // ANCHOR: create_consumer
async function createConsumer() { async function createConsumer() {
@ -19,7 +23,7 @@ async function createConsumer() {
try { try {
return await taos.tmqConnect(configMap); return await taos.tmqConnect(configMap);
} catch (err) { } catch (err) {
console.log(err); console.error(err);
throw err; throw err;
} }
@ -31,7 +35,7 @@ async function prepare() {
conf.setUser('root'); conf.setUser('root');
conf.setPwd('taosdata'); conf.setPwd('taosdata');
conf.setDb('power'); conf.setDb('power');
const createDB = `CREATE DATABASE IF NOT EXISTS POWER ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`; const createDB = `CREATE DATABASE IF NOT EXISTS ${db} KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;`;
const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`; const createStable = `CREATE STABLE IF NOT EXISTS ${db}.${stable} (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);`;
let wsSql = await taos.sqlConnect(conf); let wsSql = await taos.sqlConnect(conf);
@ -45,7 +49,7 @@ async function prepare() {
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
await wsSql.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`); await wsSql.exec(`INSERT INTO d1001 USING ${stable} (location, groupId) TAGS ("California.SanFrancisco", 3) VALUES (NOW, ${10 + i}, ${200 + i}, ${0.32 + i})`);
} }
wsSql.Close(); await wsSql.close();
} }
// ANCHOR: subscribe // ANCHOR: subscribe
@ -55,11 +59,12 @@ async function subscribe(consumer) {
for (let i = 0; i < 50; i++) { for (let i = 0; i < 50; i++) {
let res = await consumer.poll(100); let res = await consumer.poll(100);
for (let [key, value] of res) { for (let [key, value] of res) {
// Add your data processing logic here
console.log(`data: ${key} ${value}`); console.log(`data: ${key} ${value}`);
} }
} }
} catch (err) { } catch (err) {
console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to poll data, topic: ${topic}, groupId: ${groupId}, clientId: ${clientId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
throw err; throw err;
} }
@ -83,7 +88,7 @@ async function test() {
console.log("Assignment seek to beginning successfully"); console.log("Assignment seek to beginning successfully");
} }
catch (err) { catch (err) {
console.error("Seek example failed, ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error(`Failed to seek offset, topic: ${topic}, groupId: ${groupId}, clientId: ${clientId}, ErrCode: ${err.code}, ErrMessage: ${err.message}`);
} }
finally { finally {
if (consumer) { if (consumer) {

View File

@ -14,11 +14,11 @@ def create_connection():
) )
print(f"Connected to {host}:{port} successfully."); print(f"Connected to {host}:{port} successfully.");
except Exception as err: except Exception as err:
print(f"Failed to connect to {host}:{port} ; ErrMessage:{err}") print(f"Failed to connect to {host}:{port} , ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()
if __name__ == "__main__": if __name__ == "__main__":
create_connection() create_connection()

View File

@ -12,7 +12,7 @@ def create_connection():
print(f"Connected to {url} successfully."); print(f"Connected to {url} successfully.");
except Exception as err: except Exception as err:
print(f"Failed to connect to {url} ; ErrMessage:{err}") print(f"Failed to connect to {url} , ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -14,7 +14,7 @@ def create_connection():
) )
print(f"Connected to {host}:{port} successfully."); print(f"Connected to {host}:{port} successfully.");
except Exception as err: except Exception as err:
print(f"Failed to connect to {host}:{port} ; ErrMessage:{err}") print(f"Failed to connect to {host}:{port} , ErrMessage:{err}")
return conn return conn
# ANCHOR_END: connect # ANCHOR_END: connect

View File

@ -20,7 +20,7 @@ try:
print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}");
except Exception as err: except Exception as err:
print(f"Failed to create db and table, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to create database power or stable meters, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -19,7 +19,7 @@ try:
print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}");
except Exception as err: except Exception as err:
print(f"Failed to create db and table, url:{url} ; ErrMessage:{err}") print(f"Failed to create database power or stable meters, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -20,7 +20,7 @@ try:
print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}"); print(f"Create stable power.meters successfully, rowsAffected: {rowsAffected}");
except Exception as err: except Exception as err:
print(f"Failed to create db and table, db addrr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to create database power or stable meters, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -21,7 +21,7 @@ try:
print(f"Successfully inserted {affectedRows} rows to power.meters.") print(f"Successfully inserted {affectedRows} rows to power.meters.")
except Exception as err: except Exception as err:
print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to insert data to power.meters, sql: {sql}, ErrMessage: {err}.")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -20,7 +20,7 @@ try:
print(f"Successfully inserted {affectedRows} rows to power.meters.") print(f"Successfully inserted {affectedRows} rows to power.meters.")
except Exception as err: except Exception as err:
print(f"Failed to insert data to power.meters, url:{url} ; ErrMessage:{err}") print(f"Failed to insert data to power.meters, sql:{sql}, ErrMessage:{err}.")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -21,7 +21,7 @@ try:
print(f"Successfully inserted {affectedRows} rows to power.meters.") print(f"Successfully inserted {affectedRows} rows to power.meters.")
except Exception as err: except Exception as err:
print(f"Failed to insert data to power.meters, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to insert data to power.meters, sql: {sql}, ErrMessage: {err}.")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -7,15 +7,15 @@ try:
port=port, port=port,
user="root", user="root",
password="taosdata") password="taosdata")
sql = "SELECT ts, current, location FROM power.meters limit 100"
result = conn.query("SELECT ts, current, location FROM power.meters limit 100") result = conn.query(sql)
# Get data from result as list of tuple # Get data from result as list of tuple
data = result.fetch_all() data = result.fetch_all()
for row in data: for row in data:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to query data from power.meters, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to query data from power.meters, sql: {sql}, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -7,11 +7,11 @@ try:
user="root", user="root",
password="taosdata", password="taosdata",
timeout=30) timeout=30)
sql = f"SELECT ts, current, location FROM power.meters limit 100"
result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100") result = client.sql(sql)
if result["data"]: if result["data"]:
for row in result["data"]: for row in result["data"]:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to query data from power.meters, url:{url} ; ErrMessage:{err}") print(f"Failed to query data from power.meters, sql: {sql}, ErrMessage:{err}")

View File

@ -8,13 +8,13 @@ try:
password="taosdata", password="taosdata",
host=host, host=host,
port=port) port=port)
sql = "SELECT ts, current, location FROM power.meters limit 100"
result = conn.query("SELECT ts, current, location FROM power.meters limit 100") result = conn.query(sql)
for row in result: for row in result:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to query data from power.meters, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to query data from power.meters, sql: {sql}, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -17,7 +17,7 @@ try:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to execute sql with reqId:{reqId}, ErrMessage:{err}")
finally: finally:
if conn: if conn:

View File

@ -15,4 +15,4 @@ try:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to execute sql with reqId:{reqId}, url:{url} ; ErrMessage:{err}") print(f"Failed to execute sql with reqId:{reqId}, ErrMessage:{err}")

View File

@ -18,7 +18,7 @@ try:
print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}") print(f"ts: {row[0]}, current: {row[1]}, location: {row[2]}")
except Exception as err: except Exception as err:
print(f"Failed to execute sql with reqId:{reqId}, db addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to execute sql with reqId:{reqId}, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -34,7 +34,7 @@ try:
) )
print("Inserted data with schemaless successfully."); print("Inserted data with schemaless successfully.");
except Exception as err: except Exception as err:
print(f"Failed to insert data with schemaless, addr: {host}:{port} ErrMessage:{err}") print(f"Failed to insert data with schemaless, ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -1,19 +1,21 @@
import taosws import taosws
host = "localhost"
port = 6041
def prepare(): def prepare():
conn = None conn = None
try: try:
conn = taosws.connect(user="root", conn = taosws.connect(user="root",
password="taosdata", password="taosdata",
host="localhost", host=host,
port=6041) port=port)
# create database # create database
rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power") rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS power")
assert rowsAffected == 0 assert rowsAffected == 0
except Exception as err: except Exception as err:
print(f"Failed to create db and table, err:{err}") print(f"Failed to create db and table, db addrr:{host}:{port} ; ErrMessage:{err}")
raise err raise err
finally: finally:
if conn: if conn:
@ -32,8 +34,7 @@ def schemaless_insert():
jsonDemo = [ jsonDemo = [
'{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}'
] ]
host = "localhost"
port = 6041
try: try:
conn = taosws.connect(user="root", conn = taosws.connect(user="root",
password="taosdata", password="taosdata",
@ -66,7 +67,7 @@ def schemaless_insert():
) )
print("Inserted data with schemaless successfully."); print("Inserted data with schemaless successfully.");
except Exception as err: except Exception as err:
print(f"Failed to insert data with schemaless, addr: {host}:{port} ErrMessage:{err}") print(f"Failed to insert data with schemaless, ErrMessage:{err}")
raise err raise err
finally: finally:
@ -76,6 +77,6 @@ def schemaless_insert():
if __name__ == "__main__": if __name__ == "__main__":
try: try:
prepare() prepare()
schemaless_insert schemaless_insert()
except Exception as err: except Exception as err:
print(f"Failed to insert data with schemaless, err:{err}") print(f"Failed to insert data with schemaless, err:{err}")

View File

@ -7,8 +7,8 @@ numOfRow = 10
conn = None conn = None
stmt = None stmt = None
host="localhost", host="localhost"
port=6030, port=6030
try: try:
conn = taos.connect( conn = taos.connect(
user="root", user="root",
@ -56,7 +56,7 @@ try:
print(f"Successfully inserted to power.meters.") print(f"Successfully inserted to power.meters.")
except Exception as err: except Exception as err:
print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to insert to table meters using stmt, ErrMessage:{err}")
finally: finally:
if stmt: if stmt:
stmt.close() stmt.close()

View File

@ -13,8 +13,8 @@ port=6041
try: try:
conn = taosws.connect(user="root", conn = taosws.connect(user="root",
password="taosdata", password="taosdata",
host="localhost", host=host,
port=6041) port=port)
conn.execute("CREATE DATABASE IF NOT EXISTS power") conn.execute("CREATE DATABASE IF NOT EXISTS power")
conn.execute("USE power") conn.execute("USE power")
@ -61,7 +61,7 @@ try:
print(f"Successfully inserted to power.meters.") print(f"Successfully inserted to power.meters.")
except Exception as err: except Exception as err:
print(f"Failed to insert to table meters using stmt, addr:{host}:{port} ; ErrMessage:{err}") print(f"Failed to insert to table meters using stmt, ErrMessage:{err}")
finally: finally:
if stmt: if stmt:
stmt.close() stmt.close()

View File

@ -1,17 +1,24 @@
#!/usr/bin/python3
import taos import taos
db = "power"
topic = "topic_meters"
user = "root"
password = "taosdata"
host = "localhost"
port = 6030
groupId = "group1"
clientId = "1"
tdConnWsScheme = "ws"
autoOffsetReset = "latest"
autoCommitState = "true"
autoCommitIntv = "1000"
def prepareMeta(): def prepareMeta():
conn = None conn = None
try: try:
conn = taos.connect( conn = taos.connect(host=host, user=user, password=password, port=port)
host="localhost",
user="root",
password="taosdata",
port=6030,
)
db = "power"
topic = "topic_meters"
conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}")
# change database. same as execute "USE db" # change database. same as execute "USE db"
@ -33,13 +40,13 @@ def prepareMeta():
power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco')
VALUES (NOW + 1a, 10.30000, 219, 0.31000) VALUES (NOW + 1a, 10.30000, 219, 0.31000)
(NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000) (NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000)
power.d1002 USING power.meters (groupid, location) TAGS(3, 'California.SanFrancisco') power.d1002 USING power.meters (groupid, location) TAGS(3, 'California.SanFrancisco')
VALUES (NOW + 1a, 10.30000, 218, 0.25000) VALUES (NOW + 1a, 10.30000, 218, 0.25000)
""" """
affectedRows = conn.execute(sql) affectedRows = conn.execute(sql)
print(f"Inserted into {affectedRows} rows to power.meters successfully.") print(f"Inserted into {affectedRows} rows to power.meters successfully.")
except Exception as err: except Exception as err:
print(f"Prepare insert data error, ErrMessage:{err}") print(f"Failed to prepareMeta, host: {host}:{port}, db: {db}, topic: {topic}, ErrMessage:{err}.")
raise err raise err
finally: finally:
if conn: if conn:
@ -49,28 +56,24 @@ def prepareMeta():
from taos.tmq import Consumer from taos.tmq import Consumer
def create_consumer(): def create_consumer():
host = "localhost"
port = 6030
groupId = "group1"
clientId = "1"
try: try:
consumer = Consumer( consumer = Consumer(
{ {
"group.id": groupId, "group.id": groupId,
"client.id": clientId, "client.id": clientId,
"td.connect.user": "root", "td.connect.user": user,
"td.connect.pass": "taosdata", "td.connect.pass": password,
"enable.auto.commit": "true", "enable.auto.commit": autoCommitState,
"auto.commit.interval.ms": "1000", "auto.commit.interval.ms": autoCommitIntv,
"auto.offset.reset": "latest", "auto.offset.reset": autoOffsetReset,
"td.connect.ip": host, "td.connect.ip": host,
"td.connect.port": port, "td.connect.port": str(port),
} }
) )
print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}")
return consumer return consumer
except Exception as err: except Exception as err:
print(f"Failed to create native consumer, host: {host}:{port} ; ErrMessage:{err}"); print(f"Failed to create native consumer, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
# ANCHOR_END: create_consumer # ANCHOR_END: create_consumer
@ -96,7 +99,7 @@ def subscribe(consumer):
print(f"data: {data}") print(f"data: {data}")
except Exception as err: except Exception as err:
print(f"Failed to poll data, ErrMessage:{err}") print(f"Failed to poll data, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
@ -123,7 +126,7 @@ def commit_offset(consumer):
print("Commit offset manually successfully."); print("Commit offset manually successfully.");
except Exception as err: except Exception as err:
print(f"Failed to poll data, ErrMessage:{err}") print(f"Failed to commit offset, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
# ANCHOR_END: commit_offset # ANCHOR_END: commit_offset
@ -136,9 +139,9 @@ def seek_offset(consumer):
for partition in assignments: for partition in assignments:
partition.offset = 0 partition.offset = 0
consumer.seek(partition) consumer.seek(partition)
print(f"Assignment seek to beginning successfully"); print(f"Assignment seek to beginning successfully.")
except Exception as err: except Exception as err:
print(f"Seek example failed; ErrMessage:{err}") print(f"Failed to seek offset, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
# ANCHOR_END: assignment # ANCHOR_END: assignment
@ -148,10 +151,11 @@ def unsubscribe(consumer):
consumer.unsubscribe() consumer.unsubscribe()
print("Consumer unsubscribed successfully."); print("Consumer unsubscribed successfully.");
except Exception as err: except Exception as err:
print(f"Failed to unsubscribe consumer. ErrMessage:{err}") print(f"Failed to unsubscribe consumer. topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
finally: finally:
if consumer: if consumer:
consumer.close() consumer.close()
print("Consumer closed successfully.");
# ANCHOR_END: unsubscribe # ANCHOR_END: unsubscribe
if __name__ == "__main__": if __name__ == "__main__":
@ -162,10 +166,7 @@ if __name__ == "__main__":
subscribe(consumer) subscribe(consumer)
seek_offset(consumer) seek_offset(consumer)
commit_offset(consumer) commit_offset(consumer)
consumer.unsubscribe()
print("Consumer unsubscribed successfully.");
except Exception as err: except Exception as err:
print(f"Failed to stmt consumer. ErrMessage:{err}") print(f"Failed to execute consumer example, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
finally: finally:
consumer.unsubscribe() unsubscribe(consumer);

View File

@ -1,18 +1,26 @@
#!/usr/bin/python3 #!/usr/bin/python3
import taosws import taosws
topic = "topic_meters" db = "power"
topic = "topic_meters"
user = "root"
password = "taosdata"
host = "localhost"
port = 6041
groupId = "group1"
clientId = "1"
tdConnWsScheme = "ws"
autoOffsetReset = "latest"
autoCommitState = "true"
autoCommitIntv = "1000"
def prepareMeta(): def prepareMeta():
conn = None conn = None
try: try:
conn = taosws.connect(user="root", conn = taosws.connect(user=user, password=password, host=host, port=port)
password="taosdata",
host="localhost",
port=6041)
db = "power"
# create database # create database
rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}")
assert rowsAffected == 0 assert rowsAffected == 0
@ -51,7 +59,7 @@ def prepareMeta():
print(f"Inserted into {affectedRows} rows to power.meters successfully.") print(f"Inserted into {affectedRows} rows to power.meters successfully.")
except Exception as err: except Exception as err:
print(f"Failed to prepareMeta ErrMessage:{err}") print(f"Failed to prepareMeta, host: {host}:{port}, db: {db}, topic: {topic}, ErrMessage:{err}.")
raise err raise err
finally: finally:
if conn: if conn:
@ -59,26 +67,22 @@ def prepareMeta():
# ANCHOR: create_consumer # ANCHOR: create_consumer
def create_consumer(): def create_consumer():
host = "localhost"
port = 6041
groupId = "group1"
clientId = "1"
try: try:
consumer = taosws.Consumer(conf={ consumer = taosws.Consumer(conf={
"td.connect.websocket.scheme": "ws", "td.connect.websocket.scheme": tdConnWsScheme,
"group.id": groupId, "group.id": groupId,
"client.id": clientId, "client.id": clientId,
"auto.offset.reset": "latest", "auto.offset.reset": autoOffsetReset,
"td.connect.ip": host, "td.connect.ip": host,
"td.connect.port": port, "td.connect.port": port,
"enable.auto.commit": "true", "enable.auto.commit": autoCommitState,
"auto.commit.interval.ms": "1000", "auto.commit.interval.ms": autoCommitIntv,
}) })
print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}"); print(f"Create consumer successfully, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}.");
return consumer; return consumer;
except Exception as err: except Exception as err:
print(f"Failed to create websocket consumer, host: {host}:{port} ; ErrMessage:{err}"); print(f"Failed to create websocket consumer, host: {host}:{port}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.");
raise err raise err
@ -95,10 +99,10 @@ def seek_offset(consumer):
print( print(
f"vg_id: {assign.vg_id()}, offset: {assign.offset()}, begin: {assign.begin()}, end: {assign.end()}") f"vg_id: {assign.vg_id()}, offset: {assign.offset()}, begin: {assign.begin()}, end: {assign.end()}")
consumer.seek(topic, assign.vg_id(), assign.begin()) consumer.seek(topic, assign.vg_id(), assign.begin())
print("Assignment seek to beginning successfully"); print("Assignment seek to beginning successfully.")
except Exception as err: except Exception as err:
print(f"Seek example failed; ErrMessage:{err}") print(f"Failed to seek offset, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
# ANCHOR_END: assignment # ANCHOR_END: assignment
@ -116,7 +120,7 @@ def subscribe(consumer):
print(f"data: {row}") print(f"data: {row}")
except Exception as err: except Exception as err:
print(f"Failed to poll data, ErrMessage:{err}") print(f"Failed to poll data, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
@ -134,10 +138,10 @@ def commit_offset(consumer):
# after processing the data, commit the offset manually # after processing the data, commit the offset manually
consumer.commit(records) consumer.commit(records)
print("Commit offset manually successfully."); print("Commit offset manually successfully.")
except Exception as err: except Exception as err:
print(f"Failed to poll data, ErrMessage:{err}") print(f"Failed to commit offset, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
raise err raise err
@ -150,10 +154,11 @@ def unsubscribe(consumer):
consumer.unsubscribe() consumer.unsubscribe()
print("Consumer unsubscribed successfully."); print("Consumer unsubscribed successfully.");
except Exception as err: except Exception as err:
print(f"Failed to unsubscribe consumer. ErrMessage:{err}") print(f"Failed to unsubscribe consumer. topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
finally: finally:
if consumer: if consumer:
consumer.close() consumer.close()
print("Consumer closed successfully.");
# ANCHOR_END: unsubscribe # ANCHOR_END: unsubscribe
@ -166,6 +171,6 @@ if __name__ == "__main__":
seek_offset(consumer) seek_offset(consumer)
commit_offset(consumer) commit_offset(consumer)
except Exception as err: except Exception as err:
print(f"Failed to stmt consumer. ErrorMessage:{err}") print(f"Failed to execute consumer example, topic: {topic}, groupId: {groupId}, clientId: {clientId}, ErrMessage:{err}.")
finally: finally:
unsubscribe(consumer); unsubscribe(consumer)

View File

@ -10,7 +10,7 @@ async fn main() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
Err(err) => { Err(err) => {
eprintln!("Failed to connect to {}; ErrMessage: {}", dsn, err); eprintln!("Failed to connect to {}, ErrMessage: {}", dsn, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -13,7 +13,7 @@ async fn main() -> anyhow::Result<()> {
]).await { ]).await {
Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows), Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to create database power; ErrMessage: {}", err); eprintln!("Failed to create database power, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -25,7 +25,7 @@ async fn main() -> anyhow::Result<()> {
]).await { ]).await {
Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows), Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to create stable power.meters; ErrMessage: {}", err); eprintln!("Failed to create stable power.meters, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -9,22 +9,22 @@ async fn main() -> anyhow::Result<()> {
// ANCHOR: insert_data // ANCHOR: insert_data
match taos.exec(r#"INSERT INTO let insert_sql = r#"INSERT INTO
power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') power.d1001 USING power.meters TAGS(2,'California.SanFrancisco')
VALUES VALUES
(NOW + 1a, 10.30000, 219, 0.31000) (NOW + 1a, 10.30000, 219, 0.31000)
(NOW + 2a, 12.60000, 218, 0.33000) (NOW + 2a, 12.60000, 218, 0.33000)
(NOW + 3a, 12.30000, 221, 0.31000) (NOW + 3a, 12.30000, 221, 0.31000)
power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco')
VALUES VALUES
(NOW + 1a, 10.30000, 218, 0.25000) "#).await{ (NOW + 1a, 10.30000, 218, 0.25000) "#;
match taos.exec(insert_sql).await{
Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to insert data to power.meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to insert data to power.meters, sql: {}, ErrMessage: {}", insert_sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
// ANCHOR_END: insert_data // ANCHOR_END: insert_data
Ok(()) Ok(())

View File

@ -11,7 +11,8 @@ async fn main() -> anyhow::Result<()> {
// ANCHOR: query_data // ANCHOR: query_data
// query data, make sure the database and table are created before // query data, make sure the database and table are created before
match taos.query("SELECT ts, current, location FROM power.meters limit 100").await{ let sql = "SELECT ts, current, location FROM power.meters limit 100";
match taos.query(sql).await{
Ok(mut result) => { Ok(mut result) => {
for field in result.fields() { for field in result.fields() {
println!("got field: {}", field.name()); println!("got field: {}", field.name());
@ -30,7 +31,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to query data from power.meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to query data from power.meters, sql: {}, ErrMessage: {}", sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -51,6 +52,7 @@ async fn main() -> anyhow::Result<()> {
location: String, location: String,
} }
let sql = "SELECT ts, current, location FROM power.meters limit 100";
match taos.query("SELECT ts, current, location FROM power.meters limit 100").await { match taos.query("SELECT ts, current, location FROM power.meters limit 100").await {
Ok(mut query) => { Ok(mut query) => {
match query.deserialize::<Record>().try_collect::<Vec<_>>().await { match query.deserialize::<Record>().try_collect::<Vec<_>>().await {
@ -64,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to query data from power.meters, url: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to query data from power.meters, sql: {}, ErrMessage: {}", sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -92,7 +94,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to execute sql with reqId: {}, dsn: {}; ErrMessage: {}", req_id, dsn, err); eprintln!("Failed to execute sql with reqId: {}, ErrMessage: {}", req_id, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -43,7 +43,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -65,7 +65,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -96,7 +96,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -20,7 +20,13 @@ async fn main() -> anyhow::Result<()> {
let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())]; let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())];
// set table name and tags for the prepared statement. // set table name and tags for the prepared statement.
stmt.set_tbname_tags(&table_name, &tags).await?; match stmt.set_tbname_tags(&table_name, &tags).await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to set table name and tags, table_name:{}, tags:{:?}, ErrMessage: {}", table_name, tags, err);
return Err(err.into());
}
}
for j in 0..NUM_ROWS { for j in 0..NUM_ROWS {
let values = vec![ let values = vec![
ColumnView::from_millis_timestamp(vec![1648432611249 + j as i64]), ColumnView::from_millis_timestamp(vec![1648432611249 + j as i64]),
@ -29,17 +35,29 @@ async fn main() -> anyhow::Result<()> {
ColumnView::from_floats(vec![0.31 + j as f32]), ColumnView::from_floats(vec![0.31 + j as f32]),
]; ];
// bind values to the prepared statement. // bind values to the prepared statement.
stmt.bind(&values).await?; match stmt.bind(&values).await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to bind values, values:{:?}, ErrMessage: {}", values, err);
return Err(err.into());
}
}
} }
stmt.add_batch().await?; match stmt.add_batch().await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to add batch, ErrMessage: {}", err);
return Err(err.into());
}
}
} }
// execute. // execute.
match stmt.execute().await{ match stmt.execute().await{
Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to insert to table meters using stmt, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to insert to table meters using stmt, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -3,6 +3,8 @@ use std::str::FromStr;
use chrono::Local; use chrono::Local;
use chrono::DateTime; use chrono::DateTime;
use taos::*; use taos::*;
use std::thread;
use tokio::runtime::Runtime;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
@ -53,17 +55,38 @@ async fn main() -> anyhow::Result<()> {
consumer consumer
} }
Err(err) => { Err(err) => {
eprintln!("Failed to create consumer, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to create native consumer, dsn: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", dsn, group_id, client_id, err);
return Err(err.into()); return Err(err.into());
} }
}; };
// ANCHOR_END: create_consumer_ac // ANCHOR_END: create_consumer_ac
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let taos_insert = TaosBuilder::from_dsn(&dsn).unwrap().build().await.unwrap();
for i in 0..50 {
let insert_sql = format!(r#"INSERT INTO
power.d1001 USING power.meters TAGS(2,'California.SanFrancisco')
VALUES
(NOW, 10.30000, {}, 0.31000)"#, i);
if let Err(e) = taos_insert.exec(insert_sql).await {
eprintln!("Failed to execute insert: {:?}", e);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
});
}).join().unwrap();
// ANCHOR: consume // ANCHOR: consume
match consumer.subscribe(["topic_meters"]).await{ let topic = "topic_meters";
match consumer.subscribe([topic]).await{
Ok(_) => println!("Subscribe topics successfully."), Ok(_) => println!("Subscribe topics successfully."),
Err(err) => { Err(err) => {
eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to subscribe topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -94,13 +117,14 @@ async fn main() -> anyhow::Result<()> {
if let Some(data) = message.into_data() { if let Some(data) = message.into_data() {
while let Some(block) = data.fetch_raw_block().await? { while let Some(block) = data.fetch_raw_block().await? {
let records: Vec<Record> = block.deserialize().try_collect()?; let records: Vec<Record> = block.deserialize().try_collect()?;
// Add your data processing logic here
println!("** read {} records: {:#?}\n", records.len(), records); println!("** read {} records: {:#?}\n", records.len(), records);
} }
} }
Ok(()) Ok(())
}) })
.await.map_err(|e| { .await.map_err(|e| {
eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); eprintln!("Failed to poll data, topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, e);
e e
})?; })?;
@ -110,14 +134,14 @@ async fn main() -> anyhow::Result<()> {
consumer consumer
.stream() .stream()
.try_for_each(|(offset, message)| async { .try_for_each(|(offset, message)| async {
let topic = offset.topic();
// the vgroup id, like partition id in kafka. // the vgroup id, like partition id in kafka.
let vgroup_id = offset.vgroup_id(); let vgroup_id = offset.vgroup_id();
println!("* in vgroup id {vgroup_id} of topic {topic}\n"); println!("* in vgroup id {} of topic {}\n", vgroup_id, topic);
if let Some(data) = message.into_data() { if let Some(data) = message.into_data() {
while let Some(block) = data.fetch_raw_block().await? { while let Some(block) = data.fetch_raw_block().await? {
let records: Vec<Record> = block.deserialize().try_collect()?; let records: Vec<Record> = block.deserialize().try_collect()?;
// Add your data processing logic here
println!("** read {} records: {:#?}\n", records.len(), records); println!("** read {} records: {:#?}\n", records.len(), records);
} }
} }
@ -125,22 +149,30 @@ async fn main() -> anyhow::Result<()> {
match consumer.commit(offset).await{ match consumer.commit(offset).await{
Ok(_) => println!("Commit offset manually successfully."), Ok(_) => println!("Commit offset manually successfully."),
Err(err) => { Err(err) => {
eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to commit offset manually, topic: {}, groupId: {}, clientId: {}, vGroupId: {}, ErrMessage: {:?}",
topic, group_id, client_id, vgroup_id, err);
return Err(err.into()); return Err(err.into());
} }
} }
Ok(()) Ok(())
}) })
.await.map_err(|e| { .await.map_err(|e| {
eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); eprintln!("Failed to poll data, topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, e);
e e
})?; })?;
// ANCHOR_END: consumer_commit_manually // ANCHOR_END: consumer_commit_manually
// ANCHOR: seek_offset // ANCHOR: seek_offset
let assignments = consumer.assignments().await.unwrap(); let assignments = match consumer.assignments().await{
println!("Now assignments: {:?}", assignments); Some(assignments) => assignments,
None => {
let error_message = format!("Failed to get assignments. topic: {}, groupId: {}, clientId: {}", topic, group_id, client_id);
eprintln!("{}", error_message);
return Err(anyhow::anyhow!(error_message));
}
};
println!("assignments: {:?}", assignments);
// seek offset // seek offset
for topic_vec_assignment in assignments { for topic_vec_assignment in assignments {
@ -152,7 +184,7 @@ async fn main() -> anyhow::Result<()> {
let begin = assignment.begin(); let begin = assignment.begin();
let end = assignment.end(); let end = assignment.end();
println!( println!(
"topic: {}, vgroup_id: {}, current offset: {} begin {}, end: {}", "topic: {}, vgroup_id: {}, current offset: {}, begin {}, end: {}",
topic, topic,
vgroup_id, vgroup_id,
current, current,
@ -163,18 +195,26 @@ async fn main() -> anyhow::Result<()> {
match consumer.offset_seek(topic, vgroup_id, begin).await{ match consumer.offset_seek(topic, vgroup_id, begin).await{
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
eprintln!("Seek example failed; ErrMessage: {}", err); eprintln!("Failed to seek offset, topic: {}, groupId: {}, clientId: {}, vGroupId: {}, begin: {}, ErrMessage: {:?}",
topic, group_id, client_id, vgroup_id, begin, err);
return Err(err.into()); return Err(err.into());
} }
} }
} }
let topic_assignment = consumer.topic_assignment(topic).await; let topic_assignment = consumer.topic_assignment(topic).await;
println!("Topic assignment: {:?}", topic_assignment); println!("topic assignment: {:?}", topic_assignment);
} }
println!("Assignment seek to beginning successfully."); println!("Assignment seek to beginning successfully.");
// after seek offset // after seek offset
let assignments = consumer.assignments().await.unwrap(); let assignments = match consumer.assignments().await{
Some(assignments) => assignments,
None => {
let error_message = format!("Failed to get assignments. topic: {}, groupId: {}, clientId: {}", topic, group_id, client_id);
eprintln!("{}", error_message);
return Err(anyhow::anyhow!(error_message));
}
};
println!("After seek offset assignments: {:?}", assignments); println!("After seek offset assignments: {:?}", assignments);
// ANCHOR_END: seek_offset // ANCHOR_END: seek_offset

View File

@ -10,7 +10,7 @@ async fn main() -> anyhow::Result<()> {
Ok(()) Ok(())
} }
Err(err) => { Err(err) => {
eprintln!("Failed to connect to {}; ErrMessage: {}", dsn, err); eprintln!("Failed to connect to {}, ErrMessage: {}", dsn, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -13,7 +13,7 @@ async fn main() -> anyhow::Result<()> {
]).await { ]).await {
Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows), Ok(afffected_rows) => println!("Create database power successfully, rowsAffected: {}", afffected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to create database power; ErrMessage: {}", err); eprintln!("Failed to create database power, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -25,7 +25,7 @@ async fn main() -> anyhow::Result<()> {
]).await { ]).await {
Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows), Ok(afffected_rows) => println!("Create stable power.meters successfully, rowsAffected: {}", afffected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to create stable power.meters; ErrMessage: {}", err); eprintln!("Failed to create stable power.meters, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -9,22 +9,22 @@ async fn main() -> anyhow::Result<()> {
// ANCHOR: insert_data // ANCHOR: insert_data
match taos.exec(r#"INSERT INTO let insert_sql = r#"INSERT INTO
power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') power.d1001 USING power.meters TAGS(2,'California.SanFrancisco')
VALUES VALUES
(NOW + 1a, 10.30000, 219, 0.31000) (NOW + 1a, 10.30000, 219, 0.31000)
(NOW + 2a, 12.60000, 218, 0.33000) (NOW + 2a, 12.60000, 218, 0.33000)
(NOW + 3a, 12.30000, 221, 0.31000) (NOW + 3a, 12.30000, 221, 0.31000)
power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco')
VALUES VALUES
(NOW + 1a, 10.30000, 218, 0.25000) "#).await{ (NOW + 1a, 10.30000, 218, 0.25000) "#;
match taos.exec(insert_sql).await{
Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to insert data to power.meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to insert data to power.meters, sql: {}, ErrMessage: {}", insert_sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
// ANCHOR_END: insert_data // ANCHOR_END: insert_data
Ok(()) Ok(())

View File

@ -11,7 +11,8 @@ async fn main() -> anyhow::Result<()> {
// ANCHOR: query_data // ANCHOR: query_data
// query data, make sure the database and table are created before // query data, make sure the database and table are created before
match taos.query("SELECT ts, current, location FROM power.meters limit 100").await{ let sql = "SELECT ts, current, location FROM power.meters limit 100";
match taos.query(sql).await{
Ok(mut result) => { Ok(mut result) => {
for field in result.fields() { for field in result.fields() {
println!("got field: {}", field.name()); println!("got field: {}", field.name());
@ -30,7 +31,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to query data from power.meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to query data from power.meters, sql: {}, ErrMessage: {}", sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -51,6 +52,7 @@ async fn main() -> anyhow::Result<()> {
location: String, location: String,
} }
let sql = "SELECT ts, current, location FROM power.meters limit 100";
match taos.query("SELECT ts, current, location FROM power.meters limit 100").await { match taos.query("SELECT ts, current, location FROM power.meters limit 100").await {
Ok(mut query) => { Ok(mut query) => {
match query.deserialize::<Record>().try_collect::<Vec<_>>().await { match query.deserialize::<Record>().try_collect::<Vec<_>>().await {
@ -64,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to query data from power.meters, url: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to query data from power.meters, sql: {}, ErrMessage: {}", sql, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -92,7 +94,7 @@ async fn main() -> anyhow::Result<()> {
} }
} }
Err(err) => { Err(err) => {
eprintln!("Failed to execute sql with reqId: {}, dsn: {}; ErrMessage: {}", req_id, dsn, err); eprintln!("Failed to execute sql with reqId: {}, ErrMessage: {}", req_id, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -17,6 +17,15 @@ async fn main() -> anyhow::Result<()> {
let client = TaosBuilder::from_dsn(dsn)?.build().await?; let client = TaosBuilder::from_dsn(dsn)?.build().await?;
let db = "power";
client
.exec(format!("create database if not exists {db}"))
.await?;
// should specify database before insert
client.exec(format!("use {db}")).await?;
// SchemalessProtocol::Line // SchemalessProtocol::Line
let data = [ let data = [
"meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639", "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639",
@ -34,7 +43,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -56,7 +65,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -87,7 +96,7 @@ async fn main() -> anyhow::Result<()> {
match client.put(&sml_data).await{ match client.put(&sml_data).await{
Ok(_) => {}, Ok(_) => {},
Err(err) => { Err(err) => {
eprintln!("Failed to insert data with schemaless, host: {}; ErrMessage: {}", host, err); eprintln!("Failed to insert data with schemaless, data:{:?}, ErrMessage: {}", data, err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -20,7 +20,13 @@ async fn main() -> anyhow::Result<()> {
let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())]; let tags = vec![Value::Int(i as i32), Value::VarChar(format!("location_{}", i).into())];
// set table name and tags for the prepared statement. // set table name and tags for the prepared statement.
stmt.set_tbname_tags(&table_name, &tags).await?; match stmt.set_tbname_tags(&table_name, &tags).await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to set table name and tags, table_name:{}, tags:{:?}, ErrMessage: {}", table_name, tags, err);
return Err(err.into());
}
}
for j in 0..NUM_ROWS { for j in 0..NUM_ROWS {
let values = vec![ let values = vec![
ColumnView::from_millis_timestamp(vec![1648432611249 + j as i64]), ColumnView::from_millis_timestamp(vec![1648432611249 + j as i64]),
@ -29,17 +35,29 @@ async fn main() -> anyhow::Result<()> {
ColumnView::from_floats(vec![0.31 + j as f32]), ColumnView::from_floats(vec![0.31 + j as f32]),
]; ];
// bind values to the prepared statement. // bind values to the prepared statement.
stmt.bind(&values).await?; match stmt.bind(&values).await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to bind values, values:{:?}, ErrMessage: {}", values, err);
return Err(err.into());
}
}
} }
stmt.add_batch().await?; match stmt.add_batch().await{
Ok(_) => {},
Err(err) => {
eprintln!("Failed to add batch, ErrMessage: {}", err);
return Err(err.into());
}
}
} }
// execute. // execute.
match stmt.execute().await{ match stmt.execute().await{
Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows), Ok(affected_rows) => println!("Successfully inserted {} rows to power.meters.", affected_rows),
Err(err) => { Err(err) => {
eprintln!("Failed to insert to table meters using stmt, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to insert to table meters using stmt, ErrMessage: {}", err);
return Err(err.into()); return Err(err.into());
} }
} }

View File

@ -3,6 +3,8 @@ use std::str::FromStr;
use chrono::Local; use chrono::Local;
use chrono::DateTime; use chrono::DateTime;
use taos::*; use taos::*;
use std::thread;
use tokio::runtime::Runtime;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
@ -53,17 +55,38 @@ async fn main() -> anyhow::Result<()> {
consumer consumer
} }
Err(err) => { Err(err) => {
eprintln!("Failed to create consumer, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to create websocket consumer, dsn: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", dsn, group_id, client_id, err);
return Err(err.into()); return Err(err.into());
} }
}; };
// ANCHOR_END: create_consumer_ac // ANCHOR_END: create_consumer_ac
thread::spawn(move || {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let taos_insert = TaosBuilder::from_dsn(&dsn).unwrap().build().await.unwrap();
for i in 0..50 {
let insert_sql = format!(r#"INSERT INTO
power.d1001 USING power.meters TAGS(2,'California.SanFrancisco')
VALUES
(NOW, 10.30000, {}, 0.31000)"#, i);
if let Err(e) = taos_insert.exec(insert_sql).await {
eprintln!("Failed to execute insert: {:?}", e);
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
});
}).join().unwrap();
// ANCHOR: consume // ANCHOR: consume
match consumer.subscribe(["topic_meters"]).await{ let topic = "topic_meters";
match consumer.subscribe([topic]).await{
Ok(_) => println!("Subscribe topics successfully."), Ok(_) => println!("Subscribe topics successfully."),
Err(err) => { Err(err) => {
eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to subscribe topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -94,13 +117,14 @@ async fn main() -> anyhow::Result<()> {
if let Some(data) = message.into_data() { if let Some(data) = message.into_data() {
while let Some(block) = data.fetch_raw_block().await? { while let Some(block) = data.fetch_raw_block().await? {
let records: Vec<Record> = block.deserialize().try_collect()?; let records: Vec<Record> = block.deserialize().try_collect()?;
// Add your data processing logic here
println!("** read {} records: {:#?}\n", records.len(), records); println!("** read {} records: {:#?}\n", records.len(), records);
} }
} }
Ok(()) Ok(())
}) })
.await.map_err(|e| { .await.map_err(|e| {
eprintln!("Failed to poll data; ErrMessage: {:?}", e); eprintln!("Failed to poll data, topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, e);
e e
})?; })?;
@ -110,14 +134,14 @@ async fn main() -> anyhow::Result<()> {
consumer consumer
.stream() .stream()
.try_for_each(|(offset, message)| async { .try_for_each(|(offset, message)| async {
let topic = offset.topic();
// the vgroup id, like partition id in kafka. // the vgroup id, like partition id in kafka.
let vgroup_id = offset.vgroup_id(); let vgroup_id = offset.vgroup_id();
println!("* in vgroup id {vgroup_id} of topic {topic}\n"); println!("* in vgroup id {} of topic {}\n", vgroup_id, topic);
if let Some(data) = message.into_data() { if let Some(data) = message.into_data() {
while let Some(block) = data.fetch_raw_block().await? { while let Some(block) = data.fetch_raw_block().await? {
let records: Vec<Record> = block.deserialize().try_collect()?; let records: Vec<Record> = block.deserialize().try_collect()?;
// Add your data processing logic here
println!("** read {} records: {:#?}\n", records.len(), records); println!("** read {} records: {:#?}\n", records.len(), records);
} }
} }
@ -125,21 +149,29 @@ async fn main() -> anyhow::Result<()> {
match consumer.commit(offset).await{ match consumer.commit(offset).await{
Ok(_) => println!("Commit offset manually successfully."), Ok(_) => println!("Commit offset manually successfully."),
Err(err) => { Err(err) => {
eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); eprintln!("Failed to commit offset manually, topic: {}, groupId: {}, clientId: {}, vGroupId: {}, ErrMessage: {:?}",
topic, group_id, client_id, vgroup_id, err);
return Err(err.into()); return Err(err.into());
} }
} }
Ok(()) Ok(())
}) })
.await.map_err(|e| { .await.map_err(|e| {
eprintln!("Failed to execute consumer functions. ErrMessage: {:?}", e); eprintln!("Failed to poll data, topic: {}, groupId: {}, clientId: {}, ErrMessage: {:?}", topic, group_id, client_id, e);
e e
})?; })?;
// ANCHOR_END: consumer_commit_manually // ANCHOR_END: consumer_commit_manually
// ANCHOR: seek_offset // ANCHOR: seek_offset
let assignments = consumer.assignments().await.unwrap(); let assignments = match consumer.assignments().await{
Some(assignments) => assignments,
None => {
let error_message = format!("Failed to get assignments. topic: {}, groupId: {}, clientId: {}", topic, group_id, client_id);
eprintln!("{}", error_message);
return Err(anyhow::anyhow!(error_message));
}
};
println!("assignments: {:?}", assignments); println!("assignments: {:?}", assignments);
// seek offset // seek offset
@ -152,7 +184,7 @@ async fn main() -> anyhow::Result<()> {
let begin = assignment.begin(); let begin = assignment.begin();
let end = assignment.end(); let end = assignment.end();
println!( println!(
"topic: {}, vgroup_id: {}, current offset: {} begin {}, end: {}", "topic: {}, vgroup_id: {}, current offset: {}, begin {}, end: {}",
topic, topic,
vgroup_id, vgroup_id,
current, current,
@ -163,7 +195,8 @@ async fn main() -> anyhow::Result<()> {
match consumer.offset_seek(topic, vgroup_id, begin).await{ match consumer.offset_seek(topic, vgroup_id, begin).await{
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
eprintln!("seek example failed; ErrMessage: {}", err); eprintln!("Failed to seek offset, topic: {}, groupId: {}, clientId: {}, vGroupId: {}, begin: {}, ErrMessage: {:?}",
topic, group_id, client_id, vgroup_id, begin, err);
return Err(err.into()); return Err(err.into());
} }
} }
@ -174,7 +207,14 @@ async fn main() -> anyhow::Result<()> {
} }
println!("Assignment seek to beginning successfully."); println!("Assignment seek to beginning successfully.");
// after seek offset // after seek offset
let assignments = consumer.assignments().await.unwrap(); let assignments = match consumer.assignments().await{
Some(assignments) => assignments,
None => {
let error_message = format!("Failed to get assignments. topic: {}, groupId: {}, clientId: {}", topic, group_id, client_id);
eprintln!("{}", error_message);
return Err(anyhow::anyhow!(error_message));
}
};
println!("After seek offset assignments: {:?}", assignments); println!("After seek offset assignments: {:?}", assignments);
// ANCHOR_END: seek_offset // ANCHOR_END: seek_offset

View File

@ -24,11 +24,11 @@ TDengine 经过特别优化,以适应时间序列数据的独特需求,引
1. 写入数据TDengine 支持多种数据写入方式。首先,它完全兼容 SQL允许用户使用标准的 SQL 语法进行数据写入。而且 TDengine 还支持无模式Schemaless写入包括流行的 InfluxDB Line 协议、OpenTSDB 的 Telnet 和 JSON 协议这些协议的加入使得数据的导入变得更加灵活和高效。更进一步TDengine 与众多第三方工具实现了无缝集成,例如 Telegraf、Prometheus、EMQX、StatsD、collectd 和 HiveMQ 等。在 TDengine Enterprise 中, 还提供了 MQTT、OPC-UA、OPC-DA、PI、Wonderware、Kafka、InfluxDB、OpenTSDB、MySQL、Oracle 和 SQL Server 等连接器。这些工具通过简单的配置,无需一行代码,就可以将来自各种数据源的数据源源不断的写入数据库,极大地简化了数据收集和存储的过程。 1. 写入数据TDengine 支持多种数据写入方式。首先,它完全兼容 SQL允许用户使用标准的 SQL 语法进行数据写入。而且 TDengine 还支持无模式Schemaless写入包括流行的 InfluxDB Line 协议、OpenTSDB 的 Telnet 和 JSON 协议这些协议的加入使得数据的导入变得更加灵活和高效。更进一步TDengine 与众多第三方工具实现了无缝集成,例如 Telegraf、Prometheus、EMQX、StatsD、collectd 和 HiveMQ 等。在 TDengine Enterprise 中, 还提供了 MQTT、OPC-UA、OPC-DA、PI、Wonderware、Kafka、InfluxDB、OpenTSDB、MySQL、Oracle 和 SQL Server 等连接器。这些工具通过简单的配置,无需一行代码,就可以将来自各种数据源的数据源源不断的写入数据库,极大地简化了数据收集和存储的过程。
2. 查询数据TDengine 提供标准的 SQL 查询语法并针对时序数据和业务的特点优化和新增了许多语法和功能例如降采样、插值、累计求和、时间加权平均、状态窗口、时间窗口、会话窗口、滑动窗口等。TDengine 还支持用户自定义函数UDF 2. 查询数据TDengine 提供标准的 SQL 查询语法并针对时序数据和业务的特点优化和新增了许多语法和功能例如降采样、插值、累计求和、时间加权平均、状态窗口、时间窗口、会话窗口、滑动窗口等。TDengine 还支持用户自定义函数UDF
3. 缓存TDengine 使用时间驱动缓存管理策略First-In-First-OutFIFO将最近到达的当前状态数据保存在缓存中这样便于获取任何监测对象的实时状态而无需使用 Redis 等其他缓存工具,简化系统架构和运营成本。 3. 缓存TDengine 使用时间驱动缓存管理策略First-In-First-OutFIFO将最近到达的当前状态数据保存在缓存中这样便于获取任何监测对象的实时状态而无需使用 Redis 等其他缓存工具,简化系统架构和运营成本。
4. 流式计算TDengine 流式计算引擎提供了实时处理写入的数据流的能力,不仅支持连续查询,还支持事件驱动的流式计算。它提供了替代复杂流处理系统的轻量级解决方案,并能够在高吞吐的数据写入的情况下,提供毫秒级的计算结果延迟 4. 流式计算TDengine 流式计算引擎提供了实时处理写入的数据流的能力,不仅支持连续查询,还支持事件驱动的流式计算。它提供了替代复杂流处理系统的轻量级解决方案,并能够在高吞吐的数据写入的情况下,提供毫秒级的计算结果延迟
5. 数据订阅TDengine 提供了类似 Kafka 的数据订阅功能。但用户可以通过 SQL 来灵活控制订阅的数据内容,并使用 Kafka 相同的 API 来订阅一张表、一组表、全部列或部分列、甚至整个数据库的数据。TDengine 可以替代需要集成消息队列产品的场景, 从而简化系统设计的复杂度,降低运营维护成本。 5. 数据订阅TDengine 提供了类似 Kafka 的数据订阅功能。但用户可以通过 SQL 来灵活控制订阅的数据内容,并使用 Kafka 相同的 API 来订阅一张表、一组表、全部列或部分列、甚至整个数据库的数据。TDengine 可以替代需要集成消息队列产品的场景, 从而简化系统设计的复杂度,降低运营维护成本。
@ -38,13 +38,13 @@ TDengine 经过特别优化,以适应时间序列数据的独特需求,引
8. 数据迁移TDengine 提供了多种便捷的数据导入导出功能包括脚本文件导入导出、数据文件导入导出、taosdump 工具导入导出等。 8. 数据迁移TDengine 提供了多种便捷的数据导入导出功能包括脚本文件导入导出、数据文件导入导出、taosdump 工具导入导出等。
9. 编程连接器TDengine 提供不同语言的连接器,包括 C/C++、Java、Go、Node.js、Rust、Python、C#、R、PHP 等。这些连接器大多都支持原生连接和 WebSocket 两种连接方式。TDengine 也提供 REST 接口,任何语言的应用程序可以直接通过 HTTP 请求访问数据库。 9. 编程连接器TDengine 提供不同语言的连接器,包括 C/C++、Java、Go、Node.js、Rust、Python、C#、R、PHP 等。这些连接器大多都支持原生连接和 WebSocket 两种连接方式。TDengine 也提供 RESTful 接口,任何语言的应用程序可以直接通过 HTTP 请求访问数据库。
10. 数据安全TDengine 提供了丰富的用户管理和权限管理功能以控制不同用户对数据库和表的访问权限,提供了 IP 白名单功能以控制不同帐号只能从特定的服务器接入集群。TDengine 支持系统管理员对不同数据库按需加密,数据加密后对读写完全透明且对性能的影响很小。还提供了审计日志功能以记录系统中的敏感操作。 10. 数据安全TDengine 提供了丰富的用户管理和权限管理功能以控制不同用户对数据库和表的访问权限,提供了 IP 白名单功能以控制不同帐号只能从特定的服务器接入集群。TDengine 支持系统管理员对不同数据库按需加密,数据加密后对读写完全透明且对性能的影响很小。还提供了审计日志功能以记录系统中的敏感操作。
11. 常用工具TDengine 还提供了交互式命令行程序CLI便于管理集群、检查系统状态、做即时查询。压力测试工具 taosBenchmark用于测试 TDengine 的性能。TDengine 还提供了图形化管理界面,简化了操作和管理过程。 11. 常用工具TDengine 还提供了交互式命令行程序CLI便于管理集群、检查系统状态、做即时查询。压力测试工具 taosBenchmark用于测试 TDengine 的性能。TDengine 还提供了图形化管理界面,简化了操作和管理过程。
12. 零代码数据接入TDengine 企业版提供了丰富的数据接入功能依托强大的数据接入平台无需一行代码只需要做简单的配置即可实现多种数据源的数据接入目前已经支持的数据源包括OPC UA, OPC DA, Pi, MQTT, Kafka, InfluxDB, OpenTSDB, MySql, SQL Server, Oracle, Wonderware Historian, MongoDB。 12. 零代码数据接入TDengine 企业版提供了丰富的数据接入功能依托强大的数据接入平台无需一行代码只需要做简单的配置即可实现多种数据源的数据接入目前已经支持的数据源包括OPC-UA、OPC-DA、PI、MQTT、Kafka、InfluxDB、OpenTSDB、MySQL、SQL Server、Oracle、Wonderware Historian、MongoDB。
## TDengine 与典型时序数据库的区别 ## TDengine 与典型时序数据库的区别
@ -58,16 +58,14 @@ TDengine 经过特别优化,以适应时间序列数据的独特需求,引
4. 强大的分析能力TDengine 不仅支持标准 SQL 查询,还为时序数据特有的分析提供了 SQL 扩展。通过超级表、存储计算分离、分区分片、预计算、UDF 等先进技术TDengine 展现出强大的数据分析能力。 4. 强大的分析能力TDengine 不仅支持标准 SQL 查询,还为时序数据特有的分析提供了 SQL 扩展。通过超级表、存储计算分离、分区分片、预计算、UDF 等先进技术TDengine 展现出强大的数据分析能力。
5. 简单易用TDengine 安装无依赖,集群部署仅需几秒即可完成。它提供了 REST ful接口和多种编程语言的连接器与众多第三方工具无缝集成。此外命令行程序和丰富的运维工具也极大地方便了用户的管理和即时查询需求。 5. 简单易用TDengine 安装无依赖,集群部署仅需几秒即可完成。它提供了 RESTful 接口和多种编程语言的连接器,与众多第三方工具无缝集成。此外,命令行程序和丰富的运维工具也极大地方便了用户的管理和即时查询需求。
6. 核心开源TDengine 的核心代码包括集群功能均在开源协议下公开发布。它在GitHub 网站全球趋势排行榜上多次位居榜首显示出其受欢迎程度。同时TDengine 6. 核心开源TDengine 的核心代码,包括集群功能,均在开源协议下公开发布。它在 GitHub 网站全球趋势排行榜上多次位居榜首显示出其受欢迎程度。同时TDengine 拥有一个活跃的开发者社区,为技术的持续发展和创新提供了有力支持。
拥有一个活跃的开发者社区,为技术的持续发展和创新提供了有力支持。
采用 TDengine企业可以在物联网、车联网、工业互联网等典型场景中显著降低大数据平台的总拥有成本主要体现在以下几个方面 采用 TDengine企业可以在物联网、车联网、工业互联网等典型场景中显著降低大数据平台的总拥有成本主要体现在以下几个方面
1. 高性能带来的成本节约TDengine 卓越的写入、查询和存储性能意味着系统所需的计算资源和存储资源可以大幅度减少。这不仅降低了硬件成本,还减少了能源消耗和维护费用。 1. 高性能带来的成本节约TDengine 卓越的写入、查询和存储性能意味着系统所需的计算资源和存储资源可以大幅度减少。这不仅降低了硬件成本,还减少了能源消耗和维护费用。
2. 标准化与兼容性带来的成本效益:由于 TDengine 支持标准 SQL并与众多第三方软件实现了无缝集成用户可以轻松地将现有系统迁移到 TDengine 上,无须重写大量代码。这种标准化和兼容性大大降低了学习和迁移成本,缩短了项目周期。 2. 标准化与兼容性带来的成本效益:由于 TDengine 支持标准 SQL并与众多第三方软件实现了无缝集成用户可以轻松地将现有系统迁移到 TDengine 上,无须重写大量代码。这种标准化和兼容性大大降低了学习和迁移成本,缩短了项目周期。
3. 简化系统架构带来的成本降低作为一个极简的时序数据平台TDengine 集成了消息队列、缓存、流计算等必要功能,避免了额外集成众多其他组件的需要。这 3. 简化系统架构带来的成本降低作为一个极简的时序数据平台TDengine 集成了消息队列、缓存、流计算等必要功能,避免了额外集成众多其他组件的需要。这种简化的系统架构显著降低了系统的复杂度,从而减少了研发和运营成本,提高了整体运营效率。
种简化的系统架构显著降低了系统的复杂度,从而减少了研发和运营成本,提高了整体运营效率。
## 技术生态 ## 技术生态

View File

@ -146,7 +146,7 @@ Note: 从 3.0.1.7 开始,只提供 TDengine 客户端的 Windows 客户端的
</Tabs> </Tabs>
:::info :::info
下载其他组件、最新 Beta 版及之前版本的安装包,请点击[发布历史页面](../../releases/tdengine)。 下载其他组件、最新 Beta 版及之前版本的安装包,请点击[发布历史页面](https://docs.taosdata.com/releases/tdengine/)。
::: :::
:::note :::note

View File

@ -4,8 +4,6 @@ title: 集群维护
sidebar_label: 集群维护 sidebar_label: 集群维护
--- ---
## 简介
本节介绍 TDengine Enterprise 中提供的高阶集群维护手段,能够使 TDengine 集群长期运行得更健壮和高效。 本节介绍 TDengine Enterprise 中提供的高阶集群维护手段,能够使 TDengine 集群长期运行得更健壮和高效。
## 节点管理 ## 节点管理

View File

@ -4,7 +4,7 @@ sidebar_label: 双活系统
toc_max_heading_level: 4 toc_max_heading_level: 4
--- ---
## 简介 本节介绍 TDengine 双活系统的配置和使用。
1. 部分用户因为部署环境的特殊性只能部署两台服务器,同时希望实现一定的服务高可用和数据高可靠。本文主要描述基于数据复制和客户端 Failover 两项关键技术的 TDengine 双活系统的产品行为包括双活系统的架构、配置、运维等。TDengine 双活既可以用于前面所述资源受限的环境,也可用于在两套 TDengine 集群(不限资源)之间的灾备场景。双活是 TDengine Enterprise 特有功能,在 3.3.0.0 版本中第一次发布,建议使用最新版本。 1. 部分用户因为部署环境的特殊性只能部署两台服务器,同时希望实现一定的服务高可用和数据高可靠。本文主要描述基于数据复制和客户端 Failover 两项关键技术的 TDengine 双活系统的产品行为包括双活系统的架构、配置、运维等。TDengine 双活既可以用于前面所述资源受限的环境,也可用于在两套 TDengine 集群(不限资源)之间的灾备场景。双活是 TDengine Enterprise 特有功能,在 3.3.0.0 版本中第一次发布,建议使用最新版本。

View File

@ -99,7 +99,7 @@ TDengine 提供了丰富的应用程序开发接口,为了便于用户快速
- **安装前准备** - **安装前准备**
- 安装 Python。新近版本 taospy 包要求 Python 3.6.2+。早期版本 taospy 包要求 Python 3.7+。taos-ws-py 包要求 Python 3.7+。如果系统上还没有 Python 可参考 [Python BeginnersGuide](https://wiki.python.org/moin/BeginnersGuide/Download) 安装。 - 安装 Python。新近版本 taospy 包要求 Python 3.6.2+。早期版本 taospy 包要求 Python 3.7+。taos-ws-py 包要求 Python 3.7+。如果系统上还没有 Python 可参考 [Python BeginnersGuide](https://wiki.python.org/moin/BeginnersGuide/Download) 安装。
- 安装 [pip](https://pypi.org/project/pip/)。大部分情况下 Python 的安装包都自带了 pip 工具, 如果没有请参考 [pip documentation](https://pip.pypa.io/en/stable/installation/) 安装。 - 安装 [pip](https://pypi.org/project/pip/)。大部分情况下 Python 的安装包都自带了 pip 工具, 如果没有请参考 [pip documentation](https://pip.pypa.io/en/stable/installation/) 安装。
- 如果使用原生连接,还需[安装客户端驱动](../#安装客户端驱动)。客户端软件包含了 TDengine 客户端动态链接库(libtaos.so 或 taos.dll) 和 TDengine CLI。 - 如果使用原生连接,还需[安装客户端驱动](../connect/#安装客户端驱动-taosc)。客户端软件包含了 TDengine 客户端动态链接库(libtaos.so 或 taos.dll) 和 TDengine CLI。
- **使用 pip 安装** - **使用 pip 安装**
- 卸载旧版本 - 卸载旧版本
@ -399,7 +399,8 @@ C/C++ 语言连接器使用 `taos_connect()` 函数用于建立与 TDengine 数
</TabItem> </TabItem>
<TabItem label="REST API" value="rest"> <TabItem label="REST API" value="rest">
使用 REST API 方式访问 TDengine由应用程序去建立 HTTP 连接,自己控制 HTTP 连接参数。 通过 REST API 方式访问 TDengine 时,应用程序直接与 taosAdapter 建立 HTTP 连接,建议使用连接池来管理连接。
使用 REST API 的参数具体可以参考:[http-请求格式](../../reference/connector/rest-api/#http-请求格式)
</TabItem> </TabItem>
</Tabs> </Tabs>
@ -544,7 +545,7 @@ C/C++ 语言连接器使用 `taos_connect()` 函数用于建立与 TDengine 数
使用示例如下: 使用示例如下:
```java ```java
{{#include examples/JDBC/connectionPools/src/main/java/com/taosdata/example/HikariDemo.java:connection_pool}} {{#include docs/examples/java/src/main/java/com/taos/example/HikariDemo.java:connection_pool}}
``` ```
> 通过 HikariDataSource.getConnection() 获取连接后,使用完成后需要调用 close() 方法,实际上它并不会关闭连接,只是放回连接池中。 > 通过 HikariDataSource.getConnection() 获取连接后,使用完成后需要调用 close() 方法,实际上它并不会关闭连接,只是放回连接池中。
@ -555,7 +556,7 @@ C/C++ 语言连接器使用 `taos_connect()` 函数用于建立与 TDengine 数
使用示例如下: 使用示例如下:
```java ```java
{{#include examples/JDBC/connectionPools/src/main/java/com/taosdata/example/DruidDemo.java:connection_pool}} {{#include docs/examples/java/src/main/java/com/taos/example/DruidDemo.java:connection_pool}}
``` ```
> 更多 druid 使用问题请查看[官方说明](https://github.com/alibaba/druid)。 > 更多 druid 使用问题请查看[官方说明](https://github.com/alibaba/druid)。

View File

@ -27,7 +27,7 @@ REST API直接调用 `taosadapter` 提供的 REST API 接口,进行数据
<TabItem value="java" label="Java"> <TabItem value="java" label="Java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcCreatDBDemo.java:create_db_and_table}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcCreatDBDemo.java:create_db_and_table}}
``` ```
</TabItem> </TabItem>
@ -98,7 +98,7 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql/power' \
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem value="java" label="Java"> <TabItem value="java" label="Java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcInsertDataDemo.java:insert_data}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcInsertDataDemo.java:insert_data}}
``` ```
**Note** **Note**
@ -170,7 +170,7 @@ curl --location -uroot:taosdata 'http://127.0.0.1:6041/rest/sql' \
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcQueryDemo.java:query_data}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcQueryDemo.java:query_data}}
``` ```
**Note** 查询和操作关系型数据库一致,使用下标获取返回字段内容时从 1 开始,建议使用字段名称获取。 **Note** 查询和操作关系型数据库一致,使用下标获取返回字段内容时从 1 开始,建议使用字段名称获取。
@ -251,7 +251,7 @@ reqId 可用于请求链路追踪reqId 就像分布式系统中的 traceId
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/JdbcReqIdDemo.java:with_reqid}} {{#include docs/examples/java/src/main/java/com/taos/example/JdbcReqIdDemo.java:with_reqid}}
``` ```
</TabItem> </TabItem>

View File

@ -165,7 +165,7 @@ st,t1=3,t2=4,t3=t3 c1=3i64,c6="passit" 1626006833640000000
<TabItem value="java" label="Java"> <TabItem value="java" label="Java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessWsTest.java:schemaless}} {{#include docs/examples/java/src/main/java/com/taos/example/SchemalessWsTest.java:schemaless}}
``` ```
@ -216,7 +216,7 @@ writer.write(lineDemo, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO
<Tabs defaultValue="java" groupId="lang"> <Tabs defaultValue="java" groupId="lang">
<TabItem label="Java" value="java"> <TabItem label="Java" value="java">
```java ```java
{{#include examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/SchemalessJniTest.java:schemaless}} {{#include docs/examples/java/src/main/java/com/taos/example/SchemalessJniTest.java:schemaless}}
``` ```
执行带有 reqId 的无模式写入,最后一个参数 reqId 可用于请求链路追踪。 执行带有 reqId 的无模式写入,最后一个参数 reqId 可用于请求链路追踪。

Some files were not shown because too many files have changed in this diff Show More