tqdemo.c 3.3 KB
Newer Older
P
Ping Xiao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

// TAOS standard API example. The same syntax as MySQL, but only a subset
P
Ping Xiao 已提交
17
// to compile: gcc -o tqdemo tqdemo.c -ltaos -lcurl
P
Ping Xiao 已提交
18 19 20 21 22 23

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <taos.h>  // TAOS header file
P
Ping Xiao 已提交
24
#include <curl/curl.h>
P
Ping Xiao 已提交
25 26

void print_results(TAOS *taos, char *qstr);
P
Ping Xiao 已提交
27
void restful_demo(char *host, char *qstr);
P
Ping Xiao 已提交
28

P
Ping Xiao 已提交
29
int main(int argc, char *argv[]) {  
P
Ping Xiao 已提交
30 31
  char qstr[1024];
  TAOS_RES *pSql = NULL;
P
Ping Xiao 已提交
32
  
P
Ping Xiao 已提交
33 34 35 36
  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
P
Ping Xiao 已提交
37
  }  
P
Ping Xiao 已提交
38 39 40 41 42 43 44 45 46 47

  TAOS *taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
    printf("failed to connect to server, reason:%s\n", "null taos" /*taos_errstr(taos)*/);
    exit(1);
  }
  
  TAOS_RES* res;
  // create topic
  res = taos_query(taos, "create topic tq_test partitions 10");
P
Ping Xiao 已提交
48
  taos_free_result(res);  
P
Ping Xiao 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

  res = taos_query(taos, "use tq_test");
  taos_free_result(res);

  print_results(taos, "show stables");
  print_results(taos, "show tables");

  // insert data
  for (int i = 0; i < 10; i++) {
    char *sql = "insert into p%d values(now + %ds, %d, 'test')";
    sprintf(qstr, sql, i, i, i);
    res = taos_query(taos, qstr);
    taos_free_result(res);
  }

  // query data
P
Ping Xiao 已提交
65
  // print_results(taos, "select * from tq_test.ps");  
P
Ping Xiao 已提交
66 67 68

  taos_close(taos);
  taos_cleanup();
P
Ping Xiao 已提交
69 70 71

  // restful demo
  restful_demo(argv[1], "show topics");
P
Ping Xiao 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
}


void print_results(TAOS *taos, char *qstr) {
  TAOS_RES* res;
  res = taos_query(taos, qstr);
  if (res == NULL || taos_errno(res) != 0) {
    printf("failed to select, reason:%s\n", taos_errstr(res));
    taos_free_result(res);
    exit(1);
  }

  TAOS_ROW    row;
  int         rows = 0;
  int         num_fields = taos_field_count(res);
  TAOS_FIELD *fields = taos_fetch_fields(res);

  printf("num_fields = %d\n", num_fields);
  printf("%s result:\n", qstr);
  // fetch the records row by row
  while ((row = taos_fetch_row(res))) {
    char temp[1024] = {0};
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }
  taos_free_result(res);
}
P
Ping Xiao 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

void restful_demo(char *host, char *qstr) {
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl == NULL) {
    printf("failed to create curl connection");
    return;
  }

  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Authorization: Basic cm9vdDp0YW9zZGF0YQ==");
  char url[1000];
  sprintf(url, "%s:6041/rest/sql", host);
  curl_easy_setopt(curl, CURLOPT_URL, url);
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);  
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, qstr);

  res = curl_easy_perform(curl);  
  curl_easy_cleanup(curl);
}