Skip to content

Commit a1a972c

Browse files
authored
feat(client-cpp): add basic TableModel settings & insertRelationalTablet interface (#14097)
* feat(client-cpp): add sqlDialect, database, insertRelationalTablet(not tested). * temporary * feat: add test for client-cpp insertRelationalTablet * fix cmakes in linux * fix: test files * fix: multiline strings * fix: unique_ptr * fix: test success. * chore: add SessionTableModelExample.cpp * fix(client-cpp): add dataType "TIMESTAMP" parsing * refact(client-cpp): extract TableModelSession * fix(client-cpp): fix syntax errors. * fix(client-cpp): fix many syntax errors. * fix(client-cpp): test reopen link * fix(client-cpp): ParameterType need to be same with DataType * fix(client-cpp): type conversion in pointer. * fix(client-cpp): fix pointer syntax error. * fix(client-cpp): adjust datatype categories.
1 parent b3892da commit a1a972c

File tree

14 files changed

+843
-88
lines changed

14 files changed

+843
-88
lines changed

example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,9 @@ void insertAlignedTablet() {
168168
int randVal1 = 123456;
169169
double randVal2 = 123456.1234;
170170
double randVal3 = 123456.1234;
171-
tablet.addValue(0, row, &randVal1);
172-
tablet.addValue(1, row, &randVal2);
173-
tablet.addValue(2, row, &randVal3);
171+
tablet.addValue(0, row, randVal1);
172+
tablet.addValue(1, row, randVal2);
173+
tablet.addValue(2, row, randVal3);
174174
if (tablet.rowSize == tablet.maxRowNumber) {
175175
session->insertTablet(tablet, true);
176176
tablet.reset();
@@ -212,23 +212,23 @@ void insertAlignedTablets() {
212212
int randVal11 = rand();
213213
int randVal12 = rand();
214214
int randVal13 = rand();
215-
tablet1.addValue(0, row1, &randVal11);
216-
tablet2.addValue(0, row2, &randVal12);
217-
tablet3.addValue(0, row3, &randVal13);
215+
tablet1.addValue(0, row1, randVal11);
216+
tablet2.addValue(0, row2, randVal12);
217+
tablet3.addValue(0, row3, randVal13);
218218

219219
double randVal21 = rand() / 99.9;
220220
double randVal22 = rand() / 99.9;
221221
double randVal23 = rand() / 99.9;
222-
tablet1.addValue(1, row1, &randVal21);
223-
tablet2.addValue(1, row2, &randVal22);
224-
tablet3.addValue(1, row3, &randVal23);
222+
tablet1.addValue(1, row1, randVal21);
223+
tablet2.addValue(1, row2, randVal22);
224+
tablet3.addValue(1, row3, randVal23);
225225

226226
bool randVal31 = (bool)(rand() % 2);
227227
bool randVal32 = (bool)(rand() % 2);
228228
bool randVal33 = (bool)(rand() % 2);
229-
tablet1.addValue(2, row1, &randVal31);
230-
tablet2.addValue(2, row2, &randVal32);
231-
tablet3.addValue(2, row3, &randVal33);
229+
tablet1.addValue(2, row1, randVal31);
230+
tablet2.addValue(2, row2, randVal32);
231+
tablet3.addValue(2, row3, randVal33);
232232

233233
if (tablet1.rowSize == tablet1.maxRowNumber) {
234234
session->insertAlignedTablets(tabletMap, true);
@@ -267,11 +267,11 @@ void insertNullableTabletWithAlignedTimeseries() {
267267
int64_t randVal2 = rand();
268268
bool randVal3 = (bool)(rand() % 2);
269269
if (i == 0) {
270-
tablet.addValue(i, row, &randVal1);
270+
tablet.addValue(i, row, randVal1);
271271
} else if (i == 1) {
272-
tablet.addValue(i, row, &randVal2);
272+
tablet.addValue(i, row, randVal2);
273273
} else {
274-
tablet.addValue(i, row, &randVal3);
274+
tablet.addValue(i, row, randVal3);
275275
}
276276
// mark null value
277277
if ((row % 3) == (unsigned int) i) {

example/client-cpp-example/src/SessionExample.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ void insertTablet() {
159159
tablet.timestamps[row] = time;
160160

161161
bool randVal1 = rand() % 2;
162-
tablet.addValue(0, row, &randVal1);
162+
tablet.addValue(0, row, randVal1);
163163

164164
int randVal2 = rand();
165-
tablet.addValue(1, row, &randVal2);
165+
tablet.addValue(1, row, randVal2);
166166

167167
float randVal3 = (float)(rand() / 99.9);
168-
tablet.addValue(2, row, &randVal3);
168+
tablet.addValue(2, row, randVal3);
169169

170170
if (tablet.rowSize == tablet.maxRowNumber) {
171171
session->insertTablet(tablet, true);
@@ -236,22 +236,22 @@ void insertTablets() {
236236
tablet2.timestamps[row2] = time;
237237

238238
int64_t randVal11 = rand();
239-
tablet1.addValue(0, row1, &randVal11);
239+
tablet1.addValue(0, row1, randVal11);
240240

241241
double randVal12 = rand() / 99.9;
242-
tablet1.addValue(1, row1, &randVal12);
242+
tablet1.addValue(1, row1, randVal12);
243243

244244
string randVal13 = "string" + to_string(rand());
245-
tablet1.addValue(2, row1, &randVal13);
245+
tablet1.addValue(2, row1, randVal13);
246246

247247
int64_t randVal21 = rand();
248-
tablet2.addValue(0, row2, &randVal21);
248+
tablet2.addValue(0, row2, randVal21);
249249

250250
double randVal22 = rand() / 99.9;
251-
tablet2.addValue(1, row2, &randVal22);
251+
tablet2.addValue(1, row2, randVal22);
252252

253253
string randVal23 = "string" + to_string(rand());
254-
tablet2.addValue(2, row2, &randVal23);
254+
tablet2.addValue(2, row2, randVal23);
255255

256256
if (tablet1.rowSize == tablet1.maxRowNumber) {
257257
session->insertTablets(tabletMap, true);
@@ -292,7 +292,7 @@ void insertTabletWithNullValues() {
292292
tablet.timestamps[row] = time;
293293
for (int i = 0; i < 3; i++) {
294294
int64_t randVal = rand();
295-
tablet.addValue(i, row, &randVal);
295+
tablet.addValue(i, row, randVal);
296296
// mark null value
297297
if (row % 3 == (unsigned int) i) {
298298
tablet.bitMaps[i].mark(row);
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "TableSession.h"
21+
#include "TableSessionBuilder.h"
22+
23+
using namespace std;
24+
25+
TableSession *session;
26+
27+
void insertRelationalTablet() {
28+
29+
vector<pair<string, TSDataType::TSDataType>> schemaList {
30+
make_pair("region_id", TSDataType::TEXT),
31+
make_pair("plant_id", TSDataType::TEXT),
32+
make_pair("device_id", TSDataType::TEXT),
33+
make_pair("model", TSDataType::TEXT),
34+
make_pair("temperature", TSDataType::FLOAT),
35+
make_pair("humidity", TSDataType::DOUBLE)
36+
};
37+
38+
vector<ColumnCategory> columnTypes = {
39+
ColumnCategory::ID,
40+
ColumnCategory::ID,
41+
ColumnCategory::ID,
42+
ColumnCategory::ATTRIBUTE,
43+
ColumnCategory::MEASUREMENT,
44+
ColumnCategory::MEASUREMENT
45+
};
46+
47+
Tablet tablet("table1", schemaList, columnTypes, 100);
48+
49+
for (int row = 0; row < 100; row++) {
50+
int rowIndex = tablet.rowSize++;
51+
tablet.timestamps[rowIndex] = row;
52+
tablet.addValue("region_id", rowIndex, "1");
53+
tablet.addValue("plant_id", rowIndex, "5");
54+
tablet.addValue("device_id", rowIndex, "3");
55+
tablet.addValue("model", rowIndex, "A");
56+
tablet.addValue("temperature", rowIndex, 37.6F);
57+
tablet.addValue("humidity", rowIndex, 111.1);
58+
if (tablet.rowSize == tablet.maxRowNumber) {
59+
session->insert(tablet);
60+
tablet.reset();
61+
}
62+
}
63+
64+
if (tablet.rowSize != 0) {
65+
session->insert(tablet);
66+
tablet.reset();
67+
}
68+
}
69+
70+
template<typename T>
71+
inline void Output(vector<T> &columnNames) {
72+
for (auto &name: columnNames) {
73+
cout << name << "\t";
74+
}
75+
cout << endl;
76+
}
77+
78+
int main() {
79+
try {
80+
session = new TableSessionBuilder()
81+
.host("127.0.0.1")
82+
.rpcPort(6667)
83+
.username("root")
84+
.password("root")
85+
.build();
86+
87+
cout << "Create Database db1,db2" << endl;
88+
try {
89+
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
90+
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2");
91+
} catch (IoTDBException &e) {
92+
cout << e.what() << endl;
93+
}
94+
95+
cout << "Use db1 as database" << endl;
96+
try {
97+
session->executeNonQueryStatement("USE db1");
98+
} catch (IoTDBException &e) {
99+
cout << e.what() << endl;
100+
}
101+
102+
cout << "Create Table table1,table2" << endl;
103+
try {
104+
session->executeNonQueryStatement("create table db1.table1(region_id STRING ID, plant_id STRING ID, device_id STRING ID, model STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, humidity DOUBLE MEASUREMENT) with (TTL=3600000)");
105+
session->executeNonQueryStatement("create table db2.table2(region_id STRING ID, plant_id STRING ID, color STRING ATTRIBUTE, temperature FLOAT MEASUREMENT, speed DOUBLE MEASUREMENT) with (TTL=6600000)");
106+
} catch (IoTDBException &e) {
107+
cout << e.what() << endl;
108+
}
109+
110+
cout << "Show Tables" << endl;
111+
try {
112+
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
113+
Output(dataSet.getColumnNames());
114+
while(dataSet.hasNext()) {
115+
Output(dataSet.next());
116+
}
117+
} catch (IoTDBException &e) {
118+
cout << e.what() << endl;
119+
}
120+
121+
cout << "Show tables from specific database" << endl;
122+
try {
123+
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES FROM db1");
124+
Output(dataSet.getColumnNames());
125+
while(dataSet.hasNext()) {
126+
Output(dataSet.next());
127+
}
128+
} catch (IoTDBException &e) {
129+
cout << e.what() << endl;
130+
}
131+
132+
cout << "InsertTablet" << endl;
133+
try {
134+
insertRelationalTablet();
135+
} catch (IoTDBException &e) {
136+
cout << e.what() << endl;
137+
}
138+
139+
cout << "Query Table Data" << endl;
140+
try {
141+
SessionDataSet dataSet = session->executeQueryStatement("SELECT * FROM table1"
142+
" where region_id = '1' and plant_id in ('3', '5') and device_id = '3'");
143+
Output(dataSet.getColumnNames());
144+
Output(dataSet.getColumnTypeList());
145+
while(dataSet.hasNext()) {
146+
Output(dataSet.next());
147+
}
148+
} catch (IoTDBException &e) {
149+
cout << e.what() << endl;
150+
}
151+
152+
session->close();
153+
154+
// specify database in constructor
155+
session = new TableSessionBuilder()
156+
.host("127.0.0.1")
157+
.rpcPort(6667)
158+
.username("root")
159+
.password("root")
160+
.database("db1")
161+
.build();
162+
163+
cout << "Show tables from current database(db1)" << endl;
164+
try {
165+
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
166+
Output(dataSet.getColumnNames());
167+
while(dataSet.hasNext()) {
168+
Output(dataSet.next());
169+
}
170+
} catch (IoTDBException &e) {
171+
cout << e.what() << endl;
172+
}
173+
174+
cout << "Change database to db2" << endl;
175+
try {
176+
session->executeNonQueryStatement("USE db2");
177+
} catch (IoTDBException &e) {
178+
cout << e.what() << endl;
179+
}
180+
181+
cout << "Show tables from current database(db2)" << endl;
182+
try {
183+
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
184+
Output(dataSet.getColumnNames());
185+
while(dataSet.hasNext()) {
186+
Output(dataSet.next());
187+
}
188+
} catch (IoTDBException &e) {
189+
cout << e.what() << endl;
190+
}
191+
192+
cout << "Drop Database db1,db2" << endl;
193+
try {
194+
session->executeNonQueryStatement("DROP DATABASE db1");
195+
session->executeNonQueryStatement("DROP DATABASE db2");
196+
} catch (IoTDBException &e) {
197+
cout << e.what() << endl;
198+
}
199+
200+
cout << "session close\n" << endl;
201+
session->close();
202+
203+
delete session;
204+
205+
cout << "finished!\n" << endl;
206+
} catch (IoTDBConnectionException &e) {
207+
cout << e.what() << endl;
208+
} catch (IoTDBException &e) {
209+
cout << e.what() << endl;
210+
}
211+
return 0;
212+
}

iotdb-client/client-cpp/pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,22 @@
163163
<sourceFile>${project.basedir}/src/main/Session.cpp</sourceFile>
164164
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/Session.cpp</destinationFile>
165165
</fileSet>
166+
<fileSet>
167+
<sourceFile>${project.basedir}/src/main/TableSession.h</sourceFile>
168+
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSession.h</destinationFile>
169+
</fileSet>
170+
<fileSet>
171+
<sourceFile>${project.basedir}/src/main/TableSession.cpp</sourceFile>
172+
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSession.cpp</destinationFile>
173+
</fileSet>
174+
<fileSet>
175+
<sourceFile>${project.basedir}/src/main/TableSessionBuilder.h</sourceFile>
176+
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSessionBuilder.h</destinationFile>
177+
</fileSet>
178+
<fileSet>
179+
<sourceFile>${project.basedir}/src/main/AbstractSessionBuilder.h</sourceFile>
180+
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/AbstractSessionBuilder.h</destinationFile>
181+
</fileSet>
166182
</fileSets>
167183
</configuration>
168184
</execution>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#ifndef IOTDB_ABSTRACTSESSIONBUILDER_H
21+
#define IOTDB_ABSTRACTSESSIONBUILDER_H
22+
23+
#include <string>
24+
25+
class AbstractSessionBuilder {
26+
public:
27+
std::string host = "localhost";
28+
int rpcPort = 6667;
29+
std::string username = "root";
30+
std::string password = "root";
31+
std::string zoneId = "";
32+
int fetchSize = 10000;
33+
std::string sqlDialect = "tree";
34+
std::string database = "";
35+
};
36+
37+
#endif // IOTDB_ABSTRACTSESSIONBUILDER_H

0 commit comments

Comments
 (0)