Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
a8b7f1a6
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a8b7f1a6
编写于
4月 28, 2022
作者:
B
Bo Ding
提交者:
GitHub
4月 28, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: add subscribe demo for c (#12028)
* docs: add subscribe demo for c * docs: add subscribe demo for c
上级
45837967
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
68 addition
and
11 deletion
+68
-11
docs-cn/04-develop/06-subscribe.mdx
docs-cn/04-develop/06-subscribe.mdx
+6
-6
docs-cn/04-develop/_category_.yml
docs-cn/04-develop/_category_.yml
+1
-4
docs-examples/c/subscribe_demo.c
docs-examples/c/subscribe_demo.c
+61
-1
未找到文件。
docs-cn/04-develop/06-subscribe.mdx
浏览文件 @
a8b7f1a6
...
...
@@ -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: 1
597466400000
current: 12.4 voltage: 220 phase: 1 location: Beijing.Chaoyang groupid: 2
ts: 1
651146662805
current: 12.4 voltage: 220 phase: 1 location: Beijing.Chaoyang groupid: 2
```
docs-cn/04-develop/_category_.yml
浏览文件 @
a8b7f1a6
label
:
开发指南
\ No newline at end of file
link
:
slug
:
/develop
type
:
generated-index
\ No newline at end of file
docs-examples/c/subscribe_demo.c
浏览文件 @
a8b7f1a6
// 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录