From d4e2998e7fcc3cb5dc6a5e09ec5d3974da00e0b8 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 15:01:34 +0800 Subject: [PATCH 1/8] c language sample optimization --- docs/examples/c/connect_example.c | 12 ++-- docs/examples/c/create_db_demo.c | 25 ++++---- docs/examples/c/insert_data_demo.c | 21 +++--- docs/examples/c/query_data_demo.c | 20 +++--- docs/examples/c/sml_insert_demo.c | 45 ++++++++----- docs/examples/c/stmt_insert_demo.c | 27 +++++--- docs/examples/c/tmq_demo.c | 46 +++++++++---- docs/examples/c/with_reqid_demo.c | 100 +++++++++++++++-------------- 8 files changed, 166 insertions(+), 130 deletions(-) diff --git a/docs/examples/c/connect_example.c b/docs/examples/c/connect_example.c index f31db557cc..614e828018 100644 --- a/docs/examples/c/connect_example.c +++ b/docs/examples/c/connect_example.c @@ -8,19 +8,15 @@ int main() { const char *host = "localhost"; const char *user = "root"; const char *passwd = "taosdata"; - // if don't want to connect to a default db, set it to NULL or "" - const char *db = NULL; - uint16_t port = 0; // 0 means use the default port + const char *db = NULL; // if don't want to connect to a default db, set it to NULL or "" + uint16_t port = 6030; // 0 means use the default port TAOS *taos = taos_connect(host, user, passwd, db, port); if (taos == NULL) { - int errno = taos_errno(NULL); - const char *msg = taos_errstr(NULL); - printf("%d, %s\n", errno, msg); - printf("failed to connect to server %s, errno: %d, msg: %s\n", host, errno, msg); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } - printf("success to connect server %s\n", host); + printf("Connected to %s:%hu successfully.\n", host, port); /* put your code here for read and write */ diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index 32168edcaa..14848f8237 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -25,47 +25,44 @@ static int DemoCreateDB() { // ANCHOR: create_db_and_table -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); // create database TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -int code = taos_errno(result); +code = taos_errno(result); if (code != 0) { - printf("failed to create database power, reason: %s\n", taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create database power\n"); - -// use database -result = taos_query(taos, "USE power"); -taos_free_result(result); +printf("Create database power successfully.\n"); // create table -const char* sql = "CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"; +const char* sql = "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))"; result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("failed to create stable meters, reason: %s\n", taos_errstr(result)); + printf("Failed to create stable power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create table meters\n"); +printf("Create stable meters successfully.\n"); // close & clean taos_close(taos); diff --git a/docs/examples/c/insert_data_demo.c b/docs/examples/c/insert_data_demo.c index 03e86d7f2c..8796556d03 100644 --- a/docs/examples/c/insert_data_demo.c +++ b/docs/examples/c/insert_data_demo.c @@ -24,22 +24,19 @@ static int DemoInsertData() { // ANCHOR: insert_data -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); - -// use database -TAOS_RES *result = taos_query(taos, "USE power"); -taos_free_result(result); // insert data, please make sure the database and table are already created const char* sql = "INSERT INTO " \ @@ -51,10 +48,10 @@ const char* sql = "INSERT INTO " "power.d1002 USING power.meters TAGS(3, 'California.SanFrancisco') " \ "VALUES " \ "(NOW + 1a, 10.30000, 218, 0.25000) "; -result = taos_query(taos, sql); -int code = taos_errno(result); +TAOS_RES *result = taos_query(taos, sql); +code = taos_errno(result); if (code != 0) { - printf("failed to insert data to power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); + printf("Failed to insert data to power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -63,7 +60,7 @@ taos_free_result(result); // you can check affectedRows here int rows = taos_affected_rows(result); -printf("success to insert %d rows data to power.meters\n", rows); +printf("Successfully inserted %d rows into power.meters.\n", rows); // close & clean taos_close(taos); diff --git a/docs/examples/c/query_data_demo.c b/docs/examples/c/query_data_demo.c index 02f54b537c..77af4c9a8d 100644 --- a/docs/examples/c/query_data_demo.c +++ b/docs/examples/c/query_data_demo.c @@ -25,29 +25,28 @@ static int DemoQueryData() { // ANCHOR: query_data -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); -// use database -TAOS_RES *result = taos_query(taos, "USE power"); -taos_free_result(result); + // query data, please make sure the database and table are already created const char* sql = "SELECT ts, current, location FROM power.meters limit 100"; -result = taos_query(taos, sql); -int code = taos_errno(result); +TAOS_RES *result = taos_query(taos, sql); +code = taos_errno(result); if (code != 0) { - printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); + printf("Failed to query data from power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -70,7 +69,6 @@ while ((row = taos_fetch_row(result))) { } printf("total rows: %d\n", rows); taos_free_result(result); -printf("success to query data from power.meters\n"); // close & clean taos_close(taos); diff --git a/docs/examples/c/sml_insert_demo.c b/docs/examples/c/sml_insert_demo.c index 0c4be1d6f8..65636d89b8 100644 --- a/docs/examples/c/sml_insert_demo.c +++ b/docs/examples/c/sml_insert_demo.c @@ -24,33 +24,41 @@ static int DemoSmlInsert() { // ANCHOR: schemaless -const char *ip = "localhost"; +const char *host = "localhost"; const char *user = "root"; const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; // connect -TAOS *taos = taos_connect(ip, user, password, NULL, 0); +TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } -printf("success to connect server %s\n", ip); // create database TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); -int code = taos_errno(result); +code = taos_errno(result); if (code != 0) { - printf("failed to create database power, reason: %s\n", taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; } taos_free_result(result); -printf("success to create database power\n"); +printf("Create database power successfully.\n"); // use database result = taos_query(taos, "USE power"); +code = taos_errno(result); +if (code != 0) { + printf("Failed to execute use power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + taos_close(taos); + taos_cleanup(); + return -1; +} taos_free_result(result); // schemaless demo data @@ -61,29 +69,31 @@ char * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"val // influxdb line protocol char *lines[] = {line_demo}; result = taos_schemaless_insert(taos, lines, 1, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -if (taos_errno(result) != 0) { - printf("failed to insert schemaless line data, reason: %s\n", taos_errstr(result)); +code = taos_errno(result); +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)); taos_close(taos); taos_cleanup(); return -1; } int rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless line data\n", rows); +printf("Insert %d rows of schemaless line data successfully.\n", rows); taos_free_result(result); // opentsdb telnet protocol char *telnets[] = {telnet_demo}; result = taos_schemaless_insert(taos, telnets, 1, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS); -if (taos_errno(result) != 0) { - printf("failed to insert schemaless telnet data, reason: %s\n", taos_errstr(result)); +code = taos_errno(result); +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)); taos_close(taos); taos_cleanup(); return -1; } rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless telnet data\n", rows); +printf("Insert %d rows of schemaless telnet data successfully.\n", rows); taos_free_result(result); // opentsdb json protocol @@ -91,16 +101,17 @@ char *jsons[1] = {0}; // allocate memory for json data. can not use static memory. jsons[0] = malloc(1024); if (jsons[0] == NULL) { - printf("failed to allocate memory\n"); + printf("Failed to allocate memory\n"); taos_close(taos); taos_cleanup(); return -1; } (void)strncpy(jsons[0], json_demo, 1023); result = taos_schemaless_insert(taos, jsons, 1, TSDB_SML_JSON_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED); -if (taos_errno(result) != 0) { +code = taos_errno(result); +if (code != 0) { free(jsons[0]); - printf("failed to insert schemaless json data, reason: %s\n", taos_errstr(result)); + printf("Failed to insert schemaless json data, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -108,7 +119,7 @@ if (taos_errno(result) != 0) { free(jsons[0]); rows = taos_affected_rows(result); -printf("success to insert %d rows of schemaless json data\n", rows); +printf("Insert %d rows of schemaless json data successfully.\n", rows); taos_free_result(result); // close & clean diff --git a/docs/examples/c/stmt_insert_demo.c b/docs/examples/c/stmt_insert_demo.c index ba953a856f..4bf794bcda 100644 --- a/docs/examples/c/stmt_insert_demo.c +++ b/docs/examples/c/stmt_insert_demo.c @@ -64,6 +64,7 @@ typedef struct { int num_of_sub_table = 10; int num_of_row = 10; +int total_affected = 0; /** * @brief insert data using stmt API * @@ -72,10 +73,14 @@ int num_of_row = 10; void insertData(TAOS *taos) { // init TAOS_STMT *stmt = taos_stmt_init(taos); + if (stmt == NULL) { + printf("Failed to init taos_stmt, error: %s\n", taos_stmt_errstr(NULL)); + exit(EXIT_FAILURE); + } // prepare const char *sql = "INSERT INTO ? USING meters TAGS(?,?) VALUES (?,?,?,?)"; int code = taos_stmt_prepare(stmt, sql, 0); - checkErrorCode(stmt, code, "failed to execute taos_stmt_prepare"); + checkErrorCode(stmt, code, "Failed to execute taos_stmt_prepare"); for (int i = 1; i <= num_of_sub_table; i++) { char table_name[20]; sprintf(table_name, "d_bind_%d", i); @@ -99,7 +104,7 @@ void insertData(TAOS *taos) { tags[1].is_null = NULL; tags[1].num = 1; code = taos_stmt_set_tbname_tags(stmt, table_name, tags); - checkErrorCode(stmt, code, "failed to set table name and tags\n"); + checkErrorCode(stmt, code, "Failed to set table name and tags\n"); // insert rows TAOS_MULTI_BIND params[4]; @@ -142,25 +147,31 @@ void insertData(TAOS *taos) { params[3].buffer = &phase; // bind param code = taos_stmt_bind_param(stmt, params); - checkErrorCode(stmt, code, "failed to bind param"); + checkErrorCode(stmt, code, "Failed to bind param"); } // add batch code = taos_stmt_add_batch(stmt); - checkErrorCode(stmt, code, "failed to add batch"); + checkErrorCode(stmt, code, "Failed to add batch"); // execute batch code = taos_stmt_execute(stmt); - checkErrorCode(stmt, code, "failed to exec stmt"); + checkErrorCode(stmt, code, "Failed to exec stmt"); // get affected rows int affected = taos_stmt_affected_rows_once(stmt); - printf("table %s insert %d rows.\n", table_name, affected); + total_affected += affected; } + printf("Successfully inserted %d rows to power.meters.\n", total_affected); taos_stmt_close(stmt); } int main() { - TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 6030); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("failed to connect to server\n"); + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); exit(EXIT_FAILURE); } // create database and table diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index adce4bbd5f..30cbbdc1c0 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -27,9 +27,15 @@ static int running = 1; const char* topic_name = "topic_meters"; void* prepare_data(void* arg) { - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { - return NULL; + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); + return -1; } TAOS_RES* pRes; @@ -46,12 +52,12 @@ void* prepare_data(void* arg) { pRes = taos_query(pConn, buf); if (taos_errno(pRes) != 0) { - printf("error in insert data to power.meters, reason:%s\n", taos_errstr(pRes)); + printf("Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); } - printf("prepare data thread exit\n"); + printf("Prepare data thread exit\n"); return NULL; } @@ -81,7 +87,7 @@ static int32_t msg_process(TAOS_RES* msg) { rows++; // print the row content if (taos_print_row(buf, row, fields, numOfFields) < 0) { - printf("failed to print row\n"); + printf("Failed to print row\n"); break; } // print the precision and row content to the console @@ -93,8 +99,14 @@ static int32_t msg_process(TAOS_RES* msg) { // ANCHOR_END: msg_process static int32_t init_env() { - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); return -1; } @@ -145,8 +157,14 @@ END: int32_t create_topic() { printf("create topic\n"); TAOS_RES* pRes; - TAOS* pConn = taos_connect("localhost", "root", "taosdata", NULL, 0); + const char *host = "localhost"; + const char *user = "root"; + const char *password = "taosdata"; + uint16_t port = 6030; + TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + taos_cleanup(); return -1; } @@ -285,7 +303,7 @@ void consume_repeatly(tmq_t* tmq) { // get the topic assignment int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment); if (code != 0 || pAssign == NULL || numOfAssignment == 0) { - fprintf(stderr, "failed to get assignment, reason:%s", tmq_err2str(code)); + fprintf(stderr, "Failed to get assignment, reason:%s", tmq_err2str(code)); return; } @@ -295,7 +313,7 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); } } @@ -328,6 +346,8 @@ void manual_commit(tmq_t* tmq) { // free the message taos_free_result(tmqmsg); break; + } else { + printf("Commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -356,17 +376,17 @@ int main(int argc, char* argv[]) { } if (pthread_create(&thread_id, NULL, &prepare_data, NULL)) { - fprintf(stderr, "create thread failed\n"); + fprintf(stderr, "Create thread failed\n"); return 1; } // ANCHOR: create_consumer_2 tmq_t* tmq = build_consumer(); if (NULL == tmq) { - fprintf(stderr, "build consumer to localhost fail!\n"); + fprintf(stderr, "Failed to create consumer.\n"); return -1; } - printf("build consumer to localhost successfully \n"); + printf("Create consumer successfully.\n"); // ANCHOR_END: create_consumer_2 @@ -378,6 +398,8 @@ int main(int argc, char* argv[]) { if ((code = tmq_subscribe(tmq, topic_list))) { fprintf(stderr, "Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); + } else { + printf("Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); diff --git a/docs/examples/c/with_reqid_demo.c b/docs/examples/c/with_reqid_demo.c index 50d29805f9..99f03a579e 100644 --- a/docs/examples/c/with_reqid_demo.c +++ b/docs/examples/c/with_reqid_demo.c @@ -23,55 +23,59 @@ #include "taos.h" static int DemoWithReqId() { - // ANCHOR: with_reqid - const char *ip = "localhost"; - const char *user = "root"; - const char *password = "taosdata"; +// ANCHOR: with_reqid +const char *host = "localhost"; +const char *user = "root"; +const char *password = "taosdata"; +uint16_t port = 6030; +int code = 0; - // connect - TAOS *taos = taos_connect(ip, user, password, NULL, 0); - if (taos == NULL) { - printf("failed to connect to server %s, reason: %s\n", ip, taos_errstr(NULL)); - taos_cleanup(); - return -1; - } - printf("success to connect server %s\n", ip); - - const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; - // query data with reqid - TAOS_RES *result = taos_query_with_reqid(taos, sql, 3L); - int code = taos_errno(result); - if (code != 0) { - printf("failed to query data from power.meters, ip: %s, reason: %s\n", ip, taos_errstr(result)); - taos_close(taos); - taos_cleanup(); - return -1; - } - - TAOS_ROW row = NULL; - int rows = 0; - int num_fields = taos_field_count(result); - TAOS_FIELD *fields = taos_fetch_fields(result); - - printf("fields: %d\n", num_fields); - printf("sql: %s, result:\n", sql); - - // fetch the records row by row - while ((row = taos_fetch_row(result))) { - char temp[1024] = {0}; - rows++; - taos_print_row(temp, row, fields, num_fields); - printf("%s\n", temp); - } - printf("total rows: %d\n", rows); - taos_free_result(result); - printf("success to query data from power.meters\n"); - - // close & clean - taos_close(taos); +// connect +TAOS *taos = taos_connect(host, user, password, NULL, port); +if (taos == NULL) { + printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); - return 0; - // ANCHOR_END: with_reqid + return -1; +} + +const char *sql = "SELECT ts, current, location FROM power.meters limit 1"; +// query data with reqid +long reqid = 3L; +TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); +code = taos_errno(result); +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)); + taos_close(taos); + taos_cleanup(); + return -1; +} + +TAOS_ROW row = NULL; +int rows = 0; +int num_fields = taos_field_count(result); +TAOS_FIELD *fields = taos_fetch_fields(result); + +printf("fields: %d\n", num_fields); +printf("sql: %s, result:\n", sql); + +// fetch the records row by row +while ((row = taos_fetch_row(result))) { + char temp[1024] = {0}; + rows++; + taos_print_row(temp, row, fields, num_fields); + printf("%s\n", temp); +} +printf("total rows: %d\n", rows); +taos_free_result(result); + +// close & clean +taos_close(taos); +taos_cleanup(); +return 0; +// ANCHOR_END: with_reqid +} + +int main(int argc, char *argv[]) { + return DemoWithReqId(); } -int main(int argc, char *argv[]) { return DemoWithReqId(); } From 4d595d28c1ec8a79a0726aa7e9296074c190e694 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 15:54:49 +0800 Subject: [PATCH 2/8] c language sample optimization --- docs/examples/c/tmq_demo.c | 58 +++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 30cbbdc1c0..6277d61b8a 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +// to compile: gcc -o tmq_demo tmq_demo.c -ltaos -lpthread + #include #include #include @@ -33,9 +35,9 @@ void* prepare_data(void* arg) { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == 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(); - return -1; + return NULL; } TAOS_RES* pRes; @@ -52,12 +54,12 @@ void* prepare_data(void* arg) { pRes = taos_query(pConn, buf); if (taos_errno(pRes) != 0) { - printf("Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); } - printf("Prepare data thread exit\n"); + fprintf(stdout, "Prepare data thread exit\n"); return NULL; } @@ -69,9 +71,9 @@ static int32_t msg_process(TAOS_RES* msg) { const char* dbName = tmq_get_db_name(msg); int32_t vgroupId = tmq_get_vgroup_id(msg); - printf("topic: %s\n", topicName); - printf("db: %s\n", dbName); - printf("vgroup id: %d\n", vgroupId); + fprintf(stdout, "topic: %s\n", topicName); + fprintf(stdout, "db: %s\n", dbName); + fprintf(stdout, "vgroup id: %d\n", vgroupId); while (1) { // get one row data from message @@ -87,11 +89,11 @@ static int32_t msg_process(TAOS_RES* msg) { rows++; // print the row content if (taos_print_row(buf, row, fields, numOfFields) < 0) { - printf("Failed to print row\n"); + fprintf(stderr, "Failed to print row\n"); break; } // print the precision and row content to the console - printf("precision: %d, row content: %s\n", precision, buf); + fprintf(stdout, "precision: %d, data: %s\n", precision, buf); } return rows; @@ -105,42 +107,42 @@ static int32_t init_env() { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == 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(); return -1; } TAOS_RES* pRes; // drop database if exists - printf("create database\n"); + fprintf(stdout, "Create database.\n"); pRes = taos_query(pConn, "drop topic if exists topic_meters"); if (taos_errno(pRes) != 0) { - printf("error in drop topic_meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to drop topic_meters, reason:%s\n", taos_errstr(pRes)); } taos_free_result(pRes); pRes = taos_query(pConn, "drop database if exists power"); if (taos_errno(pRes) != 0) { - printf("error in drop power, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to drop database power, reason:%s\n", taos_errstr(pRes)); } taos_free_result(pRes); // create database pRes = taos_query(pConn, "create database power precision 'ms' WAL_RETENTION_PERIOD 3600"); if (taos_errno(pRes) != 0) { - printf("error in create tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create tmqdb, reason:%s\n", taos_errstr(pRes)); goto END; } taos_free_result(pRes); // create super table - printf("create super table\n"); + fprintf(stdout, "Create super table.\n"); pRes = taos_query( pConn, "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "(groupId INT, location BINARY(24))"); if (taos_errno(pRes) != 0) { - printf("failed to create super table meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create super table meters, reason:%s\n", taos_errstr(pRes)); goto END; } @@ -155,7 +157,7 @@ END: } int32_t create_topic() { - printf("create topic\n"); + fprintf(stdout, "Create topic.\n"); TAOS_RES* pRes; const char *host = "localhost"; const char *user = "root"; @@ -163,14 +165,14 @@ int32_t create_topic() { uint16_t port = 6030; TAOS *pConn = taos_connect(host, user, password, NULL, port); if (pConn == 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(); return -1; } pRes = taos_query(pConn, "use power"); if (taos_errno(pRes) != 0) { - printf("error in use tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to use tmqdb, reason:%s\n", taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -179,7 +181,7 @@ int32_t create_topic() { pConn, "CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); if (taos_errno(pRes) != 0) { - printf("failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -189,7 +191,7 @@ int32_t create_topic() { } void tmq_commit_cb_print(tmq_t* tmq, int32_t code, void* param) { - printf("tmq_commit_cb_print() code: %d, tmq: %p, param: %p\n", code, tmq, param); + fprintf(stdout, "tmq_commit_cb_print() code: %d, tmq: %p, param: %p\n", code, tmq, param); } // ANCHOR: create_consumer_1 @@ -261,6 +263,7 @@ tmq_list_t* build_topic_list() { if (code) { // if failed, destroy the list and return NULL tmq_list_destroy(topicList); + fprintf(stderr, "Failed to create topic_list, ErrCode: 0x%x; ErrMessage: %s.\n", code, tmq_err2str(code)); return NULL; } // if success, return the list @@ -313,7 +316,9 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "Failed to seek to %d, reason:%s", (int)p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek assignment %d to beginning %ld, reason: %s.\n", i, p->begin, tmq_err2str(code)); + } else { + fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); } } @@ -347,7 +352,7 @@ void manual_commit(tmq_t* tmq) { taos_free_result(tmqmsg); break; } else { - printf("Commit offset manually successfully."); + fprintf(stdout, "Commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -386,20 +391,21 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Failed to create consumer.\n"); return -1; } - printf("Create consumer successfully.\n"); + fprintf(stdout, "Create consumer successfully.\n"); // ANCHOR_END: create_consumer_2 // ANCHOR: subscribe_3 tmq_list_t* topic_list = build_topic_list(); if (NULL == topic_list) { + fprintf(stderr, "Failed to create topic_list.\n"); return -1; } if ((code = tmq_subscribe(tmq, topic_list))) { - fprintf(stderr, "Failed to tmq_subscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to subscribe tmq_subscribe(): %s\n", tmq_err2str(code)); } else { - printf("Subscribe topics successfully.\n"); + fprintf(stdout, "Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); From 96379b8c04b83af4174da34a122631476ca73c6a Mon Sep 17 00:00:00 2001 From: t_max <1172915550@qq.com> Date: Mon, 12 Aug 2024 18:21:19 +0800 Subject: [PATCH 3/8] docs: update go example --- docs/examples/go/sqlquery/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/examples/go/sqlquery/main.go b/docs/examples/go/sqlquery/main.go index e03c2a14c4..1301c79325 100644 --- a/docs/examples/go/sqlquery/main.go +++ b/docs/examples/go/sqlquery/main.go @@ -35,7 +35,7 @@ func main() { } rowsAffected, err = res.RowsAffected() if err != nil { - log.Fatalln("Failed to get create create rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) + log.Fatalln("Failed to get create db rowsAffected, url:" + taosDSN + "; ErrMessage: " + err.Error()) } // you can check rowsAffected here fmt.Println("Create stable power.meters successfully, rowsAffected:", rowsAffected) @@ -66,7 +66,7 @@ func main() { // query data, make sure the database and table are created before rows, err := db.Query("SELECT ts, current, location FROM power.meters limit 100") if err != nil { - log.Fatal("query data failed:", err) + log.Fatal("Failed to query data from power.meters, url:" + taosDSN + "; ErrMessage: " + err.Error()) } for rows.Next() { var ( @@ -76,7 +76,7 @@ func main() { ) err = rows.Scan(&ts, ¤t, &location) if err != nil { - log.Fatal("scan data failed:", err) + log.Fatal("Failed to scan data, url:" + taosDSN + "; ErrMessage: " + err.Error()) } // you can check data here fmt.Printf("ts: %s, current: %f, location: %s\n", ts, current, location) From d9b514dfe6ce40fac0dc07906f3d2a90628365b6 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 18:28:39 +0800 Subject: [PATCH 4/8] c language sample optimization --- docs/examples/c/connect_example.c | 2 +- docs/examples/c/create_db_demo.c | 6 +-- docs/examples/c/stmt_insert_demo.c | 2 +- docs/examples/c/tmq_demo.c | 70 +++++++++++++++++------------- docs/examples/c/with_reqid_demo.c | 4 +- 5 files changed, 48 insertions(+), 36 deletions(-) diff --git a/docs/examples/c/connect_example.c b/docs/examples/c/connect_example.c index 614e828018..53d2d0d59b 100644 --- a/docs/examples/c/connect_example.c +++ b/docs/examples/c/connect_example.c @@ -12,7 +12,7 @@ int main() { uint16_t port = 6030; // 0 means use the default port TAOS *taos = taos_connect(host, user, passwd, db, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } diff --git a/docs/examples/c/create_db_demo.c b/docs/examples/c/create_db_demo.c index 14848f8237..ff1f4e62fd 100644 --- a/docs/examples/c/create_db_demo.c +++ b/docs/examples/c/create_db_demo.c @@ -34,7 +34,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -43,7 +43,7 @@ if (taos == NULL) { TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); code = taos_errno(result); if (code != 0) { - printf("Failed to create database power, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, code, taos_errstr(result)); + printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; @@ -56,7 +56,7 @@ const char* sql = "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, curre result = taos_query(taos, sql); code = taos_errno(result); if (code != 0) { - printf("Failed to create stable power.meters, Server: %s:%hu; ErrCode: 0x%x; ErrMessage: %s\n.", host, port, code, taos_errstr(result)); + printf("Failed to create stable power.meters, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; diff --git a/docs/examples/c/stmt_insert_demo.c b/docs/examples/c/stmt_insert_demo.c index 4bf794bcda..854c9f86e2 100644 --- a/docs/examples/c/stmt_insert_demo.c +++ b/docs/examples/c/stmt_insert_demo.c @@ -170,7 +170,7 @@ int main() { uint16_t port = 6030; TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); exit(EXIT_FAILURE); } diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 6277d61b8a..6a9006fc99 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -33,9 +33,10 @@ void* prepare_data(void* arg) { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); 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(); return NULL; } @@ -53,8 +54,9 @@ void* prepare_data(void* arg) { i); pRes = taos_query(pConn, buf); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to insert data to power.meters, reason: %s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to insert data to power.meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); sleep(1); @@ -105,9 +107,10 @@ static int32_t init_env() { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); 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(); return -1; } @@ -116,21 +119,24 @@ static int32_t init_env() { // drop database if exists fprintf(stdout, "Create database.\n"); pRes = taos_query(pConn, "drop topic if exists topic_meters"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to drop topic_meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); pRes = taos_query(pConn, "drop database if exists power"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to drop database power, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); // create database pRes = taos_query(pConn, "create database power precision 'ms' WAL_RETENTION_PERIOD 3600"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create tmqdb, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); goto END; } taos_free_result(pRes); @@ -141,8 +147,9 @@ static int32_t init_env() { pConn, "CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS " "(groupId INT, location BINARY(24))"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create super table meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create super table meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); goto END; } @@ -163,16 +170,18 @@ int32_t create_topic() { const char *user = "root"; const char *password = "taosdata"; uint16_t port = 6030; + int code = 0; TAOS *pConn = taos_connect(host, user, password, NULL, port); 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(); return -1; } pRes = taos_query(pConn, "use power"); + code = taos_errno(pRes); if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to use tmqdb, reason:%s\n", taos_errstr(pRes)); + fprintf(stderr, "Failed to use tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -180,8 +189,9 @@ int32_t create_topic() { pRes = taos_query( pConn, "CREATE TOPIC IF NOT EXISTS topic_meters AS SELECT ts, current, voltage, phase, groupid, location FROM meters"); - if (taos_errno(pRes) != 0) { - fprintf(stderr, "Failed to create topic topic_meters, reason:%s\n", taos_errstr(pRes)); + code = taos_errno(pRes); + if (code != 0) { + fprintf(stderr, "Failed to create topic topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); return -1; } taos_free_result(pRes); @@ -263,7 +273,7 @@ tmq_list_t* build_topic_list() { if (code) { // if failed, destroy the list and return NULL 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, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); return NULL; } // if success, return the list @@ -306,7 +316,7 @@ void consume_repeatly(tmq_t* tmq) { // get the topic assignment int32_t code = tmq_get_topic_assignment(tmq, topic_name, &pAssign, &numOfAssignment); if (code != 0 || pAssign == NULL || numOfAssignment == 0) { - fprintf(stderr, "Failed to get assignment, reason:%s", tmq_err2str(code)); + fprintf(stderr, "Failed to get assignment, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); return; } @@ -316,7 +326,7 @@ void consume_repeatly(tmq_t* tmq) { code = tmq_offset_seek(tmq, topic_name, p->vgId, p->begin); if (code != 0) { - fprintf(stderr, "Failed to seek assignment %d to beginning %ld, reason: %s.\n", i, p->begin, tmq_err2str(code)); + fprintf(stderr, "Failed to seek assignment %d to beginning %ld, ErrCode: 0x%x, ErrMessage: %s.\n", i, p->begin, code, tmq_err2str(code)); } else { fprintf(stdout, "Seek assignment %d to beginning %ld successfully.\n", i, p->begin); } @@ -347,12 +357,12 @@ void manual_commit(tmq_t* tmq) { int32_t code = tmq_commit_sync(tmq, tmqmsg); if (code) { - fprintf(stderr, "Failed to commit message: %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to commit message, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); // free the message taos_free_result(tmqmsg); break; } else { - fprintf(stdout, "Commit offset manually successfully."); + fprintf(stdout, "commit offset manually successfully."); } // free the message taos_free_result(tmqmsg); @@ -364,7 +374,7 @@ void manual_commit(tmq_t* tmq) { } // print the result: total messages and total rows consumed - fprintf(stderr, "%d msg consumed, include %d rows\n", msgCnt, totalRows); + fprintf(stderr, "%d msg consumed, include %d rows.\n", msgCnt, totalRows); } // ANCHOR_END: manual_commit @@ -381,7 +391,7 @@ int main(int argc, char* argv[]) { } if (pthread_create(&thread_id, NULL, &prepare_data, NULL)) { - fprintf(stderr, "Create thread failed\n"); + fprintf(stderr, "Failed to create thread.\n"); return 1; } @@ -403,9 +413,9 @@ int main(int argc, char* argv[]) { } if ((code = tmq_subscribe(tmq, topic_list))) { - fprintf(stderr, "Failed to subscribe tmq_subscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to subscribe topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stdout, "Subscribe topics successfully.\n"); + fprintf(stdout, "subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); @@ -421,15 +431,17 @@ int main(int argc, char* argv[]) { // unsubscribe the topic code = tmq_unsubscribe(tmq); if (code) { - fprintf(stderr, "Failed to tmq_unsubscribe(): %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to unsubscribe consumer, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); + } else { + fprintf(stderr, "consumer unsubscribed successfully.\n"); } - fprintf(stderr, "Unsubscribed consumer successfully.\n"); + // close the consumer code = tmq_consumer_close(tmq); if (code) { - fprintf(stderr, "Failed to close consumer: %s\n", tmq_err2str(code)); + fprintf(stderr, "Failed to close consumer: %s.\n", tmq_err2str(code)); } else { - fprintf(stderr, "Consumer closed successfully.\n"); + fprintf(stderr, "consumer closed successfully.\n"); } // ANCHOR_END: unsubscribe_and_close diff --git a/docs/examples/c/with_reqid_demo.c b/docs/examples/c/with_reqid_demo.c index 99f03a579e..724b682850 100644 --- a/docs/examples/c/with_reqid_demo.c +++ b/docs/examples/c/with_reqid_demo.c @@ -33,7 +33,7 @@ int code = 0; // connect TAOS *taos = taos_connect(host, user, password, NULL, port); if (taos == NULL) { - printf("Failed to connect to %s:%hu; ErrCode: 0x%x; ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); + printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); taos_cleanup(); return -1; } @@ -44,7 +44,7 @@ long reqid = 3L; TAOS_RES *result = taos_query_with_reqid(taos, sql, reqid); code = taos_errno(result); 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)); + printf("Failed to execute sql with reqId: %ld, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s\n.", reqid, host, port, code, taos_errstr(result)); taos_close(taos); taos_cleanup(); return -1; From d30cda78d436f264da6a976b936df06116b38414 Mon Sep 17 00:00:00 2001 From: Yaming Pei Date: Mon, 12 Aug 2024 19:00:37 +0800 Subject: [PATCH 5/8] mod c sample code --- docs/examples/c/tmq_demo.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/examples/c/tmq_demo.c b/docs/examples/c/tmq_demo.c index 6a9006fc99..d3285cb2d0 100644 --- a/docs/examples/c/tmq_demo.c +++ b/docs/examples/c/tmq_demo.c @@ -118,14 +118,14 @@ static int32_t init_env() { TAOS_RES* pRes; // 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); if (code != 0) { fprintf(stderr, "Failed to drop topic_meters, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); } taos_free_result(pRes); - pRes = taos_query(pConn, "drop database if exists power"); + pRes = taos_query(pConn, "DROP DATABASE IF EXISTS power"); code = taos_errno(pRes); if (code != 0) { fprintf(stderr, "Failed to drop database power, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -133,7 +133,7 @@ static int32_t init_env() { taos_free_result(pRes); // create database - 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); if (code != 0) { fprintf(stderr, "Failed to create tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -178,7 +178,7 @@ int32_t create_topic() { return -1; } - pRes = taos_query(pConn, "use power"); + pRes = taos_query(pConn, "USE POWER"); code = taos_errno(pRes); if (taos_errno(pRes) != 0) { fprintf(stderr, "Failed to use tmqdb, ErrCode: 0x%x, ErrMessage: %s.\n", code, taos_errstr(pRes)); @@ -362,7 +362,7 @@ void manual_commit(tmq_t* tmq) { taos_free_result(tmqmsg); break; } else { - fprintf(stdout, "commit offset manually successfully."); + fprintf(stdout, "Commit offset manually successfully.\n"); } // free the message taos_free_result(tmqmsg); @@ -415,7 +415,7 @@ int main(int argc, char* argv[]) { if ((code = tmq_subscribe(tmq, topic_list))) { fprintf(stderr, "Failed to subscribe topic_list, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stdout, "subscribe topics successfully.\n"); + fprintf(stdout, "Subscribe topics successfully.\n"); } tmq_list_destroy(topic_list); @@ -433,7 +433,7 @@ int main(int argc, char* argv[]) { if (code) { fprintf(stderr, "Failed to unsubscribe consumer, ErrCode: 0x%x, ErrMessage: %s.\n", code, tmq_err2str(code)); } else { - fprintf(stderr, "consumer unsubscribed successfully.\n"); + fprintf(stderr, "Consumer unsubscribed successfully.\n"); } // close the consumer @@ -441,7 +441,7 @@ int main(int argc, char* argv[]) { if (code) { fprintf(stderr, "Failed to close consumer: %s.\n", tmq_err2str(code)); } else { - fprintf(stderr, "consumer closed successfully.\n"); + fprintf(stderr, "Consumer closed successfully.\n"); } // ANCHOR_END: unsubscribe_and_close From edcc5f5b67e52453b3a42a22b6d97395153084fc Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:05:01 +0800 Subject: [PATCH 6/8] improve log --- .../rust/nativeexample/examples/tmq.rs | 15 ++++++++------- docs/examples/rust/restexample/examples/tmq.rs | 6 +++--- .../com/taosdata/example/ConsumerLoopFull.java | 18 +++++++++--------- .../taosdata/example/WsConsumerLoopFull.java | 18 +++++++++--------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/docs/examples/rust/nativeexample/examples/tmq.rs b/docs/examples/rust/nativeexample/examples/tmq.rs index 4ae71bc445..7f3bd416d1 100644 --- a/docs/examples/rust/nativeexample/examples/tmq.rs +++ b/docs/examples/rust/nativeexample/examples/tmq.rs @@ -61,7 +61,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: consume match consumer.subscribe(["topic_meters"]).await{ - Ok(_) => println!("subscribe topics successfully."), + Ok(_) => println!("Subscribe topics successfully."), Err(err) => { eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); @@ -123,7 +123,7 @@ async fn main() -> anyhow::Result<()> { } // commit offset manually when you have processed the message. match consumer.commit(offset).await{ - Ok(_) => println!("commit offset manually successfully."), + Ok(_) => println!("Commit offset manually successfully."), Err(err) => { eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); @@ -140,7 +140,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: seek_offset let assignments = consumer.assignments().await.unwrap(); - println!("assignments: {:?}", assignments); + println!("Now assignments: {:?}", assignments); // seek offset for topic_vec_assignment in assignments { @@ -163,23 +163,24 @@ async fn main() -> anyhow::Result<()> { match consumer.offset_seek(topic, vgroup_id, begin).await{ Ok(_) => (), Err(err) => { - eprintln!("seek example failed; ErrMessage: {}", err); + eprintln!("Seek example failed; ErrMessage: {}", err); return Err(err.into()); } } } 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 let assignments = consumer.assignments().await.unwrap(); - println!("after seek offset assignments: {:?}", assignments); + println!("After seek offset assignments: {:?}", assignments); // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; + println!("Consumer unsubscribed successfully."); // ANCHOR_END: unsubscribe tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index c828bd91ac..37fb9edc21 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -172,15 +172,15 @@ async fn main() -> anyhow::Result<()> { let topic_assignment = consumer.topic_assignment(topic).await; println!("topic assignment: {:?}", topic_assignment); } - println!("assignment seek to beginning successfully."); + println!("Assignment seek to beginning successfully."); // after seek offset let assignments = consumer.assignments().await.unwrap(); - println!("after seek offset assignments: {:?}", assignments); + println!("After seek offset assignments: {:?}", assignments); // ANCHOR_END: seek_offset // ANCHOR: unsubscribe consumer.unsubscribe().await; - println!("consumer unsubscribed successfully."); + println!("Consumer unsubscribed successfully."); // ANCHOR_END: unsubscribe tokio::time::sleep(Duration::from_secs(1)).await; diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java index 39782e787c..3c0798d198 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/ConsumerLoopFull.java @@ -59,7 +59,7 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -88,9 +88,9 @@ public class ConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); Set assignment = consumer.assignment(); - System.out.println("now assignment: " + JSON.toJSONString(assignment)); + System.out.println("Now assignment: " + JSON.toJSONString(assignment)); ConsumerRecords records = ConsumerRecords.emptyRecord(); // make sure we have got some data @@ -99,13 +99,13 @@ public class ConsumerLoopFull { } 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()); + System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } catch (Exception ex) { - System.out.println("seek example failed; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } // ANCHOR_END: consumer_seek @@ -128,7 +128,7 @@ public class ConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); - System.out.println("commit offset manually successfully."); + System.out.println("Commit offset manually successfully."); } } } catch (SQLException ex) { @@ -149,7 +149,7 @@ public class ConsumerLoopFull { try { // unsubscribe the consumer 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()); @@ -161,7 +161,7 @@ public class ConsumerLoopFull { finally { // close the consumer consumer.close(); - System.out.println("consumer closed successfully."); + System.out.println("Consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } diff --git a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java index 45767f8461..d7207ffe71 100644 --- a/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java +++ b/examples/JDBC/JDBCDemo/src/main/java/com/taosdata/example/WsConsumerLoopFull.java @@ -57,7 +57,7 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); for (int i = 0; i < 50; i++) { // poll data ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); @@ -86,9 +86,9 @@ public class WsConsumerLoopFull { // subscribe to the topics consumer.subscribe(topics); - System.out.println("subscribe topics successfully."); + System.out.println("Subscribe topics successfully."); Set assignment = consumer.assignment(); - System.out.println("now assignment: " + JSON.toJSONString(assignment)); + System.out.println("Now assignment: " + JSON.toJSONString(assignment)); ConsumerRecords records = ConsumerRecords.emptyRecord(); // make sure we have got some data @@ -97,13 +97,13 @@ public class WsConsumerLoopFull { } 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()); + System.out.println("Seek example failed; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } catch (Exception ex) { - System.out.println("seek example failed; ErrMessage: " + ex.getMessage()); + System.out.println("Seek example failed; ErrMessage: " + ex.getMessage()); throw new SQLException("seek example failed", ex); } // ANCHOR_END: consumer_seek @@ -126,7 +126,7 @@ public class WsConsumerLoopFull { if (!records.isEmpty()) { // after processing the data, commit the offset manually consumer.commitSync(); - System.out.println("commit offset manually successfully."); + System.out.println("Commit offset manually successfully."); } } } catch (SQLException ex) { @@ -147,7 +147,7 @@ public class WsConsumerLoopFull { try { // unsubscribe the consumer 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()); @@ -159,7 +159,7 @@ public class WsConsumerLoopFull { finally { // close the consumer consumer.close(); - System.out.println("consumer closed successfully."); + System.out.println("Consumer closed successfully."); } // ANCHOR_END: unsubscribe_data_code_piece } From 1690c81510d7de697e1d186b12210822546236f8 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:09:59 +0800 Subject: [PATCH 7/8] improve log --- docs/examples/rust/restexample/examples/tmq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 37fb9edc21..3f26414105 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -61,7 +61,7 @@ async fn main() -> anyhow::Result<()> { // ANCHOR: consume match consumer.subscribe(["topic_meters"]).await{ - Ok(_) => println!("subscribe topics successfully."), + Ok(_) => println!("Subscribe topics successfully."), Err(err) => { eprintln!("Failed to subscribe topic_meters, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into()); From 4d12ad127ee1f7ae98882890bad0b2dc7266cdd4 Mon Sep 17 00:00:00 2001 From: sheyanjie-qq <249478495@qq.com> Date: Mon, 12 Aug 2024 19:12:37 +0800 Subject: [PATCH 8/8] improve log --- docs/examples/rust/restexample/examples/tmq.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/rust/restexample/examples/tmq.rs b/docs/examples/rust/restexample/examples/tmq.rs index 3f26414105..2abc0a36da 100644 --- a/docs/examples/rust/restexample/examples/tmq.rs +++ b/docs/examples/rust/restexample/examples/tmq.rs @@ -123,7 +123,7 @@ async fn main() -> anyhow::Result<()> { } // commit offset manually when you have processed the message. match consumer.commit(offset).await{ - Ok(_) => println!("commit offset manually successfully."), + Ok(_) => println!("Commit offset manually successfully."), Err(err) => { eprintln!("Failed to commit offset manually, dsn: {}; ErrMessage: {}", dsn, err); return Err(err.into());