connect_two_cluster.c 4.4 KB
Newer Older
dengyihao's avatar
dengyihao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 76 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 122 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
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include "taos.h"
int numOfThreads = 1;

void* connectClusterAndDeal(void *arg) {
  int port = *(int *)arg;
  const char *host = "127.0.0.1";  
  const char *user = "root";  
  const char *passwd = "taosdata"; 
  TAOS* taos1 = taos_connect(host, user, passwd, "", port); 
  TAOS* taos2 = taos_connect(host, user, passwd, "", port + 1000); 
  if (NULL == taos1 || NULL == taos2) {
    printf("connect to (%d/%d) failed \n", port, port + 1000); 
    return NULL;
  } 
  TAOS_RES *result = NULL;
  result = taos_query(taos1, "drop database if exists db"); 
  if (0 != taos_errno(result)) {
    printf("failed %s\n", taos_errstr(result));
  }
  taos_free_result(result);

  taos_query(taos2, "drop database if exists db"); 
  if (0 != taos_errno(result)) {
    printf("failed %s\n", taos_errstr(result));
  }
  
  taos_free_result(result);
  // ========= build database
  {
    result = taos_query(taos1, "create database db");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    
    taos_free_result(result);
  }
  {
    result = taos_query(taos2, "create database db");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    taos_free_result(result);
  }
  
  //======== create table 
  {
    result = taos_query(taos1, "create stable db.stest (ts timestamp, port int) tags(tport int)");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    taos_free_result(result);
  }
  {
    result = taos_query(taos2, "create stable db.stest (ts timestamp, port int) tags(tport int)");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    taos_free_result(result);

  }
  //======== create table 
  {
    result = taos_query(taos1, "use db");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    taos_free_result(result);
  }
  {
    result = taos_query(taos2, "use db");
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
    taos_free_result(result);

  }
  {
    char buf[1024] = {0};
    sprintf(buf, "insert into db.t1 using stest tags(%d) values(now, %d)", port, port);
    for (int i = 0; i < 100000; i++) {
      //printf("error here\t");
      result = taos_query(taos1, buf);
    if (0 != taos_errno(result)) {
      printf("failed %s\n", taos_errstr(result));
    }
      taos_free_result(result);
      //sleep(1); 
    }
  }

  {
    char buf[1024] = {0};
    sprintf(buf, "insert into db.t1 using stest tags(%d) values(now, %d)", port + 1000, port + 1000);
    for (int i = 0; i < 100000; i++) {
      result = taos_query(taos2, buf);
      if (0 != taos_errno(result)) {
        printf("failed %s\n", taos_errstr(result));
      }
      taos_free_result(result);
      //sleep(1); 
    }
  }
  // query result
  {
    result = taos_query(taos1, "select * from stest");
    if (result == NULL || taos_errno(result) != 0) {
      printf("query failed %s\n", taos_errstr(result));
      taos_free_result(result);
    }
    TAOS_ROW row;
    int rows = 0;
    int num_fields = taos_field_count(result);
    TAOS_FIELD *fields = taos_fetch_fields(result);
    while ((row = taos_fetch_row(result))) {
      char temp[1024] = {0};
      rows++;
      taos_print_row(temp, row, fields , num_fields);
      printf("%s\n", temp);
    }
    taos_free_result(result);
  } 

  // query result
  {
    result = taos_query(taos2, "select * from stest");
    if (result == NULL || taos_errno(result) != 0) {
      printf("query failed %s\n", taos_errstr(result));
      taos_free_result(result);
    }
    TAOS_ROW row;
    int rows = 0;
    int num_fields = taos_field_count(result);
    TAOS_FIELD *fields = taos_fetch_fields(result);
    while ((row = taos_fetch_row(result))) {
      char temp[1024] = {0};
      rows++;
      taos_print_row(temp, row, fields , num_fields);
      printf("%s\n", temp);
    }
    taos_free_result(result);
  } 
  taos_close(taos1); 
  taos_close(taos2); 
  return NULL;
}
int main(int argc, char* argv[]) {
  pthread_t *pthreads = malloc(sizeof(pthread_t) * numOfThreads);   
  
  int *port = malloc(sizeof(int) * numOfThreads);
  port[0] = 6030;
  for (int i = 0; i < numOfThreads; i++) {
    pthread_create(&pthreads[i], NULL, connectClusterAndDeal, (void *)&port[i]);
  }
  for (int i = 0; i < numOfThreads; i++) {
    pthread_join(pthreads[i], NULL);
  }
  free(port);
}