c language sample optimization

This commit is contained in:
Yaming Pei 2024-08-12 15:01:34 +08:00
parent 89244490ee
commit d4e2998e7f
8 changed files with 166 additions and 130 deletions

View File

@ -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 */

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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);

View File

@ -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(); }