stream.c 7.5 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/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
19 20 21
#include <pthread.h>
#include <unistd.h>
#include <taos.h>  // include TDengine header file
H
hzcheng 已提交
22 23 24 25 26 27 28 29

typedef struct {
	char  server_ip[64];
    char  db_name[64];
    char  tbl_name[64];
} param;

int g_thread_exit_flag = 0;
30
void* insert_rows(void *sarg);
H
hzcheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

void streamCallBack(void *param, TAOS_RES *res, TAOS_ROW row)
{
  // in this simple demo, it just print out the result
  char temp[128];

  TAOS_FIELD *fields = taos_fetch_fields(res);
  int         numFields = taos_num_fields(res);

  taos_print_row(temp, row, fields, numFields);

  printf("\n%s\n", temp);
}

int main(int argc, char *argv[]) 
{
  TAOS       *taos;
  char        db_name[64];
  char        tbl_name[64];
  char        sql[1024] = { 0 };

  if (argc != 4) {
    printf("usage: %s server-ip dbname tblname\n", argv[0]);
    exit(0);
  } 

  // init TAOS
  taos_init();

  strcpy(db_name, argv[2]);
  strcpy(tbl_name, argv[3]);
  
  // create pthread to insert into row per second for stream calc
  param *t_param = (param *)malloc(sizeof(param));
  if (NULL == t_param)
  {
    printf("failed to malloc\n");
    exit(1);
  }
  memset(t_param, 0, sizeof(param)); 
  strcpy(t_param->server_ip, argv[1]);
  strcpy(t_param->db_name, db_name);
  strcpy(t_param->tbl_name, tbl_name);

  pthread_t pid;
76
  pthread_create(&pid, NULL, (void * (*)(void *))insert_rows, t_param);
H
hzcheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

  sleep(3); // waiting for database is created.
  // open connection to database
  taos = taos_connect(argv[1], "root", "taosdata", db_name, 0);
  if (taos == NULL) {
    printf("failed to connet to server:%s\n", argv[1]);
	free(t_param);
    exit(1);
  }

  // starting stream calc, 
  printf("please input stream SQL:[e.g., select count(*) from tblname interval(10s);]\n");
  #if 0
  fgets(sql, sizeof(sql), stdin);
  if (sql[0] == 0) {
    printf("input NULL stream SQL, so exit!\n");	
	free(t_param);
  	exit(1);
  }
  #endif
  strcpy(sql, "select count(*) from tblname interval(3s);");

  // param is set to NULL in this demo, it shall be set to the pointer to app context 
  TAOS_STREAM *pStream = taos_open_stream(taos, sql, streamCallBack, 0, NULL, NULL);
  if (NULL == pStream) {
    printf("failed to create stream: %s\n", taos_errstr(taos));	
	free(t_param);
  	exit(1);
  }
  
  printf("presss any key to exit\n");
  getchar();

  taos_close_stream(pStream);
  
  g_thread_exit_flag = 1;  
  pthread_join(pid, NULL);

  taos_close(taos);
  free(t_param); 

  return 0;
}


122
void* insert_rows(void *sarg)
H
hzcheng 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
  TAOS  *taos;
  char    command[1024] = { 0 };
  param  *winfo = (param * )sarg;

  if (NULL == winfo){
  	printf("para is null!\n");
    exit(1);
  }

  taos = taos_connect(winfo->server_ip, "root", "taosdata", NULL, 0);
  if (taos == NULL) {
	  printf("failed to connet to server:%s\n", winfo->server_ip);
	  exit(1);
  }
  
  // drop database
  sprintf(command, "drop database %s;", winfo->db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to drop database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }

  // create database
  sprintf(command, "create database %s;", winfo->db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to create database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }

  // use database
  sprintf(command, "use %s;", winfo->db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to use database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }

  // create table
  sprintf(command, "create table %s (ts timestamp, speed int);", winfo->tbl_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to create table, reason:%s\n", taos_errstr(taos));
    exit(1);
  }

  // insert data
  int index = 0;  
  while (1) {
  	if (g_thread_exit_flag) break;
	
  	index++;
    sprintf(command, "insert into %s values (now, %d)", winfo->tbl_name, index);
    if (taos_query(taos, command)) {
  	  printf("failed to insert row [%s], reason:%s\n", command, taos_errstr(taos));
    }
	sleep(1);
  }  

  taos_close(taos);
  return 0;
}



#if 0
int bak_main(int argc, char *argv[]) 
{
  TAOS       *taos;
  char        db_name[64];
  char        tbl_name[64];
  char        sql[1024] = { 0 };
  char        command[1024] = { 0 };

  if (argc != 4) {
    printf("usage: %s server-ip dbname tblname\n", argv[0]);
    exit(0);
  } 

  // init TAOS
  taos_init();

  // open connection to database
  taos = taos_connect(argv[1], "root", "taosdata", NULL, 0);
  if (taos == NULL) {
    printf("failed to connet to server:%s\n", argv[1]);
    exit(1);
  }

  strcpy(db_name, argv[2]);
  strcpy(tbl_name, argv[3]);
  
  // drop database
  sprintf(command, "drop database %s;", db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to drop database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  
  sprintf(command, "create database %s;", db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to create database, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  else {
	printf("create database[%s] success!\n", db_name);
  }  	

  // create table
  sprintf(command, "create table %s.%s (ts timestamp, speed int);", db_name, tbl_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to create table, reason:%s\n", taos_errstr(taos));
    exit(1);
  }
  else {
	printf("create table[%s] success!\n", tbl_name);
  }

  // create pthread to insert into row per second for stream calc
  param *t_param = (param *)malloc(sizeof(param));
  if (NULL == t_param)
  {
    printf("failed to malloc\n");
    exit(1);
  }
  memset(t_param, 0, sizeof(param));
  
  strcpy(t_param->db_name, db_name);
  strcpy(t_param->tbl_name, tbl_name);
  t_param->taos = taos_connect(argv[1], "root", "taosdata", db_name, 0);
  if (t_param->taos == NULL) {
	  printf("failed to connet to server:%s\n", argv[1]);
	  free(t_param);
	  exit(1);
  }

  pthread_t pid;
  pthread_create(&pid, NULL, insertRow, t_param);

  printf("start inserting records into the m1 table ......\n");
  sleep(5);

  // starting stream calc, 
  printf("please input stream SQL:[e.g., select count(*) from streamdb.m1 interval(10s) sliding(2s);]\n");
  fgets(sql, sizeof(sql), stdin);
  if (sql[0] == 0) {
    printf("input NULL stream SQL, so exit!\n");	
	free(t_param);
  	exit(1);
  }

  sprintf(command, "use %s", db_name);
  if (taos_query(taos, command) != 0) {
    printf("failed to use %s, reason:%s\n", db_name, taos_errstr(taos));
    exit(1);
  }
  // param is set to NULL in this demo, it shall be set to the pointer to app context 
  TAOS_STREAM *pStream = taos_open_stream(taos, sql, streamCallBack, 0, NULL, NULL);
  if (NULL == pStream) {
    printf("failed to create stream: %s\n", taos_errstr(taos));	
	free(t_param);
  	exit(1);
  }
  else {
  	printf("success to create stream\n");
  }
  
  printf("presss any key to exit\n");
  getchar();

  taos_close_stream(pStream);
  
  g_thread_exit_flag = 1;  
  pthread_join(pid, NULL);

  taos_close(taos);
  taos_close(t_param->taos);
  free(t_param); 

  return 0;
}
#endif