Merge branch 'feat/TD-30813-2' of https://github.com/taosdata/TDengine into feat/TD-30813-2
This commit is contained in:
commit
f0bd7af9cf
|
@ -134,3 +134,4 @@ tags
|
||||||
.clangd
|
.clangd
|
||||||
*CMakeCache*
|
*CMakeCache*
|
||||||
*CMakeFiles*
|
*CMakeFiles*
|
||||||
|
.history/
|
||||||
|
|
|
@ -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).
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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 */
|
||||||
|
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
|
@ -33,25 +33,27 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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') "
|
||||||
|
"VALUES "
|
||||||
"(NOW + 1a, 10.30000, 218, 0.25000) ";
|
"(NOW + 1a, 10.30000, 218, 0.25000) ";
|
||||||
TAOS_RES *result = taos_query(taos, sql);
|
TAOS_RES *result = taos_query(taos, sql);
|
||||||
code = taos_errno(result);
|
code = taos_errno(result);
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
printf("Failed to insert data to power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result));
|
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_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -60,7 +62,7 @@ taos_free_result(result);
|
||||||
|
|
||||||
// you can check affectedRows here
|
// you can check affectedRows here
|
||||||
int rows = taos_affected_rows(result);
|
int rows = taos_affected_rows(result);
|
||||||
printf("Successfully inserted %d rows into power.meters.\n", rows);
|
fprintf(stdout, "Successfully inserted %d rows into power.meters.\n", rows);
|
||||||
|
|
||||||
// close & clean
|
// close & clean
|
||||||
taos_close(taos);
|
taos_close(taos);
|
||||||
|
|
|
@ -22,7 +22,6 @@
|
||||||
#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";
|
||||||
|
@ -34,19 +33,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// query data, please make sure the database and table are already created
|
// query data, please make sure the database and table are already created
|
||||||
const char *sql = "SELECT ts, current, location FROM power.meters limit 100";
|
const char *sql = "SELECT ts, current, location FROM power.meters limit 100";
|
||||||
TAOS_RES *result = taos_query(taos, sql);
|
TAOS_RES *result = taos_query(taos, sql);
|
||||||
code = taos_errno(result);
|
code = taos_errno(result);
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
printf("Failed to query data from power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result));
|
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_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -57,17 +56,15 @@ int rows = 0;
|
||||||
int num_fields = taos_field_count(result);
|
int num_fields = taos_field_count(result);
|
||||||
TAOS_FIELD *fields = taos_fetch_fields(result);
|
TAOS_FIELD *fields = taos_fetch_fields(result);
|
||||||
|
|
||||||
printf("fields: %d\n", num_fields);
|
fprintf(stdout, "query successfully, got %d fields, the sql is: %s.\n", num_fields, sql);
|
||||||
printf("sql: %s, result:\n", sql);
|
|
||||||
|
|
||||||
// fetch the records row by row
|
// fetch the records row by row
|
||||||
while ((row = taos_fetch_row(result))) {
|
while ((row = taos_fetch_row(result))) {
|
||||||
char temp[1024] = {0};
|
// Add your data processing logic here
|
||||||
|
|
||||||
rows++;
|
rows++;
|
||||||
taos_print_row(temp, row, fields, num_fields);
|
|
||||||
printf("%s\n", temp);
|
|
||||||
}
|
}
|
||||||
printf("total rows: %d\n", rows);
|
fprintf(stdout, "total rows: %d\n", rows);
|
||||||
taos_free_result(result);
|
taos_free_result(result);
|
||||||
|
|
||||||
// close & clean
|
// close & clean
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
#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";
|
||||||
|
@ -33,7 +32,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -42,19 +42,18 @@ if (taos == NULL) {
|
||||||
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;
|
||||||
|
@ -62,23 +61,28 @@ if (code != 0) {
|
||||||
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 =
|
||||||
|
"meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 "
|
||||||
|
"1626006833639";
|
||||||
char *telnet_demo = "metric_telnet 1707095283260 4 host=host0 interface=eth0";
|
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\"}}";
|
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_errstr(result));
|
||||||
taos_close(taos);
|
taos_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
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
|
||||||
|
@ -86,22 +90,24 @@ 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_errstr(result));
|
||||||
taos_close(taos);
|
taos_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
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;
|
||||||
|
jsons[0] = malloc(size);
|
||||||
if (jsons[0] == NULL) {
|
if (jsons[0] == NULL) {
|
||||||
printf("Failed to allocate memory\n");
|
fprintf(stderr, "Failed to allocate memory: %zu bytes.\n", size);
|
||||||
taos_close(taos);
|
taos_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -111,7 +117,8 @@ result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML
|
||||||
code = taos_errno(result);
|
code = taos_errno(result);
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
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));
|
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_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -119,7 +126,7 @@ if (code != 0) {
|
||||||
free(jsons[0]);
|
free(jsons[0]);
|
||||||
|
|
||||||
rows = taos_affected_rows(result);
|
rows = taos_affected_rows(result);
|
||||||
printf("Insert %d rows of schemaless json data successfully.\n", rows);
|
fprintf(stdout, "Insert %d rows of schemaless json data successfully.\n", rows);
|
||||||
taos_free_result(result);
|
taos_free_result(result);
|
||||||
|
|
||||||
// close & clean
|
// close & clean
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,6 +41,18 @@ 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";
|
||||||
|
@ -78,43 +91,27 @@ 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";
|
||||||
|
@ -124,16 +121,16 @@ static int32_t init_env() {
|
||||||
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_close(pConn);
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t create_topic(TAOS* pConn) {
|
||||||
TAOS_RES* pRes;
|
TAOS_RES* pRes;
|
||||||
const char *host = "localhost";
|
|
||||||
const char *user = "root";
|
|
||||||
const char *password = "taosdata";
|
|
||||||
uint16_t port = 6030;
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ 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_close(taos);
|
||||||
taos_cleanup();
|
taos_cleanup();
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -55,17 +55,15 @@ int rows = 0;
|
||||||
int num_fields = taos_field_count(result);
|
int num_fields = taos_field_count(result);
|
||||||
TAOS_FIELD *fields = taos_fetch_fields(result);
|
TAOS_FIELD *fields = taos_fetch_fields(result);
|
||||||
|
|
||||||
printf("fields: %d\n", num_fields);
|
fprintf(stdout, "query successfully, got %d fields, the sql is: %s.\n", num_fields, sql);
|
||||||
printf("sql: %s, result:\n", sql);
|
|
||||||
|
|
||||||
// fetch the records row by row
|
// fetch the records row by row
|
||||||
while ((row = taos_fetch_row(result))) {
|
while ((row = taos_fetch_row(result))) {
|
||||||
char temp[1024] = {0};
|
// Add your data processing logic here
|
||||||
|
|
||||||
rows++;
|
rows++;
|
||||||
taos_print_row(temp, row, fields, num_fields);
|
|
||||||
printf("%s\n", temp);
|
|
||||||
}
|
}
|
||||||
printf("total rows: %d\n", rows);
|
fprintf(stdout, "total rows: %d\n", rows);
|
||||||
taos_free_result(result);
|
taos_free_result(result);
|
||||||
|
|
||||||
// close & clean
|
// close & clean
|
||||||
|
@ -78,4 +76,3 @@ return 0;
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
return DemoWithReqId();
|
return DemoWithReqId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,13 +42,13 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,13 +62,13 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -52,13 +52,14 @@ 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
|
||||||
|
@ -67,8 +68,6 @@ namespace Examples
|
||||||
private static void InsertData(ITDengineClient client, string connectionString)
|
private static void InsertData(ITDengineClient client, string connectionString)
|
||||||
{
|
{
|
||||||
// ANCHOR: insert_data
|
// ANCHOR: insert_data
|
||||||
try
|
|
||||||
{
|
|
||||||
// insert data, please make sure the database and table are created before
|
// insert data, please make sure the database and table are created before
|
||||||
var insertQuery = "INSERT INTO " +
|
var insertQuery = "INSERT INTO " +
|
||||||
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
|
"power.d1001 USING power.meters TAGS(2,'California.SanFrancisco') " +
|
||||||
|
@ -79,19 +78,24 @@ namespace Examples
|
||||||
"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) ";
|
||||||
|
try
|
||||||
|
{
|
||||||
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
|
||||||
|
@ -100,14 +104,15 @@ namespace Examples
|
||||||
private static void QueryData(ITDengineClient client, string connectionString)
|
private static void QueryData(ITDengineClient client, string connectionString)
|
||||||
{
|
{
|
||||||
// ANCHOR: select_data
|
// ANCHOR: select_data
|
||||||
try
|
|
||||||
{
|
|
||||||
// query data, make sure the database and table are created before
|
// query data, make sure the database and table are created before
|
||||||
var query = "SELECT ts, current, location FROM power.meters limit 100";
|
var query = "SELECT ts, current, location FROM power.meters limit 100";
|
||||||
|
try
|
||||||
|
{
|
||||||
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,13 +124,15 @@ 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
|
||||||
|
@ -135,10 +142,10 @@ namespace Examples
|
||||||
{
|
{
|
||||||
// ANCHOR: query_id
|
// ANCHOR: query_id
|
||||||
var reqId = (long)3;
|
var reqId = (long)3;
|
||||||
try
|
|
||||||
{
|
|
||||||
// query data
|
// query data
|
||||||
var query = "SELECT ts, current, location FROM power.meters limit 1";
|
var query = "SELECT ts, current, location FROM power.meters limit 1";
|
||||||
|
try
|
||||||
|
{
|
||||||
// query with request id 3
|
// query with request id 3
|
||||||
using (var rows = client.Query(query, reqId))
|
using (var rows = client.Query(query, reqId))
|
||||||
{
|
{
|
||||||
|
@ -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
|
||||||
|
|
|
@ -63,13 +63,13 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,14 +44,14 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -14,7 +14,7 @@ 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)
|
||||||
|
@ -23,11 +23,13 @@ func main() {
|
||||||
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"
|
||||||
|
rows, err := db.QueryContext(ctx, querySql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to execute sql with reqId: %d, url: %s; ErrMessage: %s\n", reqId, taosDSN, err.Error())
|
log.Fatalf("Failed to execute sql with reqId: %d, url: %s, sql: %s, ErrMessage: %s\n", reqId, taosDSN, querySql, err.Error())
|
||||||
}
|
}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
// Add your data processing logic here
|
||||||
var (
|
var (
|
||||||
ts time.Time
|
ts time.Time
|
||||||
current float32
|
current float32
|
||||||
|
@ -35,7 +37,7 @@ func main() {
|
||||||
)
|
)
|
||||||
err = rows.Scan(&ts, ¤t, &location)
|
err = rows.Scan(&ts, ¤t, &location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Scan error: ", err)
|
log.Fatalf("Failed to scan data, reqId: %d, url:%s, sql: %s, ErrMessage: %s\n", reqId, taosDSN, querySql, 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)
|
||||||
}
|
}
|
||||||
|
@ -45,14 +47,14 @@ func main() {
|
||||||
func initEnv(conn *sql.DB) {
|
func initEnv(conn *sql.DB) {
|
||||||
_, 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.Fatal("Create database error: ", err)
|
log.Fatal("Create database power 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))")
|
_, 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 {
|
if err != nil {
|
||||||
log.Fatal("Create table error: ", err)
|
log.Fatal("Create stable meters error: ", err)
|
||||||
}
|
}
|
||||||
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
|
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Insert data error: ", err)
|
log.Fatal("Insert data to power.meters error: ", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,29 +13,29 @@ 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)
|
||||||
|
@ -53,22 +53,24 @@ func main() {
|
||||||
"(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"
|
||||||
|
rows, err := db.Query(sql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to query data from power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error())
|
log.Fatalf("Failed to query data from power.meters, sql: %s, ErrMessage: %s\n", sql, err.Error())
|
||||||
}
|
}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
|
// Add your data processing logic here
|
||||||
var (
|
var (
|
||||||
ts time.Time
|
ts time.Time
|
||||||
current float32
|
current float32
|
||||||
|
@ -76,9 +78,8 @@ func main() {
|
||||||
)
|
)
|
||||||
err = rows.Scan(&ts, ¤t, &location)
|
err = rows.Scan(&ts, ¤t, &location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to scan data, url:" + taosDSN + "; ErrMessage: " + err.Error())
|
log.Fatalf("Failed to scan data, sql: %s, ErrMessage: %s\n", sql, err)
|
||||||
}
|
}
|
||||||
// you can check data here
|
|
||||||
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
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,13 +12,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var done = make(chan struct{})
|
var done = make(chan struct{})
|
||||||
|
var groupID string
|
||||||
|
var clientID string
|
||||||
|
var host string
|
||||||
|
var topic string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// init env
|
// init env
|
||||||
taosDSN := "root:taosdata@tcp(127.0.0.1:6030)/"
|
taosDSN := "root:taosdata@tcp(127.0.0.1:6030)/"
|
||||||
conn, err := sql.Open("taosSql", taosDSN)
|
conn, 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 func() {
|
defer func() {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
@ -26,9 +30,9 @@ func main() {
|
||||||
initEnv(conn)
|
initEnv(conn)
|
||||||
// ANCHOR: create_consumer
|
// ANCHOR: create_consumer
|
||||||
// create consumer
|
// create consumer
|
||||||
groupID := "group1"
|
groupID = "group1"
|
||||||
clientID := "client1"
|
clientID = "client1"
|
||||||
host := "127.0.0.1"
|
host = "127.0.0.1"
|
||||||
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
|
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
|
||||||
"td.connect.user": "root",
|
"td.connect.user": "root",
|
||||||
"td.connect.pass": "taosdata",
|
"td.connect.pass": "taosdata",
|
||||||
|
@ -40,15 +44,28 @@ func main() {
|
||||||
"client.id": clientID,
|
"client.id": clientID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to create native consumer, host : " + host + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to create native consumer, host: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
host,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Create consumer successfully, host: " + host + ", groupId: " + groupID + ", clientId: " + clientID)
|
log.Printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s\n", host, groupID, clientID)
|
||||||
|
|
||||||
// ANCHOR_END: create_consumer
|
// ANCHOR_END: create_consumer
|
||||||
// ANCHOR: subscribe
|
// ANCHOR: subscribe
|
||||||
err = consumer.Subscribe("topic_meters", nil)
|
topic = "topic_meters"
|
||||||
|
err = consumer.Subscribe(topic, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to subscribe, host : " + host + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to subscribe topic_meters, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Subscribe topics successfully")
|
log.Println("Subscribe topics successfully")
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
|
@ -62,13 +79,19 @@ func main() {
|
||||||
// commit offset
|
// commit offset
|
||||||
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
|
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to commit offset, host : " + host + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to commit offset, topic: %s, groupId: %s, clientId: %s, offset %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
e.TopicPartition,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Commit offset manually successfully.")
|
log.Println("Commit offset manually successfully.")
|
||||||
// ANCHOR_END: commit_offset
|
// ANCHOR_END: commit_offset
|
||||||
case tmqcommon.Error:
|
case tmqcommon.Error:
|
||||||
fmt.Printf("%% Error: %v: %v\n", e.Code(), e)
|
log.Fatalf("Failed to poll data, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n", topic, groupID, clientID, e.Error())
|
||||||
log.Fatalln("Failed to poll data, host : " + host + "; ErrMessage: " + err.Error())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +100,7 @@ func main() {
|
||||||
// get assignment
|
// get assignment
|
||||||
partitions, err := consumer.Assignment()
|
partitions, err := consumer.Assignment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to get assignment; ErrMessage: " + err.Error())
|
log.Fatalf("Failed to get assignment, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n", topic, groupID, clientID, err.Error())
|
||||||
}
|
}
|
||||||
fmt.Println("Now assignment:", partitions)
|
fmt.Println("Now assignment:", partitions)
|
||||||
for i := 0; i < len(partitions); i++ {
|
for i := 0; i < len(partitions); i++ {
|
||||||
|
@ -88,7 +111,15 @@ func main() {
|
||||||
Offset: 0,
|
Offset: 0,
|
||||||
}, 0)
|
}, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Seek example failed; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to execute seek offset, topic: %s, groupId: %s, clientId: %s, partition: %d, offset: %d, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
partitions[i].Partition,
|
||||||
|
0,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("Assignment seek to beginning successfully")
|
fmt.Println("Assignment seek to beginning successfully")
|
||||||
|
@ -97,13 +128,27 @@ func main() {
|
||||||
// unsubscribe
|
// unsubscribe
|
||||||
err = consumer.Unsubscribe()
|
err = consumer.Unsubscribe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error())
|
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
|
// close consumer
|
||||||
err = consumer.Close()
|
err = consumer.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to close consumer. ErrMessage: " + err.Error())
|
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
|
// ANCHOR_END: close
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
@ -111,22 +156,22 @@ func main() {
|
||||||
func initEnv(conn *sql.DB) {
|
func initEnv(conn *sql.DB) {
|
||||||
_, 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.Fatal("Failed to create database. ErrMessage: " + err.Error())
|
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))")
|
_, 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 {
|
if err != nil {
|
||||||
log.Fatal("Failed to create stable. ErrMessage: " + err.Error())
|
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")
|
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to create topic. ErrMessage: " + err.Error())
|
log.Fatal("Failed to create topic, ErrMessage: " + err.Error())
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
time.Sleep(time.Second)
|
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)")
|
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to insert data. ErrMessage: " + err.Error())
|
log.Fatal("Failed to insert data, ErrMessage: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
|
|
|
@ -13,13 +13,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var done = make(chan struct{})
|
var done = make(chan struct{})
|
||||||
|
var groupID string
|
||||||
|
var clientID string
|
||||||
|
var host string
|
||||||
|
var topic string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// init env
|
// init env
|
||||||
taosDSN := "root:taosdata@ws(127.0.0.1:6041)/"
|
taosDSN := "root:taosdata@ws(127.0.0.1:6041)/"
|
||||||
conn, err := sql.Open("taosWS", taosDSN)
|
conn, err := sql.Open("taosWS", 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 func() {
|
defer func() {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
|
@ -28,8 +32,9 @@ func main() {
|
||||||
// ANCHOR: create_consumer
|
// ANCHOR: create_consumer
|
||||||
// create consumer
|
// create consumer
|
||||||
wsUrl := "ws://127.0.0.1:6041"
|
wsUrl := "ws://127.0.0.1:6041"
|
||||||
groupID := "group1"
|
groupID = "group1"
|
||||||
clientID := "client1"
|
clientID = "client1"
|
||||||
|
host = "127.0.0.1"
|
||||||
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
|
consumer, err := tmq.NewConsumer(&tmqcommon.ConfigMap{
|
||||||
"ws.url": wsUrl,
|
"ws.url": wsUrl,
|
||||||
"ws.message.channelLen": uint(0),
|
"ws.message.channelLen": uint(0),
|
||||||
|
@ -45,15 +50,28 @@ func main() {
|
||||||
"client.id": clientID,
|
"client.id": clientID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to create websocket consumer, host : " + wsUrl + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to create websocket consumer, host: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
host,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Create consumer successfully, host: " + wsUrl + ", groupId: " + groupID + ", clientId: " + clientID)
|
log.Printf("Create consumer successfully, host: %s, groupId: %s, clientId: %s\n", host, groupID, clientID)
|
||||||
|
|
||||||
// ANCHOR_END: create_consumer
|
// ANCHOR_END: create_consumer
|
||||||
// ANCHOR: subscribe
|
// ANCHOR: subscribe
|
||||||
err = consumer.Subscribe("topic_meters", nil)
|
topic = "topic_meters"
|
||||||
|
err = consumer.Subscribe(topic, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to subscribe, host : " + wsUrl + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to subscribe topic_meters, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Subscribe topics successfully")
|
log.Println("Subscribe topics successfully")
|
||||||
for i := 0; i < 50; i++ {
|
for i := 0; i < 50; i++ {
|
||||||
|
@ -67,13 +85,25 @@ func main() {
|
||||||
// commit offset
|
// commit offset
|
||||||
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
|
_, err = consumer.CommitOffsets([]tmqcommon.TopicPartition{e.TopicPartition})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Failed to commit offset, host : " + wsUrl + "; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to commit offset, topic: %s, groupId: %s, clientId: %s, offset %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
e.TopicPartition,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
log.Println("Commit offset manually successfully.")
|
log.Println("Commit offset manually successfully.")
|
||||||
// ANCHOR_END: commit_offset
|
// ANCHOR_END: commit_offset
|
||||||
case tmqcommon.Error:
|
case tmqcommon.Error:
|
||||||
fmt.Printf("%% Error: %v: %v\n", e.Code(), e)
|
log.Fatalf(
|
||||||
log.Fatalln("Failed to poll data, host : " + wsUrl + "; ErrMessage: " + err.Error())
|
"Failed to poll data, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
e.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +112,13 @@ func main() {
|
||||||
// get assignment
|
// get assignment
|
||||||
partitions, err := consumer.Assignment()
|
partitions, err := consumer.Assignment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to get assignment; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to get assignment, topic: %s, groupId: %s, clientId: %s, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
fmt.Println("Now assignment:", partitions)
|
fmt.Println("Now assignment:", partitions)
|
||||||
for i := 0; i < len(partitions); i++ {
|
for i := 0; i < len(partitions); i++ {
|
||||||
|
@ -93,7 +129,15 @@ func main() {
|
||||||
Offset: 0,
|
Offset: 0,
|
||||||
}, 0)
|
}, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln("Seek example failed; ErrMessage: " + err.Error())
|
log.Fatalf(
|
||||||
|
"Failed to seek offset, topic: %s, groupId: %s, clientId: %s, partition: %d, offset: %d, ErrMessage: %s\n",
|
||||||
|
topic,
|
||||||
|
groupID,
|
||||||
|
clientID,
|
||||||
|
partitions[i].Partition,
|
||||||
|
0,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println("Assignment seek to beginning successfully")
|
fmt.Println("Assignment seek to beginning successfully")
|
||||||
|
@ -102,13 +146,27 @@ func main() {
|
||||||
// unsubscribe
|
// unsubscribe
|
||||||
err = consumer.Unsubscribe()
|
err = consumer.Unsubscribe()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to unsubscribe consumer. ErrMessage: " + err.Error())
|
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
|
// close consumer
|
||||||
err = consumer.Close()
|
err = consumer.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to close consumer. ErrMessage: " + err.Error())
|
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
|
// ANCHOR_END: close
|
||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
|
@ -116,22 +174,22 @@ func main() {
|
||||||
func initEnv(conn *sql.DB) {
|
func initEnv(conn *sql.DB) {
|
||||||
_, 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.Fatal("Failed to create database. ErrMessage: " + err.Error())
|
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))")
|
_, 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 {
|
if err != nil {
|
||||||
log.Fatal("Failed to create stable. ErrMessage: " + err.Error())
|
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")
|
_, err = conn.Exec("CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM power.meters")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to create topic. ErrMessage: " + err.Error())
|
log.Fatal("Failed to create topic, ErrMessage: " + err.Error())
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
time.Sleep(time.Second)
|
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)")
|
_, err = conn.Exec("INSERT INTO power.d1001 USING power.meters TAGS (2, 'California.SanFrancisco') VALUES (NOW , 10.2, 219, 0.32)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to insert data. ErrMessage: " + err.Error())
|
log.Fatal("Failed to insert data, ErrMessage: " + err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done <- struct{}{}
|
done <- struct{}{}
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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);
|
|
@ -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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
// 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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
// 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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
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());
|
|
@ -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;
|
|
@ -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
|
|
@ -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
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
|
@ -9,9 +9,10 @@ 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 =
|
||||||
|
// "jdbc:TAOS://localhost:6030/dbName?user=root&password=taosdata";
|
||||||
// if you want to connect a specified database named "dbName".
|
// if you want to connect a specified database named "dbName".
|
||||||
String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
|
String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
|
||||||
Properties connProps = new Properties();
|
Properties connProps = new Properties();
|
||||||
|
@ -24,15 +25,16 @@ public static void main(String[] args) throws SQLException {
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ANCHOR_END: main
|
// ANCHOR_END: main
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 (SQLException ex) {
|
|
||||||
// handle any errors, please refer to the JDBC specifications for detailed exceptions info
|
|
||||||
System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage());
|
|
||||||
throw ex;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to create db and table, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to create database power or stable 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;
|
throw ex;
|
||||||
}
|
}
|
||||||
// ANCHOR_END: create_db_and_table
|
// ANCHOR_END: create_db_and_table
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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) {
|
|
||||||
// handle any errors, 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());
|
|
||||||
throw ex;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to query data from power.meters, url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to query data from power.meters, sql: %s, %sErrMessage: %s%n",
|
||||||
|
sql,
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
// ANCHOR_END: query_data
|
// ANCHOR_END: query_data
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printResult(ResultSet resultSet) throws SQLException {
|
|
||||||
Util.printResult(resultSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -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) {
|
|
||||||
// handle any errors, 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());
|
|
||||||
throw ex;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to execute sql with reqId: " + reqId + ", url:" + jdbcUrl + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to execute sql with reqId: %s, %sErrMessage: %s%n", reqId,
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
// ANCHOR_END: with_reqid
|
// ANCHOR_END: with_reqid
|
|
@ -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) {
|
|
||||||
// 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) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to insert to table meters using stmt, %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;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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
|
|
@ -6,19 +6,21 @@ 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) {
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
|
||||||
// handle any errors, 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());
|
|
||||||
throw ex;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to insert data with schemaless, %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;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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) {
|
|
||||||
// handle any errors, 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());
|
|
||||||
throw ex;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to insert data with schemaless, host:" + host + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to insert data with schemaless, %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;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
|
||||||
// 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) {
|
} catch (Exception ex) {
|
||||||
System.out.println("Failed to insert to table meters using stmt, url: " + jdbcUrl + "; ErrMessage: " + ex.getMessage());
|
// please refer to the JDBC specifications for detailed exceptions info
|
||||||
|
System.out.printf("Failed to insert to table meters using stmt, %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;
|
throw ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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
|
|
@ -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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
// 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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
// 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
|
||||||
try {
|
|
||||||
List<String> topics = Collections.singletonList("topic_meters");
|
List<String> topics = Collections.singletonList("topic_meters");
|
||||||
|
try {
|
||||||
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;
|
|
@ -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;
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -13,7 +13,7 @@ async function createConnect() {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ async function createDbAndTable() {
|
||||||
|
|
||||||
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();
|
||||||
|
@ -62,7 +62,7 @@ async function insertData() {
|
||||||
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,16 +75,17 @@ 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) {
|
||||||
|
@ -111,7 +112,7 @@ async function sqlWithReqid() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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) {
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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],
|
||||||
|
@ -24,7 +28,7 @@ async function createConsumer() {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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}")
|
||||||
finally:
|
finally:
|
||||||
if conn:
|
if conn:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
|
@ -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}")
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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}")
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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}")
|
|
@ -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()
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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"
|
||||||
|
@ -39,7 +46,7 @@ def prepareMeta():
|
||||||
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);
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,26 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
import taosws
|
import taosws
|
||||||
|
|
||||||
|
db = "power"
|
||||||
topic = "topic_meters"
|
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:
|
||||||
|
@ -60,25 +68,21 @@ 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)
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ 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)
|
||||||
|
@ -17,14 +17,14 @@ async fn main() -> anyhow::Result<()> {
|
||||||
(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(())
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ 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)
|
||||||
|
@ -17,14 +17,14 @@ async fn main() -> anyhow::Result<()> {
|
||||||
(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(())
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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-Out,FIFO),将最近到达的(当前状态)数据保存在缓存中,这样便于获取任何监测对象的实时状态,而无需使用 Redis 等其他缓存工具,简化系统架构和运营成本。
|
3. 缓存:TDengine 使用时间驱动缓存管理策略(First-In-First-Out,FIFO),将最近到达的(当前状态)数据保存在缓存中,这样便于获取任何监测对象的实时状态,而无需使用 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 与典型时序数据库的区别
|
||||||
|
|
||||||
|
@ -60,14 +60,12 @@ TDengine 经过特别优化,以适应时间序列数据的独特需求,引
|
||||||
|
|
||||||
5. 简单易用:TDengine 安装无依赖,集群部署仅需几秒即可完成。它提供了 RESTful 接口和多种编程语言的连接器,与众多第三方工具无缝集成。此外,命令行程序和丰富的运维工具也极大地方便了用户的管理和即时查询需求。
|
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 集成了消息队列、缓存、流计算等必要功能,避免了额外集成众多其他组件的需要。这种简化的系统架构显著降低了系统的复杂度,从而减少了研发和运营成本,提高了整体运营效率。
|
||||||
种简化的系统架构显著降低了系统的复杂度,从而减少了研发和运营成本,提高了整体运营效率。
|
|
||||||
|
|
||||||
## 技术生态
|
## 技术生态
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -4,8 +4,6 @@ title: 集群维护
|
||||||
sidebar_label: 集群维护
|
sidebar_label: 集群维护
|
||||||
---
|
---
|
||||||
|
|
||||||
## 简介
|
|
||||||
|
|
||||||
本节介绍 TDengine Enterprise 中提供的高阶集群维护手段,能够使 TDengine 集群长期运行得更健壮和高效。
|
本节介绍 TDengine Enterprise 中提供的高阶集群维护手段,能够使 TDengine 集群长期运行得更健壮和高效。
|
||||||
|
|
||||||
## 节点管理
|
## 节点管理
|
||||||
|
|
|
@ -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 版本中第一次发布,建议使用最新版本。
|
||||||
|
|
||||||
|
|
|
@ -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)。
|
||||||
|
|
|
@ -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>
|
||||||
|
|
|
@ -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
Loading…
Reference in New Issue