demo.c 5.7 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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 subet
// to compile: gcc -o demo demo.c -ltaos

19
#include <pthread.h>
H
hzcheng 已提交
20 21 22
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
23
#include <taos.h>  // TAOS header file
H
Haojun Liao 已提交
24 25
#include <sys/time.h>
#include <inttypes.h>
H
hzcheng 已提交
26

H
[TD-98]  
hjxilinx 已提交
27
static int32_t doQuery(TAOS* taos, const char* sql) {
H
Haojun Liao 已提交
28 29 30
  struct timeval t1 = {0};
  gettimeofday(&t1, NULL);
  
H
Haojun Liao 已提交
31 32
  TAOS_RES* res = taos_query(taos, sql);
  if (taos_errno(res) != 0) {
H
Hui Li 已提交
33
    printf("failed to execute query, reason:%s\n", taos_errstr(taos));
H
[TD-98]  
hjxilinx 已提交
34 35 36 37 38 39 40 41 42
    return -1;
  }
  
  TAOS_ROW row = NULL;
  char buf[512] = {0};
  
  int32_t numOfFields = taos_num_fields(res);
  TAOS_FIELD* pFields = taos_fetch_fields(res);
  
H
Haojun Liao 已提交
43
  int32_t i = 0;
H
[TD-98]  
hjxilinx 已提交
44 45
  while((row = taos_fetch_row(res)) != NULL) {
    taos_print_row(buf, row, pFields, numOfFields);
H
Haojun Liao 已提交
46
    printf("%d:%s\n", ++i, buf);
H
[TD-98]  
hjxilinx 已提交
47 48 49 50
    memset(buf, 0, 512);
  }
  
  taos_free_result(res);
H
Haojun Liao 已提交
51 52 53 54 55
  
  struct timeval t2 = {0};
  gettimeofday(&t2, NULL);
  
  printf("elapsed time:%"PRId64 " ms\n", ((t2.tv_sec*1000000 + t2.tv_usec) - (t1.tv_sec*1000000 + t1.tv_usec))/1000);
sangshuduo's avatar
sangshuduo 已提交
56
  return 0;
H
[TD-98]  
hjxilinx 已提交
57 58
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
void* oneLoader(void* param) {
  TAOS* conn = (TAOS*) param;
  
  for(int32_t i = 0; i < 20000; ++i) {
//    doQuery(conn, "show databases");
    doQuery(conn, "use test");
//    doQuery(conn, "describe t12");
//    doQuery(conn, "show tables");
//    doQuery(conn, "create table if not exists abc (ts timestamp, k int)");
//    doQuery(conn, "select * from t12");
  }
  
  return 0;
}


static __attribute__((unused)) void multiThreadTest(int32_t numOfThreads, void* conn) {
  pthread_attr_t thattr;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
  
H
Hui Li 已提交
80
  pthread_t* threadId = (pthread_t*)malloc(sizeof(pthread_t)*(uint32_t)numOfThreads);
81 82 83 84 85 86 87 88 89
  
  for (int i = 0; i < numOfThreads; ++i) {
    pthread_create(&threadId[i], NULL, oneLoader, conn);
  }
  
  for (int32_t i = 0; i < numOfThreads; ++i) {
    pthread_join(threadId[i], NULL);
  }
  
H
Haojun Liao 已提交
90
  free(threadId);
91 92 93
  pthread_attr_destroy(&thattr);
}

H
hzcheng 已提交
94 95 96 97
int main(int argc, char *argv[]) {
  TAOS *    taos;
  char      qstr[1024];
  TAOS_RES *result;
H
hjxilinx 已提交
98
  
H
hzcheng 已提交
99 100 101 102 103
  // connect to server
  if (argc < 2) {
    printf("please input server-ip \n");
    return 0;
  }
H
hjxilinx 已提交
104
  
105
  taos_options(TSDB_OPTION_CONFIGDIR, "~/sec/cfg");
H
[TD-98]  
hjxilinx 已提交
106
  
H
hzcheng 已提交
107 108
  // init TAOS
  taos_init();
H
hjxilinx 已提交
109
  
H
hzcheng 已提交
110 111
  taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
H
Haojun Liao 已提交
112
    printf("failed to connect to server, reason:%s\n", taos_errstr(NULL));
H
hzcheng 已提交
113 114
    exit(1);
  }
H
hjxilinx 已提交
115
  
H
Haojun Liao 已提交
116 117
  printf("success to connect to server\n");
//  doQuery(taos, "select c1,count(*) from group_db0.group_mt0 where c1<8 group by c1");
H
Haojun Liao 已提交
118
//  doQuery(taos, "select * from test.m1");
H
Haojun Liao 已提交
119

120
//  multiThreadTest(1, taos);
H
Haojun Liao 已提交
121 122 123
//  doQuery(taos, "select tbname from test.m1");
//   doQuery(taos, "select max(c1), min(c2), sum(c3), avg(c4), first(c7), last(c8), first(c9) from lm2_db0.lm2_stb0 where ts >= 1537146000000 and ts <= 1543145400000 and tbname in ('lm2_tb0') interval(1s) group by t1");
//   doQuery(taos, "select max(c1), min(c2), sum(c3), avg(c4), first(c7), last(c8), first(c9) from lm2_db0.lm2_stb0 where ts >= 1537146000000 and ts <= 1543145400000 and tbname in ('lm2_tb0', 'lm2_tb1', 'lm2_tb2') interval(1s)");
H
Haojun Liao 已提交
124 125 126
  for(int32_t i = 0; i < 200; ++i) {
    doQuery(taos, "select * from lm2_db0.lm2_stb0");
  }
127
//  doQuery(taos, "create table t1(ts timestamp, k binary(12), f nchar(2))");
H
[TD-98]  
hjxilinx 已提交
128 129 130
  
  taos_close(taos);
  return 0;
H
hjxilinx 已提交
131
  
H
hzcheng 已提交
132 133 134 135 136 137
  taos_query(taos, "drop database demo");
  if (taos_query(taos, "create database demo") != 0) {
    printf("failed to create database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  printf("success to create database\n");
H
hjxilinx 已提交
138
  
H
[TD-98]  
hjxilinx 已提交
139
  
H
hzcheng 已提交
140
  taos_query(taos, "use demo");
H
hjxilinx 已提交
141
  
H
[TD-98]  
hjxilinx 已提交
142
  
H
hzcheng 已提交
143 144 145 146 147 148
  // create table
  if (taos_query(taos, "create table m1 (ts timestamp, speed int)") != 0) {
    printf("failed to create table, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  printf("success to create table\n");
H
hjxilinx 已提交
149
  
H
[TD-98]  
hjxilinx 已提交
150
  
H
hzcheng 已提交
151 152
  // sleep for one second to make sure table is created on data node
  // taosMsleep(1000);
H
hjxilinx 已提交
153
  
H
[TD-98]  
hjxilinx 已提交
154
  
H
hzcheng 已提交
155 156 157
  // insert 10 records
  int i = 0;
  for (i = 0; i < 10; ++i) {
H
huili 已提交
158
    sprintf(qstr, "insert into m1 values (%ld, %d)", 1546300800000 + i * 1000, i * 10);
H
hzcheng 已提交
159 160 161 162 163 164
    if (taos_query(taos, qstr)) {
      printf("failed to insert row: %i, reason:%s\n", i, taos_errstr(taos));
    }
    //sleep(1);
  }
  printf("success to insert rows, total %d rows\n", i);
H
hjxilinx 已提交
165
  
H
[TD-98]  
hjxilinx 已提交
166
  
H
hzcheng 已提交
167 168 169 170 171 172
  // query the records
  sprintf(qstr, "SELECT * FROM m1");
  if (taos_query(taos, qstr) != 0) {
    printf("failed to select, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
H
hjxilinx 已提交
173
  
H
hzcheng 已提交
174 175 176 177
  if (result == NULL) {
    printf("failed to get result, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
H
[TD-98]  
hjxilinx 已提交
178 179

//  TAOS_ROW    row;
H
hjxilinx 已提交
180 181
  
  TAOS_ROW    row;
H
hzcheng 已提交
182 183 184 185
  int         rows = 0;
  int         num_fields = taos_field_count(taos);
  TAOS_FIELD *fields = taos_fetch_fields(result);
  char        temp[256];
H
hjxilinx 已提交
186
  
H
[TD-98]  
hjxilinx 已提交
187
  
H
hjxilinx 已提交
188 189 190 191 192 193 194 195
  printf("select * from table, result:\n");
  // fetch the records row by row
  while ((row = taos_fetch_row(result))) {
    rows++;
    taos_print_row(temp, row, fields, num_fields);
    printf("%s\n", temp);
  }
  
H
hzcheng 已提交
196 197 198 199
  taos_free_result(result);
  printf("====demo end====\n\n");
  return getchar();
}