From a8b7f1a674b63553ea2b624987e45ad2a09c4231 Mon Sep 17 00:00:00 2001 From: Bo Ding Date: Thu, 28 Apr 2022 20:09:41 +0800 Subject: [PATCH] docs: add subscribe demo for c (#12028) * docs: add subscribe demo for c * docs: add subscribe demo for c --- docs-cn/04-develop/06-subscribe.mdx | 12 +++--- docs-cn/04-develop/_category_.yml | 5 +-- docs-examples/c/subscribe_demo.c | 62 ++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/docs-cn/04-develop/06-subscribe.mdx b/docs-cn/04-develop/06-subscribe.mdx index 4a5b2d2832..d33a60d8de 100644 --- a/docs-cn/04-develop/06-subscribe.mdx +++ b/docs-cn/04-develop/06-subscribe.mdx @@ -176,7 +176,7 @@ $ taos ### 准备数据 -```sql +``` # create database "power" taos> create database power; # use "power" as the database in following operations @@ -220,10 +220,10 @@ Query OK, 5 row(s) in set (0.004896s) - + */} - */} + ### 运行示例程序 @@ -240,14 +240,14 @@ ts: 1597465200000 current: 11.2 voltage: 220 phase: 1 location: Beijing.Haidian 接着,使用 taos 客户端向表中新增一条数据: -```sql +``` # taos taos> use power; -taos> insert into d1001 values("2020-08-15 12:40:00.000", 12.4, 220, 1); +taos> insert into d1001 values(now, 12.4, 220, 1); ``` 因为这条数据的电流大于 10A,示例程序会将其消费: ``` -ts: 1597466400000 current: 12.4 voltage: 220 phase: 1 location: Beijing.Chaoyang groupid: 2 +ts: 1651146662805 current: 12.4 voltage: 220 phase: 1 location: Beijing.Chaoyang groupid: 2 ``` diff --git a/docs-cn/04-develop/_category_.yml b/docs-cn/04-develop/_category_.yml index 8368716492..509a9405c4 100644 --- a/docs-cn/04-develop/_category_.yml +++ b/docs-cn/04-develop/_category_.yml @@ -1,4 +1 @@ -label: 开发指南 -link: - slug: /develop - type: generated-index \ No newline at end of file +label: 开发指南 \ No newline at end of file diff --git a/docs-examples/c/subscribe_demo.c b/docs-examples/c/subscribe_demo.c index 0c4d1b65d9..3c6c4176f7 100644 --- a/docs-examples/c/subscribe_demo.c +++ b/docs-examples/c/subscribe_demo.c @@ -1,3 +1,63 @@ // compile with: // gcc -o subscribe_demo subscribe_demo.c -ltaos -// writing... + +#include +#include +#include +#include + +int nTotalRows; + +/** + * @brief callback function of subscription. + * + * @param tsub + * @param res + * @param param. the additional parameter passed to taos_subscribe + * @param code. error code + */ +void subscribe_callback(TAOS_SUB* tsub, TAOS_RES* res, void* param, int code) { + if (code != 0) { + printf("error: %d\n", code); + exit(EXIT_FAILURE); + } + + TAOS_ROW row = NULL; + int num_fields = taos_num_fields(res); + TAOS_FIELD* fields = taos_fetch_fields(res); + int nRows = 0; + + while ((row = taos_fetch_row(res))) { + char buf[4096] = {0}; + taos_print_row(buf, row, fields, num_fields); + puts(buf); + nRows++; + } + + nTotalRows += nRows; + printf("%d rows consumed.\n", nRows); +} + +int main() { + TAOS* taos = taos_connect("localhost", "root", "taosdata", NULL, 6030); + if (taos == NULL) { + printf("failed to connect to server\n"); + exit(EXIT_FAILURE); + } + + int restart = 1; // if the topic already exists, where to subscribe from the begine. + const char* topic = "topic-meter-current-bg-10"; + const char* sql = "select * from power.meters where current > 10"; + void* param = NULL; // additional parameter. + int interval = 2000; // consumption interval in microseconds. + TAOS_SUB* tsub = taos_subscribe(taos, restart, topic, sql, subscribe_callback, NULL, interval); + + getchar(); // press Enter to stop + + printf("total rows consumed: %d\n", nTotalRows); + int keep = 0; // weather to keep subscribe process + taos_unsubscribe(tsub, keep); + + taos_close(taos); + taos_cleanup(); +} \ No newline at end of file -- GitLab