diff --git a/docs-cn/04-develop/06-subscribe.mdx b/docs-cn/04-develop/06-subscribe.mdx index 4a5b2d2832450a0370743d6ccb4602292b927fbd..d33a60d8de0a4552fa531d5e390c0faae22057fa 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 83687164926268d4ae4cbe897006dde89d2e2c7b..509a9405c42939a4819b87669a4c5b244bd29a8b 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 0c4d1b65d989a3ca81649478d25bda14e84094d6..3c6c4176f7b4914b9a50184906f220d0e8a1a248 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