未验证 提交 a8b7f1a6 编写于 作者: B Bo Ding 提交者: GitHub

docs: add subscribe demo for c (#12028)

* docs: add subscribe demo for c

* docs: add subscribe demo for c
上级 45837967
......@@ -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)
</TabItem>
<TabItem label="C#" value="csharp">
<CSharp/>
</TabItem>
</TabItem> */}
<TabItem label="C" value="c">
<CDemo/>
</TabItem> */}
</TabItem>
</Tabs>
### 运行示例程序
......@@ -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
```
label: 开发指南
\ No newline at end of file
link:
slug: /develop
type: generated-index
\ No newline at end of file
// compile with:
// gcc -o subscribe_demo subscribe_demo.c -ltaos
// writing...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taos.h>
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册