subscribe.c 2.3 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

9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

void print_result(TAOS_RES* res) {
  TAOS_ROW    row;
  int         num_fields = taos_num_fields(res);
  TAOS_FIELD* fields = taos_fetch_fields(res);

  while ((row = taos_fetch_row(res))) {
    char temp[256];
    taos_print_row(temp, row, fields, num_fields);
    puts(temp);
  }
}

void subscribe_callback(TAOS_SUB* tsub, TAOS_RES *res, void* param, int code) {
  print_result(res);
}


weixin_48148422's avatar
weixin_48148422 已提交
27
int main(int argc, char *argv[]) {
28 29 30
  const char* host = "127.0.0.1";
  const char* user = "root";
  const char* passwd = "taosdata";
31
  const char* sql = "select * from meters;";
weixin_48148422's avatar
weixin_48148422 已提交
32 33
  const char* topic = "test-multiple";
  int async = 1, restart = 0, keep = 1;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  TAOS_SUB* tsub = NULL;

  for (int i = 1; i < argc; i++) {
    if (strncmp(argv[i], "-h=", 3) == 0) {
      host = argv[i] + 3;
      continue;
    }
    if (strncmp(argv[i], "-u=", 3) == 0) {
      user = argv[i] + 3;
      continue;
    }
    if (strncmp(argv[i], "-p=", 3) == 0) {
      passwd = argv[i] + 3;
      continue;
    }
49 50 51 52 53 54
    if (strcmp(argv[i], "-sync") == 0) {
      async = 0;
      continue;
    }
    if (strcmp(argv[i], "-restart") == 0) {
      restart = 1;
55 56
      continue;
    }
57 58
    if (strcmp(argv[i], "-single") == 0) {
      sql = "select * from t0;";
weixin_48148422's avatar
weixin_48148422 已提交
59 60 61 62 63
      topic = "test-single";
      continue;
    }
    if (strcmp(argv[i], "-nokeep") == 0) {
      keep = 0;
64 65
      continue;
    }
66 67 68 69 70
    if (strncmp(argv[i], "-sql=", 5) == 0) {
      sql = argv[i] + 5;
      topic = "test-custom";
      continue;
    }
71 72
  }

weixin_48148422's avatar
weixin_48148422 已提交
73 74
  // init TAOS
  taos_init();
H
hzcheng 已提交
75

76
  TAOS* taos = taos_connect(host, user, passwd, "test", 0);
weixin_48148422's avatar
weixin_48148422 已提交
77 78
  if (taos == NULL) {
    printf("failed to connect to db, reason:%s\n", taos_errstr(taos));
H
hzcheng 已提交
79 80 81
    exit(1);
  }

82
  if (async) {
weixin_48148422's avatar
weixin_48148422 已提交
83
    tsub = taos_subscribe(taos, restart, topic, sql, subscribe_callback, NULL, 1000);
84
  } else {
weixin_48148422's avatar
weixin_48148422 已提交
85
    tsub = taos_subscribe(taos, restart, topic, sql, NULL, NULL, 0);
86 87 88
  }

  if (tsub == NULL) {
weixin_48148422's avatar
weixin_48148422 已提交
89 90 91
    printf("failed to create subscription.\n");
    exit(0);
  } 
H
hzcheng 已提交
92

93 94 95
  if (async) {
    getchar();
  } else while(1) {
weixin_48148422's avatar
weixin_48148422 已提交
96
    TAOS_RES* res = taos_consume(tsub);
97 98
    print_result(res);
    getchar();
H
hzcheng 已提交
99 100
  }

weixin_48148422's avatar
weixin_48148422 已提交
101
  taos_unsubscribe(tsub, keep);
H
hzcheng 已提交
102 103 104 105

  return 0;
}