@@ -225,10 +225,212 @@ session = (new TableSessionBuilder())
225225
226226The sample code of using these interfaces is in:
227227
228- - `example/client-cpp-example/src/TableSessionExample .cpp`: [TableSessionExample ](https://github.com/apache/iotdb/tree/rc/2.0.1/ example/client-cpp-example/src/TableSessionExample .cpp)
228+ - `example/client-cpp-example/src/TableModelSessionExample .cpp`: [TableModelSessionExample ](https://github.com/apache/iotdb/blob/master/ example/client-cpp-example/src/TableModelSessionExample .cpp)
229229
230230If the compilation finishes successfully, the example project will be placed under `example/client-cpp-example/target`
231231
232+
233+ ```cpp
234+ #include "TableSession.h"
235+ #include "TableSessionBuilder.h"
236+
237+ using namespace std;
238+
239+ shared_ptr<TableSession> session;
240+
241+ void insertRelationalTablet() {
242+
243+ vector<pair<string, TSDataType::TSDataType>> schemaList {
244+ make_pair("region_id", TSDataType::TEXT),
245+ make_pair("plant_id", TSDataType::TEXT),
246+ make_pair("device_id", TSDataType::TEXT),
247+ make_pair("model", TSDataType::TEXT),
248+ make_pair("temperature", TSDataType::FLOAT),
249+ make_pair("humidity", TSDataType::DOUBLE)
250+ };
251+
252+ vector<ColumnCategory> columnTypes = {
253+ ColumnCategory::TAG,
254+ ColumnCategory::TAG,
255+ ColumnCategory::TAG,
256+ ColumnCategory::ATTRIBUTE,
257+ ColumnCategory::FIELD,
258+ ColumnCategory::FIELD
259+ };
260+
261+ Tablet tablet("table1", schemaList, columnTypes, 100);
262+
263+ for (int row = 0; row < 100; row++) {
264+ int rowIndex = tablet.rowSize++;
265+ tablet.timestamps[rowIndex] = row;
266+
267+ // Using index-based API is more efficient than column name lookup
268+ // Prefer: tablet.addValue(0, rowIndex, "1");
269+ // Avoid: tablet.addValue("region_id", rowIndex, "1");
270+ tablet.addValue(0, rowIndex, "1"); // region_id
271+ tablet.addValue(1, rowIndex, "5"); // plant_id
272+ tablet.addValue(2, rowIndex, "3"); // device_id
273+ tablet.addValue(3, rowIndex, "A"); // model
274+ tablet.addValue(4, rowIndex, 37.6F); // temperature
275+ tablet.addValue(5, rowIndex, 111.1); // humidity
276+ if (tablet.rowSize == tablet.maxRowNumber) {
277+ session->insert(tablet);
278+ tablet.reset();
279+ }
280+ }
281+
282+ if (tablet.rowSize != 0) {
283+ session->insert(tablet);
284+ tablet.reset();
285+ }
286+ }
287+
288+ void Output(unique_ptr<SessionDataSet> &dataSet) {
289+ for (const string &name: dataSet->getColumnNames()) {
290+ cout << name << " ";
291+ }
292+ cout << endl;
293+ while (dataSet->hasNext()) {
294+ cout << dataSet->next()->toString();
295+ }
296+ cout << endl;
297+ }
298+
299+ void OutputWithType(unique_ptr<SessionDataSet> &dataSet) {
300+ for (const string &name: dataSet->getColumnNames()) {
301+ cout << name << " ";
302+ }
303+ cout << endl;
304+ for (const string &type: dataSet->getColumnTypeList()) {
305+ cout << type << " ";
306+ }
307+ cout << endl;
308+ while (dataSet->hasNext()) {
309+ cout << dataSet->next()->toString();
310+ }
311+ cout << endl;
312+ }
313+
314+ int main() {
315+ try {
316+ session = (new TableSessionBuilder())
317+ ->host("127.0.0.1")
318+ ->rpcPort(6667)
319+ ->username("root")
320+ ->password("root")
321+ ->build();
322+
323+ cout << "[Create Database db1,db2]\n" << endl;
324+ try {
325+ session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
326+ session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2");
327+ } catch (IoTDBException &e) {
328+ cout << e.what() << endl;
329+ }
330+
331+ cout << "[Use db1 as database]\n" << endl;
332+ try {
333+ session->executeNonQueryStatement("USE db1");
334+ } catch (IoTDBException &e) {
335+ cout << e.what() << endl;
336+ }
337+
338+ cout << "[Create Table table1,table2]\n" << endl;
339+ try {
340+ session->executeNonQueryStatement("create table db1.table1(region_id STRING TAG, plant_id STRING TAG, device_id STRING TAG, model STRING ATTRIBUTE, temperature FLOAT FIELD, humidity DOUBLE FIELD) with (TTL=3600000)");
341+ session->executeNonQueryStatement("create table db2.table2(region_id STRING TAG, plant_id STRING TAG, color STRING ATTRIBUTE, temperature FLOAT FIELD, speed DOUBLE FIELD) with (TTL=6600000)");
342+ } catch (IoTDBException &e) {
343+ cout << e.what() << endl;
344+ }
345+
346+ cout << "[Show Tables]\n" << endl;
347+ try {
348+ unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
349+ Output(dataSet);
350+ } catch (IoTDBException &e) {
351+ cout << e.what() << endl;
352+ }
353+
354+ cout << "[Show tables from specific database]\n" << endl;
355+ try {
356+ unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES FROM db1");
357+ Output(dataSet);
358+ } catch (IoTDBException &e) {
359+ cout << e.what() << endl;
360+ }
361+
362+ cout << "[InsertTablet]\n" << endl;
363+ try {
364+ insertRelationalTablet();
365+ } catch (IoTDBException &e) {
366+ cout << e.what() << endl;
367+ }
368+
369+ cout << "[Query Table Data]\n" << endl;
370+ try {
371+ unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SELECT * FROM table1"
372+ " where region_id = ' 1' and plant_id in (' 3' , ' 5' ) and device_id = ' 3' ");
373+ OutputWithType(dataSet);
374+ } catch (IoTDBException &e) {
375+ cout << e.what() << endl;
376+ }
377+
378+ session->close();
379+
380+ // specify database in constructor
381+ session = (new TableSessionBuilder())
382+ ->host("127.0.0.1")
383+ ->rpcPort(6667)
384+ ->username("root")
385+ ->password("root")
386+ ->database("db1")
387+ ->build();
388+
389+ cout << "[Show tables from current database(db1)]\n" << endl;
390+ try {
391+ unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
392+ Output(dataSet);
393+ } catch (IoTDBException &e) {
394+ cout << e.what() << endl;
395+ }
396+
397+ cout << "[Change database to db2]\n" << endl;
398+ try {
399+ session->executeNonQueryStatement("USE db2");
400+ } catch (IoTDBException &e) {
401+ cout << e.what() << endl;
402+ }
403+
404+ cout << "[Show tables from current database(db2)]\n" << endl;
405+ try {
406+ unique_ptr<SessionDataSet> dataSet = session->executeQueryStatement("SHOW TABLES");
407+ Output(dataSet);
408+ } catch (IoTDBException &e) {
409+ cout << e.what() << endl;
410+ }
411+
412+ cout << "[Drop Database db1,db2]\n" << endl;
413+ try {
414+ session->executeNonQueryStatement("DROP DATABASE db1");
415+ session->executeNonQueryStatement("DROP DATABASE db2");
416+ } catch (IoTDBException &e) {
417+ cout << e.what() << endl;
418+ }
419+
420+ cout << "session close\n" << endl;
421+ session->close();
422+
423+ cout << "finished!\n" << endl;
424+ } catch (IoTDBConnectionException &e) {
425+ cout << e.what() << endl;
426+ } catch (IoTDBException &e) {
427+ cout << e.what() << endl;
428+ }
429+ return 0;
430+ }
431+ ```
432+
433+
232434## 5. FAQ
233435
234436### 5.1 on Mac
0 commit comments