subscribe.c 1.1 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6
// sample code for TDengine subscribe/consume API
// to compile: gcc -o subscribe subscribe.c -ltaos

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
7
#include <taos.h>  // include TDengine header file
H
hzcheng 已提交
8

weixin_48148422's avatar
weixin_48148422 已提交
9 10 11
int main(int argc, char *argv[]) {
  // init TAOS
  taos_init();
H
hzcheng 已提交
12

weixin_48148422's avatar
weixin_48148422 已提交
13 14 15
  TAOS* taos = taos_connect(argv[1], "root", "taosdata", "test", 0);
  if (taos == NULL) {
    printf("failed to connect to db, reason:%s\n", taos_errstr(taos));
H
hzcheng 已提交
16 17 18
    exit(1);
  }

weixin_48148422's avatar
weixin_48148422 已提交
19 20 21 22 23
  TAOS_SUB* tsub = taos_subscribe(taos, "select * from meters;", NULL, NULL, 0);
  if ( tsub == NULL ) {
    printf("failed to create subscription.\n");
    exit(0);
  } 
H
hzcheng 已提交
24

weixin_48148422's avatar
weixin_48148422 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  for( int i = 0; i < 3; i++ ) {
    TAOS_RES* res = taos_consume(tsub);
    TAOS_ROW    row;
    int         rows = 0;
    int         num_fields = taos_subfields_count(tsub);
    TAOS_FIELD *fields = taos_fetch_fields(res);
    char        temp[256];

    // fetch the records row by row
    while ((row = taos_fetch_row(res))) {
      rows++;
      taos_print_row(temp, row, fields, num_fields);
      printf("%s\n", temp);
    }

    printf("\n");
H
hzcheng 已提交
41 42 43 44 45 46 47
  }

  taos_unsubscribe(tsub);

  return 0;
}