docs: add C schemaless and stmt example
This commit is contained in:
parent
b45c4aec1a
commit
45ca0f3256
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// TAOS standard API example. The same syntax as MySQL, but only a subset
|
||||
// to compile: gcc -o CSmlInsertDemo CSmlInsertDemo.c -ltaos
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "taos.h"
|
||||
|
||||
|
||||
static int DemoSmlInsert() {
|
||||
// ANCHOR: schemaless
|
||||
const char *ip = "localhost";
|
||||
const char *user = "root";
|
||||
const char *password = "taosdata";
|
||||
|
||||
// connect
|
||||
TAOS *taos = taos_connect(ip, user, password, NULL, 0);
|
||||
if (taos == NULL) {
|
||||
printf("failed to connect to server, reason: %s\n", taos_errstr(NULL));
|
||||
taos_cleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// create database
|
||||
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
|
||||
int code = taos_errno(result);
|
||||
if (code != 0) {
|
||||
printf("failed to create database, reason: %s\n", taos_errstr(result));
|
||||
taos_close(taos);
|
||||
taos_cleanup();
|
||||
return -1;
|
||||
}
|
||||
taos_free_result(result);
|
||||
printf("success to create database\n");
|
||||
|
||||
// use database
|
||||
result = taos_query(taos, "USE power");
|
||||
taos_free_result(result);
|
||||
|
||||
// schemaless demo data
|
||||
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 * json_demo = "{\"metric\": \"metric_json\",\"timestamp\": 1626846400,\"value\": 10.3, \"tags\": {\"groupid\": 2, \"location\": \"California.SanFrancisco\", \"id\": \"d1001\"}}";
|
||||
|
||||
// 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));
|
||||
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);
|
||||
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));
|
||||
taos_close(taos);
|
||||
taos_cleanup();
|
||||
return -1;
|
||||
}
|
||||
|
||||
rows = taos_affected_rows(result);
|
||||
printf("success to insert %d rows of schemaless telnet data\n", rows);
|
||||
taos_free_result(result);
|
||||
|
||||
// opentsdb json protocol
|
||||
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");
|
||||
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) {
|
||||
free(jsons[0]);
|
||||
printf("failed to insert schemaless json data, reason: %s\n", taos_errstr(result));
|
||||
taos_close(taos);
|
||||
taos_cleanup();
|
||||
return -1;
|
||||
}
|
||||
free(jsons[0]);
|
||||
|
||||
rows = taos_affected_rows(result);
|
||||
printf("success to insert %d rows of schemaless json data\n", rows);
|
||||
taos_free_result(result);
|
||||
|
||||
// close & clean
|
||||
taos_close(taos);
|
||||
taos_cleanup();
|
||||
return 0;
|
||||
// ANCHOR_END: schemaless
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
return DemoSmlInsert();
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
|
||||
*
|
||||
* This program is free software: you can use, redistribute, and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3
|
||||
* or later ("AGPL"), as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
// TAOS standard API example. The same syntax as MySQL, but only a subset
|
||||
// to compile: gcc -o CStmtInsertDemo CStmtInsertDemo.c -ltaos
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include "taos.h"
|
||||
|
||||
/**
|
||||
* @brief execute sql only.
|
||||
*
|
||||
* @param taos
|
||||
* @param sql
|
||||
*/
|
||||
void executeSQL(TAOS *taos, const char *sql) {
|
||||
TAOS_RES *res = taos_query(taos, sql);
|
||||
int code = taos_errno(res);
|
||||
if (code != 0) {
|
||||
printf("%s\n", taos_errstr(res));
|
||||
taos_free_result(res);
|
||||
taos_close(taos);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
taos_free_result(res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief check return status and exit program when error occur.
|
||||
*
|
||||
* @param stmt
|
||||
* @param code
|
||||
* @param msg
|
||||
*/
|
||||
void checkErrorCode(TAOS_STMT *stmt, int code, const char *msg) {
|
||||
if (code != 0) {
|
||||
printf("%s. code: %d, error: %s\n", msg,code,taos_stmt_errstr(stmt));
|
||||
taos_stmt_close(stmt);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
int64_t ts;
|
||||
float current;
|
||||
int voltage;
|
||||
float phase;
|
||||
} Row;
|
||||
|
||||
int num_of_sub_table = 10;
|
||||
int num_of_row = 10;
|
||||
/**
|
||||
* @brief insert data using stmt API
|
||||
*
|
||||
* @param taos
|
||||
*/
|
||||
void insertData(TAOS *taos) {
|
||||
// init
|
||||
TAOS_STMT *stmt = taos_stmt_init(taos);
|
||||
// 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");
|
||||
for (int i = 1; i <= num_of_sub_table; i++) {
|
||||
char table_name[20];
|
||||
sprintf(table_name, "d_bind_%d", i);
|
||||
char location[20];
|
||||
sprintf(location, "location_%d", i);
|
||||
|
||||
// set table name and tags
|
||||
TAOS_MULTI_BIND tags[2];
|
||||
// groupId
|
||||
tags[0].buffer_type = TSDB_DATA_TYPE_INT;
|
||||
tags[0].buffer_length = sizeof(int);
|
||||
tags[0].length = (int32_t *)&tags[0].buffer_length;
|
||||
tags[0].buffer = &i;
|
||||
tags[0].is_null = NULL;
|
||||
tags[0].num = 1;
|
||||
// location
|
||||
tags[1].buffer_type = TSDB_DATA_TYPE_BINARY;
|
||||
tags[1].buffer_length = strlen(location);
|
||||
tags[1].length =(int32_t *) &tags[1].buffer_length;
|
||||
tags[1].buffer = location;
|
||||
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");
|
||||
|
||||
// insert rows
|
||||
TAOS_MULTI_BIND params[4];
|
||||
// ts
|
||||
params[0].buffer_type = TSDB_DATA_TYPE_TIMESTAMP;
|
||||
params[0].buffer_length = sizeof(int64_t);
|
||||
params[0].length = (int32_t *)¶ms[0].buffer_length;
|
||||
params[0].is_null = NULL;
|
||||
params[0].num = 1;
|
||||
// current
|
||||
params[1].buffer_type = TSDB_DATA_TYPE_FLOAT;
|
||||
params[1].buffer_length = sizeof(float);
|
||||
params[1].length = (int32_t *)¶ms[1].buffer_length;
|
||||
params[1].is_null = NULL;
|
||||
params[1].num = 1;
|
||||
// voltage
|
||||
params[2].buffer_type = TSDB_DATA_TYPE_INT;
|
||||
params[2].buffer_length = sizeof(int);
|
||||
params[2].length = (int32_t *)¶ms[2].buffer_length;
|
||||
params[2].is_null = NULL;
|
||||
params[2].num = 1;
|
||||
// phase
|
||||
params[3].buffer_type = TSDB_DATA_TYPE_FLOAT;
|
||||
params[3].buffer_length = sizeof(float);
|
||||
params[3].length = (int32_t *)¶ms[3].buffer_length;
|
||||
params[3].is_null = NULL;
|
||||
params[3].num = 1;
|
||||
|
||||
for (int j = 0; j < num_of_row; j++) {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
long long milliseconds = tv.tv_sec * 1000LL + tv.tv_usec / 1000; // current timestamp in milliseconds
|
||||
int64_t ts = milliseconds + j;
|
||||
float current = (float)rand() / RAND_MAX * 30;
|
||||
int voltage = rand() % 300;
|
||||
float phase = (float)rand() / RAND_MAX;
|
||||
params[0].buffer = &ts;
|
||||
params[1].buffer = ¤t;
|
||||
params[2].buffer = &voltage;
|
||||
params[3].buffer = &phase;
|
||||
// bind param
|
||||
code = taos_stmt_bind_param(stmt, params);
|
||||
checkErrorCode(stmt, code, "failed to bind param");
|
||||
}
|
||||
// add batch
|
||||
code = taos_stmt_add_batch(stmt);
|
||||
checkErrorCode(stmt, code, "failed to add batch");
|
||||
// execute batch
|
||||
code = taos_stmt_execute(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);
|
||||
}
|
||||
taos_stmt_close(stmt);
|
||||
}
|
||||
|
||||
int main() {
|
||||
TAOS *taos = taos_connect("localhost", "root", "taosdata", NULL, 6030);
|
||||
if (taos == NULL) {
|
||||
printf("failed to connect to server\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// create database and table
|
||||
executeSQL(taos, "CREATE DATABASE IF NOT EXISTS power");
|
||||
executeSQL(taos, "USE power");
|
||||
executeSQL(taos,
|
||||
"CREATE STABLE IF NOT EXISTS power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS "
|
||||
"(groupId INT, location BINARY(24))");
|
||||
insertData(taos);
|
||||
taos_close(taos);
|
||||
taos_cleanup();
|
||||
}
|
|
@ -234,6 +234,9 @@ writer.write(lineDemo, SchemalessProtocolType.LINE, SchemalessTimestampType.NANO
|
|||
```
|
||||
</TabItem>
|
||||
<TabItem label="C" value="c">
|
||||
```c
|
||||
{{#include docs/examples/c/CSmlInsertDemo.c:schemaless}}
|
||||
```
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
|
|
@ -94,6 +94,9 @@ import TabItem from "@theme/TabItem";
|
|||
```
|
||||
</TabItem>
|
||||
<TabItem label="C" value="c">
|
||||
```c
|
||||
{{#include docs/examples/c/CStmtInsertDemo.c}}
|
||||
```
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
|
|
@ -42,7 +42,7 @@ REST 连接支持所有能运行 Go 的平台。
|
|||
}
|
||||
```
|
||||
|
||||
## TDengine DataType 和 Go DataType
|
||||
## 数据类型映射
|
||||
|
||||
| TDengine DataType | Go Type |
|
||||
|-------------------|-----------|
|
||||
|
|
|
@ -44,7 +44,7 @@ TDengine 不再支持 32 位 Windows 平台。
|
|||
|
||||
`TDengine.Connector` 会抛出异常,应用程序需要处理异常。taosc 异常类型 `TDengineError`,包含错误码和错误信息,应用程序可以根据错误码和错误信息进行处理。
|
||||
|
||||
## TDengine DataType 和 C# DataType
|
||||
## 数据类型映射
|
||||
|
||||
| TDengine DataType | C# Type |
|
||||
|-------------------|------------------|
|
||||
|
|
Loading…
Reference in New Issue