-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_table.c
149 lines (130 loc) · 4.46 KB
/
create_table.c
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
/*
* Written by Yanlu Ma, 2019-Dec-20 at CENC, Beijing.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
#include <taos.h>
int main(int argc, char *argv[])
{
TAOS *taos;
int i;
size_t nt, nl;
char server_ip[64], db_name[64], stable_name[64], line[1024];
char **tbl_name, cmd[1024], net[8], sta[8], loc[8], cha[8];
FILE *fin;
if (argc != 5) {
fprintf(stderr, "Usage: %s server_ip db_name stable_name tables_"
"list.txt\n", argv[0]);
exit(0);
}
// init TAOS
taos_init();
strncpy(server_ip, argv[1], 64);
strncpy(db_name, argv[2], 64);
strncpy(stable_name, argv[3], 64);
if ((fin = fopen(argv[4], "r")) == NULL) {
fprintf(stderr, "failed to open %s!\n", argv[3]);
exit(1);
}
nt = 0;
while (fgets(line, 1024, fin) != NULL)
nt++;
if ((tbl_name = malloc(nt * sizeof(*tbl_name))) == NULL) {
fprintf(stderr, "allocation failed for tbl_name!\n");
exit(1);
}
rewind(fin);
for (i = 0; i < nt; i++) {
fgets(line, 1024, fin);
nl = strlen(line) + 1;
if ((tbl_name[i] = malloc(nl * sizeof(char))) == NULL) {
fprintf(stderr, "allocation failed for tbl_name[%d]!\n", i);
exit(1);
}
sscanf(line, "%s", tbl_name[i]);
}
fclose(fin);
taos = taos_connect(server_ip, "root", "taosdata", NULL, 0);
if (taos == NULL) {
fprintf(stderr, "failed to connet to server: %s\n", server_ip);
exit(1);
}
// drop database
sprintf(cmd, "drop database if exists %s;", db_name);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to drop database, reason: %s\n",
taos_errstr(taos));
exit(1);
}
// create database
sprintf(cmd, "create database if not exists %s;", db_name);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to create database, reason: %s\n",
taos_errstr(taos));
exit(1);
}
// use database
sprintf(cmd, "use %s;", db_name);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to use database, reason: %s\n",
taos_errstr(taos));
exit(1);
}
// create supertable
sprintf(cmd, "create table if not exists %s (ts timestamp, v int) "
"tags (network binary(8), station binary(8), "
"location binary(8), channel binary(8));",
stable_name);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to use database, reason: %s\n",
taos_errstr(taos));
exit(1);
}
sprintf(cmd, "create table if not exists %s_md (ts timestamp, "
"sn int, starttime timestamp, endtime timestamp, npts int, "
"samprate float) tags (network binary(8), station "
"binary(8), location binary(8), channel binary(8));",
stable_name);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to use database, reason: %s\n",
taos_errstr(taos));
exit(1);
}
// create tables
for (i = 0; i < nt; i++) {
printf("create table %s ...\n", tbl_name[i]);
fflush(stdout);
sscanf(tbl_name[i], "%[^.].%[^.].%[^.].%s", net, sta, loc, cha);
if (loc[0] == '-')
loc[0] = '\0';
sprintf(cmd, "create table if not exists %s_%s_%s_%s using %s "
"tags ('%s', '%s', '%s', '%s');", net, sta, loc, cha,
stable_name, net, sta, loc, cha);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to create table, reason: %s\n",
taos_errstr(taos));
exit(1);
}
}
for (i = 0; i < nt; i++) {
printf("create table %s_md ...\n", tbl_name[i]);
fflush(stdout);
sscanf(tbl_name[i], "%[^.].%[^.].%[^.].%s", net, sta, loc, cha);
if (loc[0] == '-')
loc[0] = '\0';
sprintf(cmd, "create table if not exists %s_%s_%s_%s_md using %s_md "
"tags ('%s', '%s', '%s', '%s');", net, sta, loc, cha,
stable_name, net, sta, loc, cha);
if (taos_query(taos, cmd) != 0) {
fprintf(stderr, "failed to create table, reason: %s\n",
taos_errstr(taos));
exit(1);
}
}
taos_close(taos);
return 0;
}