-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAlignedTimeseriesSessionExample.cpp
More file actions
413 lines (342 loc) · 13 KB
/
AlignedTimeseriesSessionExample.cpp
File metadata and controls
413 lines (342 loc) · 13 KB
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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include "Session.h"
using namespace std;
Session *session;
#define DEFAULT_ROW_NUMBER 1000000
void createAlignedTimeseries() {
string alignedDeviceId = "root.db1.d1";
vector<string> measurements = {"s1", "s2", "s3"};
vector<string> alignedTimeseries = {"root.db1.d1.s1", "root.db1.d1.s2", "root.db1.d1.s3"};
vector<TSDataType::TSDataType> dataTypes = {TSDataType::INT32, TSDataType::DOUBLE, TSDataType::BOOLEAN};
vector<TSEncoding::TSEncoding> encodings = {TSEncoding::PLAIN, TSEncoding::GORILLA, TSEncoding::RLE};
vector<CompressionType::CompressionType> compressors = {
CompressionType::SNAPPY, CompressionType::UNCOMPRESSED, CompressionType::SNAPPY};
for (const string ×eries: alignedTimeseries) {
if (session->checkTimeseriesExists(timeseries)) {
session->deleteTimeseries(timeseries);
}
}
session->createAlignedTimeseries(alignedDeviceId, measurements, dataTypes, encodings, compressors);
}
void createSchemaTemplate() {
if (!session->checkTemplateExists("template1")) {
Template temp("template1", false);
InternalNode iNodeD99("d99", true);
MeasurementNode mNodeS1("s1", TSDataType::INT32, TSEncoding::RLE, CompressionType::SNAPPY);
MeasurementNode mNodeS2("s2", TSDataType::INT64, TSEncoding::RLE, CompressionType::SNAPPY);
MeasurementNode mNodeD99S1("s1", TSDataType::DOUBLE, TSEncoding::RLE, CompressionType::SNAPPY);
MeasurementNode mNodeD99S2("s2", TSDataType::BOOLEAN, TSEncoding::RLE, CompressionType::SNAPPY);
iNodeD99.addChild(mNodeD99S1);
iNodeD99.addChild(mNodeD99S2);
temp.addToTemplate(iNodeD99);
temp.addToTemplate(mNodeS1);
temp.addToTemplate(mNodeS2);
session->createSchemaTemplate(temp);
session->setSchemaTemplate("template1", "root.db3.d1");
}
}
void ActivateTemplate() {
session->executeNonQueryStatement("insert into root.db3.d1(timestamp,s1, s2) values(200, 1, 1);");
}
void showDevices() {
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("show devices with database");
for (const string &name: dataSet->getColumnNames()) {
cout << name << " ";
}
cout << endl;
dataSet->setFetchSize(1024);
while (dataSet->hasNext()) {
cout << dataSet->next()->toString();
}
cout << endl;
dataSet->closeOperationHandle();
}
void showTimeseries() {
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("show timeseries");
for (const string &name: dataSet->getColumnNames()) {
cout << name << " ";
}
cout << endl;
dataSet->setFetchSize(1024);
while (dataSet->hasNext()) {
cout << dataSet->next()->toString();
}
cout << endl;
dataSet->closeOperationHandle();
}
void insertAlignedRecord() {
string deviceId = "root.db1.d1";
vector<string> measurements;
measurements.emplace_back("s1");
measurements.emplace_back("s2");
measurements.emplace_back("s3");
for (int64_t time = 0; time < 10; time++) {
vector<string> values;
values.emplace_back("1");
values.emplace_back("1.0");
values.emplace_back("true");
session->insertAlignedRecord(deviceId, time, measurements, values);
}
}
void insertAlignedRecords() {
string deviceId = "root.db1.d1";
vector<string> measurements;
measurements.emplace_back("s1");
measurements.emplace_back("s2");
measurements.emplace_back("s3");
vector<string> deviceIds;
vector<vector<string>> measurementsList;
vector<vector<string>> valuesList;
vector<int64_t> timestamps;
for (int64_t time = 10; time < 20; time++) {
vector<string> values;
values.emplace_back("1");
values.emplace_back("1.0");
values.emplace_back("true");
deviceIds.push_back(deviceId);
measurementsList.push_back(measurements);
valuesList.push_back(values);
timestamps.push_back(time);
if (time != 10 && time % 10 == 0) {
session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList);
deviceIds.clear();
measurementsList.clear();
valuesList.clear();
timestamps.clear();
}
}
session->insertAlignedRecords(deviceIds, timestamps, measurementsList, valuesList);
}
void insertAlignedTablet() {
pair<string, TSDataType::TSDataType> pairA("s1", TSDataType::INT32);
pair<string, TSDataType::TSDataType> pairB("s2", TSDataType::DOUBLE);
pair<string, TSDataType::TSDataType> pairC("s3", TSDataType::DOUBLE);
vector<pair<string, TSDataType::TSDataType>> schemas;
schemas.push_back(pairA);
schemas.push_back(pairB);
schemas.push_back(pairC);
Tablet tablet("root.db2.d2", schemas, 100000);
tablet.setAligned(true);
for (int64_t time = 0; time < DEFAULT_ROW_NUMBER; time++) {
size_t row = tablet.rowSize++;
tablet.timestamps[row] = time;
int randVal1 = 123456;
double randVal2 = 123456.1234;
double randVal3 = 123456.1234;
tablet.addValue(0, row, randVal1);
tablet.addValue(1, row, randVal2);
tablet.addValue(2, row, randVal3);
if (tablet.rowSize == tablet.maxRowNumber) {
session->insertTablet(tablet, true);
tablet.reset();
}
}
if (tablet.rowSize != 0) {
session->insertTablet(tablet);
tablet.reset();
}
}
void insertAlignedTablets() {
pair<string, TSDataType::TSDataType> pairA("s1", TSDataType::INT32);
pair<string, TSDataType::TSDataType> pairB("s2", TSDataType::DOUBLE);
pair<string, TSDataType::TSDataType> pairC("s3", TSDataType::BOOLEAN);
vector<pair<string, TSDataType::TSDataType>> schemas;
schemas.push_back(pairA);
schemas.push_back(pairB);
schemas.push_back(pairC);
Tablet tablet1("root.db1.d1", schemas, 100);
Tablet tablet2("root.db1.d2", schemas, 100);
Tablet tablet3("root.db1.d3", schemas, 100);
unordered_map<string, Tablet *> tabletMap;
tabletMap["root.db1.d1"] = &tablet1;
tabletMap["root.db1.d2"] = &tablet2;
tabletMap["root.db1.d3"] = &tablet3;
for (int64_t time = 0; time < 20; time++) {
size_t row1 = tablet1.rowSize++;
size_t row2 = tablet2.rowSize++;
size_t row3 = tablet3.rowSize++;
tablet1.timestamps[row1] = time;
tablet2.timestamps[row2] = time;
tablet3.timestamps[row3] = time;
int randVal11 = rand();
int randVal12 = rand();
int randVal13 = rand();
tablet1.addValue(0, row1, randVal11);
tablet2.addValue(0, row2, randVal12);
tablet3.addValue(0, row3, randVal13);
double randVal21 = rand() / 99.9;
double randVal22 = rand() / 99.9;
double randVal23 = rand() / 99.9;
tablet1.addValue(1, row1, randVal21);
tablet2.addValue(1, row2, randVal22);
tablet3.addValue(1, row3, randVal23);
bool randVal31 = (bool)(rand() % 2);
bool randVal32 = (bool)(rand() % 2);
bool randVal33 = (bool)(rand() % 2);
tablet1.addValue(2, row1, randVal31);
tablet2.addValue(2, row2, randVal32);
tablet3.addValue(2, row3, randVal33);
if (tablet1.rowSize == tablet1.maxRowNumber) {
session->insertAlignedTablets(tabletMap, true);
tablet1.reset();
tablet2.reset();
tablet3.reset();
}
}
if (tablet1.rowSize != 0) {
session->insertAlignedTablets(tabletMap, true);
tablet1.reset();
tablet2.reset();
tablet3.reset();
}
}
void insertNullableTabletWithAlignedTimeseries() {
pair<string, TSDataType::TSDataType> pairA("s1", TSDataType::INT32);
pair<string, TSDataType::TSDataType> pairB("s2", TSDataType::INT64);
pair<string, TSDataType::TSDataType> pairC("s3", TSDataType::BOOLEAN);
vector<pair<string, TSDataType::TSDataType>> schemas;
schemas.push_back(pairA);
schemas.push_back(pairB);
schemas.push_back(pairC);
Tablet tablet("root.db1.d4", schemas, 20);
tablet.setAligned(true);
for (int64_t time = 0; time < 20; time++) {
size_t row = tablet.rowSize++;
tablet.timestamps[row] = time;
for (int i = 0; i < 3; i++) {
int randVal1 = rand();
int64_t randVal2 = rand();
bool randVal3 = (bool)(rand() % 2);
if (i == 0) {
tablet.addValue(i, row, randVal1);
} else if (i == 1) {
tablet.addValue(i, row, randVal2);
} else {
tablet.addValue(i, row, randVal3);
}
// mark null value
if ((row % 3) == (unsigned int) i) {
tablet.bitMaps[i].mark(row);
}
}
if (tablet.rowSize == tablet.maxRowNumber) {
session->insertTablet(tablet, true);
tablet.reset();
}
}
if (tablet.rowSize != 0) {
session->insertTablet(tablet);
tablet.reset();
}
}
void query() {
unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("select * from root.db1.**");
cout << "timestamp" << " ";
for (const string &name: dataSet->getColumnNames()) {
cout << name << " ";
}
cout << endl;
dataSet->setFetchSize(1024);
while (dataSet->hasNext()) {
cout << dataSet->next()->toString();
}
cout << endl;
dataSet->closeOperationHandle();
}
void deleteData() {
string path = "root.**";
int64_t deleteTime = 49;
session->deleteData(path, deleteTime);
}
void deleteTimeseries() {
vector<string> paths;
vector<string> alignedTimeseries = {"root.db1.d1.s1", "root.db1.d1.s2", "root.db1.d1.s3", "root.db1.d1.s4",
"root.db1.d2.s1", "root.db1.d2.s2", "root.db1.d2.s3",
"root.db1.d3.s1", "root.db1.d3.s2", "root.db1.d3.s3",
"root.db1.d4.s1", "root.db1.d4.s2", "root.db1.d4.s3",
"root.db2.d2.s1", "root.db2.d2.s2", "root.db2.d2.s3", };
for (const string ×eries: alignedTimeseries) {
if (session->checkTimeseriesExists(timeseries)) {
paths.push_back(timeseries);
}
}
session->deleteTimeseries(paths);
}
void deleteStorageGroups() {
vector<string> storageGroups;
storageGroups.emplace_back("root.db1");
storageGroups.emplace_back("root.db2");
session->deleteStorageGroups(storageGroups);
}
int main() {
LOG_LEVEL = LEVEL_DEBUG;
session = new Session("127.0.0.1", 6667, "root", "root");
cout << "session open\n" << endl;
session->open(false);
cout << "setStorageGroup\n" << endl;
try {
session->setStorageGroup("root.db1");
}
catch (IoTDBException &e) {
string errorMessage(e.what());
if (errorMessage.find("StorageGroupAlreadySetException") == string::npos) {
cout << errorMessage << endl;
//throw e;
}
}
cout << "createAlignedTimeseries\n" << endl;
createAlignedTimeseries();
cout << "createSchemaTemplate\n" << endl;
createSchemaTemplate();
cout << "ActivateTemplate\n" << endl;
ActivateTemplate();
cout << "showDevices\n" << endl;
showDevices();
cout << "showTimeseries\n" << endl;
showTimeseries();
cout << "insertAlignedRecord\n" << endl;
insertAlignedRecord();
cout << "insertAlignedRecords\n" << endl;
insertAlignedRecords();
cout << "insertAlignedTablet" << endl;
cout << "Insert " << DEFAULT_ROW_NUMBER << " records." << endl;
time_t now1 = time(0);
insertAlignedTablet();
time_t now2 = time(0);
time_t useTime = now2 - now1;
cout << "Use time: " << useTime << "s.\n" << endl;
cout << "insertAlignedTablets\n" << endl;
insertAlignedTablets();
cout << "insertNullableTabletWithAlignedTimeseries\n" << endl;
insertNullableTabletWithAlignedTimeseries();
cout << "query\n" << endl;
query();
cout << "deleteData\n" << endl;
deleteData();
cout << "deleteTimeseries\n" << endl;
deleteTimeseries();
cout << "deleteStorageGroups\n" << endl;
deleteStorageGroups();
cout << "session close\n" << endl;
session->close();
delete session;
cout << "finished\n" << endl;
return 0;
}