Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client-cpp): add basic TableModel settings & insertRelationalTablet interface #14097

Merged
merged 22 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
95bf3b3
feat(client-cpp): add sqlDialect, database, insertRelationalTablet(no…
xiangmy21 Oct 27, 2024
d5a3dba
temporary
xiangmy21 Nov 13, 2024
5489ea5
feat: add test for client-cpp insertRelationalTablet
xiangmy21 Nov 14, 2024
f572965
fix cmakes in linux
xiangmy21 Nov 14, 2024
0f96f70
fix: test files
xiangmy21 Nov 14, 2024
857f2ca
fix: multiline strings
xiangmy21 Nov 14, 2024
5cc9208
fix: unique_ptr
xiangmy21 Nov 14, 2024
58e409d
fix: test success.
xiangmy21 Nov 14, 2024
af55e9d
chore: add SessionTableModelExample.cpp
xiangmy21 Nov 14, 2024
0e08c00
Merge branch 'apache:master' into master
xiangmy21 Nov 14, 2024
d7142de
fix(client-cpp): add dataType "TIMESTAMP" parsing
xiangmy21 Nov 21, 2024
66762c4
Merge branch 'master' of github.com:xiangmy21/iotdb into dev2
xiangmy21 Nov 21, 2024
3b6aaab
Merge branch 'apache:master' into master
xiangmy21 Nov 29, 2024
a588991
refact(client-cpp): extract TableModelSession
xiangmy21 Nov 29, 2024
7381e15
fix(client-cpp): fix syntax errors.
xiangmy21 Dec 1, 2024
3d3d465
fix(client-cpp): fix many syntax errors.
xiangmy21 Dec 1, 2024
5ced356
fix(client-cpp): test reopen link
xiangmy21 Dec 1, 2024
101f8bc
Merge branch 'apache:master' into master
xiangmy21 Dec 1, 2024
1241e13
fix(client-cpp): ParameterType need to be same with DataType
xiangmy21 Dec 1, 2024
2f2019c
fix(client-cpp): type conversion in pointer.
xiangmy21 Dec 1, 2024
e613d1a
fix(client-cpp): fix pointer syntax error.
xiangmy21 Dec 2, 2024
ee08530
fix(client-cpp): adjust datatype categories.
xiangmy21 Dec 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions example/client-cpp-example/src/AlignedTimeseriesSessionExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ void insertAlignedTablet() {
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);
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();
Expand Down Expand Up @@ -212,23 +212,23 @@ void insertAlignedTablets() {
int randVal11 = rand();
int randVal12 = rand();
int randVal13 = rand();
tablet1.addValue(0, row1, &randVal11);
tablet2.addValue(0, row2, &randVal12);
tablet3.addValue(0, row3, &randVal13);
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);
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);
tablet1.addValue(2, row1, randVal31);
tablet2.addValue(2, row2, randVal32);
tablet3.addValue(2, row3, randVal33);

