Merge branch 'docs/sheyj-3.0' of github.com:taosdata/TDengine into docs/sheyj-3.0

This commit is contained in:
Yaming Pei 2024-08-12 21:12:46 +08:00
commit 05181f162d
8 changed files with 65 additions and 63 deletions

View File

@ -22,55 +22,57 @@
#include <string.h> #include <string.h>
#include "taos.h" #include "taos.h"
static int DemoCreateDB() { static int DemoCreateDB() {
// ANCHOR: create_db_and_table // ANCHOR: create_db_and_table
const char *host = "localhost"; const char *host = "localhost";
const char *user = "root"; const char *user = "root";
const char *password = "taosdata"; const char *password = "taosdata";
uint16_t port = 6030; uint16_t port = 6030;
int code = 0; int code = 0;
// connect // connect
TAOS *taos = taos_connect(host, user, password, NULL, port); TAOS *taos = taos_connect(host, user, password, NULL, port);
if (taos == NULL) { if (taos == NULL) {
printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL), taos_errstr(NULL)); printf("Failed to connect to %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, taos_errno(NULL),
taos_cleanup(); taos_errstr(NULL));
return -1; taos_cleanup();
} return -1;
}
// create database // create database
TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power"); TAOS_RES *result = taos_query(taos, "CREATE DATABASE IF NOT EXISTS power");
code = taos_errno(result); code = taos_errno(result);
if (code != 0) { if (code != 0) {
printf("Failed to create database power, Server: %s:%hu, ErrCode: 0x%x, ErrMessage: %s.\n", host, port, code, taos_errstr(result)); 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("Create database power successfully.\n");
// create table
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 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("Create stable power.meters successfully.\n");
// close & clean
taos_close(taos); taos_close(taos);
taos_cleanup(); taos_cleanup();
return -1; return 0;
} // ANCHOR_END: create_db_and_table
taos_free_result(result);
printf("Create database power successfully.\n");
// create table
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 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("Create stable meters successfully.\n");
// close & clean
taos_close(taos);
taos_cleanup();
return 0;
// ANCHOR_END: create_db_and_table
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) { return DemoCreateDB(); }
return DemoCreateDB();
}

View File

@ -66,7 +66,7 @@ async function subscribe(consumer) {
console.log("Commit offset manually successfully."); console.log("Commit offset manually successfully.");
} }
} catch (err) { } catch (err) {
console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message);
throw err; throw err;
} }
// ANCHOR_END: commit // ANCHOR_END: commit

View File

@ -18,11 +18,11 @@ async function createConsumer() {
]); ]);
try { try {
return await taos.tmqConnect(configMap); return await taos.tmqConnect(configMap);
}catch (err) { } catch (err) {
console.log(err); console.log(err);
throw err; throw err;
} }
} }
// ANCHOR_END: create_consumer // ANCHOR_END: create_consumer
@ -33,7 +33,7 @@ async function prepare() {
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 POWER ${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);
await wsSql.exec(createDB); await wsSql.exec(createDB);
await wsSql.exec(createStable); await wsSql.exec(createStable);
@ -57,9 +57,9 @@ async function subscribe(consumer) {
for (let [key, value] of res) { for (let [key, value] of res) {
console.log(key, value); console.log(key, value);
} }
} }
}catch (err) { } catch (err) {
console.error("Failed to poll data; err.code, ErrCode:" + err.code + "; ErrMessage: " + err.message); console.error("Failed to poll data; ErrCode:" + err.code + "; ErrMessage: " + err.message);
throw err; throw err;
} }
@ -76,7 +76,7 @@ async function test() {
let res = new Map(); let res = new Map();
while (res.size == 0) { while (res.size == 0) {
res = await consumer.poll(100); res = await consumer.poll(100);
} }
let assignment = await consumer.assignment(); let assignment = await consumer.assignment();
await consumer.seekToBeginning(assignment); await consumer.seekToBeginning(assignment);

View File

@ -1,7 +1,7 @@
import taos import taos
conn = None conn = None
reqId = 1 reqId = 3
host="localhost" host="localhost"
port=6030 port=6030
try: try:
@ -21,4 +21,4 @@ except Exception as err:
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -2,7 +2,7 @@ import taosrest
client = None client = None
url="http://localhost:6041" url="http://localhost:6041"
reqId = 1 reqId = 3
try: try:
client = taosrest.RestClient(url=url, client = taosrest.RestClient(url=url,
user="root", user="root",

View File

@ -1,7 +1,7 @@
import taosws import taosws
conn = None conn = None
reqId = 1 reqId = 3
host="localhost" host="localhost"
port=6041 port=6041
try: try:
@ -12,7 +12,7 @@ try:
port=port, port=port,
) )
result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=1) result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=3)
# Get data from result as list of tuple # Get data from result as list of tuple
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]}")
@ -21,4 +21,4 @@ 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}, db addr:{host}:{port} ; ErrMessage:{err}")
finally: finally:
if conn: if conn:
conn.close() conn.close()

View File

@ -42,10 +42,10 @@ public class ConsumerLoopFull {
return consumer; return consumer;
} catch (SQLException ex) { } catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create native consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); 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); 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") System.out.println("Failed to create native consumer, host: " + config.getProperty("bootstrap.servers")
+ "; ErrMessage: " + ex.getMessage()); + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex); throw new SQLException("Failed to create consumer", ex);
} }

View File

@ -40,10 +40,10 @@ public class WsConsumerLoopFull {
return consumer; return consumer;
} catch (SQLException ex) { } catch (SQLException ex) {
// handle any errors, please refer to the JDBC specifications for detailed exceptions info // handle any errors, please refer to the JDBC specifications for detailed exceptions info
System.out.println("Failed to create websocket consumer, host : " + config.getProperty("bootstrap.servers") + "; ErrCode:" + ex.getErrorCode() + "; ErrMessage: " + ex.getMessage()); 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); 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") System.out.println("Failed to create websocket consumer, host: " + config.getProperty("bootstrap.servers")
+ "; ErrMessage: " + ex.getMessage()); + "; ErrMessage: " + ex.getMessage());
throw new SQLException("Failed to create consumer", ex); throw new SQLException("Failed to create consumer", ex);
} }