if (tablet1.rowSize == tablet1.maxRowNumber) {
session->insertAlignedTablets(tabletMap, true);
Expand Down Expand Up @@ -267,11 +267,11 @@ void insertNullableTabletWithAlignedTimeseries() {
int64_t randVal2 = rand();
bool randVal3 = (bool)(rand() % 2);
if (i == 0) {
tablet.addValue(i, row, &randVal1);
tablet.addValue(i, row, randVal1);
} else if (i == 1) {
tablet.addValue(i, row, &randVal2);
tablet.addValue(i, row, randVal2);
} else {
tablet.addValue(i, row, &randVal3);
tablet.addValue(i, row, randVal3);
}
// mark null value
if ((row % 3) == (unsigned int) i) {
Expand Down
20 changes: 10 additions & 10 deletions example/client-cpp-example/src/SessionExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ void insertTablet() {
tablet.timestamps[row] = time;

bool randVal1 = rand() % 2;
tablet.addValue(0, row, &randVal1);
tablet.addValue(0, row, randVal1);

int randVal2 = rand();
tablet.addValue(1, row, &randVal2);
tablet.addValue(1, row, randVal2);

float randVal3 = (float)(rand() / 99.9);
tablet.addValue(2, row, &randVal3);
tablet.addValue(2, row, randVal3);

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

int64_t randVal11 = rand();
tablet1.addValue(0, row1, &randVal11);
tablet1.addValue(0, row1, randVal11);

double randVal12 = rand() / 99.9;
tablet1.addValue(1, row1, &randVal12);
tablet1.addValue(1, row1, randVal12);

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

int64_t randVal21 = rand();
tablet2.addValue(0, row2, &randVal21);
tablet2.addValue(0, row2, randVal21);

double randVal22 = rand() / 99.9;
tablet2.addValue(1, row2, &randVal22);
tablet2.addValue(1, row2, randVal22);

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

if (tablet1.rowSize == tablet1.maxRowNumber) {
session->insertTablets(tabletMap, true);
Expand Down Expand Up @@ -292,7 +292,7 @@ void insertTabletWithNullValues() {
tablet.timestamps[row] = time;
for (int i = 0; i < 3; i++) {
int64_t randVal = rand();
tablet.addValue(i, row, &randVal);
tablet.addValue(i, row, randVal);
// mark null value
if (row % 3 == (unsigned int) i) {
tablet.bitMaps[i].mark(row);
Expand Down
212 changes: 212 additions & 0 deletions example/client-cpp-example/src/TableModelSessionExample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* 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 "TableSession.h"
#include "TableSessionBuilder.h"

using namespace std;

TableSession *session;

void insertRelationalTablet() {

vector<pair<string, TSDataType::TSDataType>> schemaList {
make_pair("region_id", TSDataType::TEXT),
make_pair("plant_id", TSDataType::TEXT),
make_pair("device_id", TSDataType::TEXT),
make_pair("model", TSDataType::TEXT),
make_pair("temperature", TSDataType::FLOAT),
make_pair("humidity", TSDataType::DOUBLE)
};

vector<ColumnCategory> columnTypes = {
ColumnCategory::ID,
ColumnCategory::ID,
ColumnCategory::ID,
ColumnCategory::ATTRIBUTE,
ColumnCategory::MEASUREMENT,
ColumnCategory::MEASUREMENT
};

Tablet tablet("table1", schemaList, columnTypes, 100);

for (int row = 0; row < 100; row++) {
int rowIndex = tablet.rowSize++;
tablet.timestamps[rowIndex] = row;
tablet.addValue("region_id", rowIndex, "1");
tablet.addValue("plant_id", rowIndex, "5");
tablet.addValue("device_id", rowIndex, "3");
tablet.addValue("model", rowIndex, "A");
tablet.addValue("temperature", rowIndex, 37.6F);
tablet.addValue("humidity", rowIndex, 111.1);
if (tablet.rowSize == tablet.maxRowNumber) {
session->insert(tablet);
tablet.reset();
}
}

if (tablet.rowSize != 0) {
session->insert(tablet);
tablet.reset();
}
}

template<typename T>
inline void Output(vector<T> &columnNames) {
for (auto &name: columnNames) {
cout << name << "\t";
}
cout << endl;
}

int main() {
try {
session = new TableSessionBuilder()
.host("127.0.0.1")
.rpcPort(6667)
.username("root")
.password("root")
.build();

cout << "Create Database db1,db2" << endl;
try {
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db1");
session->executeNonQueryStatement("CREATE DATABASE IF NOT EXISTS db2");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Use db1 as database" << endl;
try {
session->executeNonQueryStatement("USE db1");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Create Table table1,table2" << endl;
try {
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)");
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)");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Show Tables" << endl;
try {
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
Output(dataSet.getColumnNames());
while(dataSet.hasNext()) {
Output(dataSet.next());
}
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Show tables from specific database" << endl;
try {
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES FROM db1");
Output(dataSet.getColumnNames());
while(dataSet.hasNext()) {
Output(dataSet.next());
}
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "InsertTablet" << endl;
try {
insertRelationalTablet();
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Query Table Data" << endl;
try {
SessionDataSet dataSet = session->executeQueryStatement("SELECT * FROM table1"
" where region_id = '1' and plant_id in ('3', '5') and device_id = '3'");
Output(dataSet.getColumnNames());
Output(dataSet.getColumnTypeList());
while(dataSet.hasNext()) {
Output(dataSet.next());
}
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

session->close();

// specify database in constructor
session = new TableSessionBuilder()
.host("127.0.0.1")
.rpcPort(6667)
.username("root")
.password("root")
.database("db1")
.build();

cout << "Show tables from current database(db1)" << endl;
try {
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
Output(dataSet.getColumnNames());
while(dataSet.hasNext()) {
Output(dataSet.next());
}
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Change database to db2" << endl;
try {
session->executeNonQueryStatement("USE db2");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Show tables from current database(db2)" << endl;
try {
SessionDataSet dataSet = session->executeQueryStatement("SHOW TABLES");
Output(dataSet.getColumnNames());
while(dataSet.hasNext()) {
Output(dataSet.next());
}
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "Drop Database db1,db2" << endl;
try {
session->executeNonQueryStatement("DROP DATABASE db1");
session->executeNonQueryStatement("DROP DATABASE db2");
} catch (IoTDBException &e) {
cout << e.what() << endl;
}

cout << "session close\n" << endl;
session->close();

delete session;

cout << "finished!\n" << endl;
} catch (IoTDBConnectionException &e) {
cout << e.what() << endl;
} catch (IoTDBException &e) {
cout << e.what() << endl;
}
return 0;
}
16 changes: 16 additions & 0 deletions iotdb-client/client-cpp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@
<sourceFile>${project.basedir}/src/main/Session.cpp</sourceFile>
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/Session.cpp</destinationFile>
</fileSet>
<fileSet>
<sourceFile>${project.basedir}/src/main/TableSession.h</sourceFile>
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSession.h</destinationFile>
</fileSet>
<fileSet>
<sourceFile>${project.basedir}/src/main/TableSession.cpp</sourceFile>
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSession.cpp</destinationFile>
</fileSet>
<fileSet>
<sourceFile>${project.basedir}/src/main/TableSessionBuilder.h</sourceFile>
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/TableSessionBuilder.h</destinationFile>
</fileSet>
<fileSet>
<sourceFile>${project.basedir}/src/main/AbstractSessionBuilder.h</sourceFile>
<destinationFile>${project.build.directory}/build/main/generated-sources-cpp/AbstractSessionBuilder.h</destinationFile>
</fileSet>
</fileSets>
</configuration>
</execution>
Expand Down
37 changes: 37 additions & 0 deletions iotdb-client/client-cpp/src/main/AbstractSessionBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.
*/

#ifndef IOTDB_ABSTRACTSESSIONBUILDER_H
#define IOTDB_ABSTRACTSESSIONBUILDER_H

#include <string>

class AbstractSessionBuilder {
public:
std::string host = "localhost";
int rpcPort = 6667;
std::string username = "root";
std::string password = "root";
std::string zoneId = "";
int fetchSize = 10000;
std::string sqlDialect = "tree";
std::string database = "";
};

#endif // IOTDB_ABSTRACTSESSIONBUILDER_H
Loading
Loading