diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 965688e..f8593c4 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -13,3 +13,5 @@ # See the License for the specific language governing permissions and # limitations under the License. # + +add_subdirectory(sync) diff --git a/examples/sync/CMakeLists.txt b/examples/sync/CMakeLists.txt new file mode 100644 index 0000000..025f887 --- /dev/null +++ b/examples/sync/CMakeLists.txt @@ -0,0 +1,21 @@ +# +# Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. +# +# Licensed 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. +# + +add_executable(ExampleSyncQuery Query.cpp) +add_executable(ExampleSyncWrite Write.cpp) + +target_link_libraries(ExampleSyncQuery PRIVATE ${PROJECT_NAME}::Client) +target_link_libraries(ExampleSyncWrite PRIVATE ${PROJECT_NAME}::Client) diff --git a/examples/sync/Query.cpp b/examples/sync/Query.cpp new file mode 100644 index 0000000..ed6efea --- /dev/null +++ b/examples/sync/Query.cpp @@ -0,0 +1,60 @@ +// +// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. +// +// Licensed 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. +// + +// +// Example: Query from server synchronously. +// + +#include + +#include +#include +#include + +int main(int argc, char** argv) +{ + // Constructs an openGemini client object. + opengemini::Client client{ opengemini::ClientConfigBuilder() + // At least one server endpoint needed. + .AppendAddress({ "127.0.0.1", 8086 }) + .Finalize() }; + + { + // Performs a query request and print the result, the operation will + // block until completes or an exception is thrown. + auto result = client.Query({ + "ExampleDatabase", + "select * from ExampleMeasurement", + }); + std::cout << result << std::endl; + } + + { + // Performs a query which will fail. + try { + opengemini::QueryResult result = client.Query({ + "ExampleDatabase", + {}, // Passing an empty is not allowed + }); + } + catch (const opengemini::Exception& exception) { + // Simply prints the exception information. + std::cout << exception << std::endl; + } + } + + return 0; +} diff --git a/examples/sync/Write.cpp b/examples/sync/Write.cpp new file mode 100644 index 0000000..1fa6a4f --- /dev/null +++ b/examples/sync/Write.cpp @@ -0,0 +1,97 @@ +// +// Copyright 2024 Huawei Cloud Computing Technologies Co., Ltd. +// +// Licensed 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. +// + +// +// Example: Write points synchronously. +// + +#include + +#include +#include +#include + +int main(int argc, char** argv) +{ + // Constructs an openGemini client object. + opengemini::Client client{ opengemini::ClientConfigBuilder() + // At least one server endpoint needed. + .AppendAddress({ "127.0.0.1", 8086 }) + .Finalize() }; + + { + // Writes single point to server, the operation will + // block until completes or an exception is thrown. + client.Write("ExampleDatabase", + { + "ExampleMeasurement", + { + { "Weather", "sunny" }, + { "Humidity", 521 }, + { "Temperature", 38.1 }, + }, + std::chrono::system_clock::now(), + }); + } + + { + // Constructs two points for writing. + opengemini::Point point1{ + "ExampleMeasurement", + { + { "Weather", "sunny" }, + { "Humidity", 521 }, + { "Temperature", 38.1 }, + }, + }; + opengemini::Point point2{ + "ExampleMeasurement", + { + { "Weather", "rainy" }, + { "Humidity", 333 }, + { "Temperature", 36.5 }, + }, + }; + + // Writes multiple points to server, the operation will + // block until completes or an exception is thrown. + client.Write("ExampleDatabase", + { std::move(point1), std::move(point2) }); + } + + { + // Performs a write request which will fail. + try { + opengemini::QueryResult result = client.Query({ + "ExampleDatabase", + {}, // Passing an empty is not allowed + }); + + client.Write("ExampleDatabase", + { + "ExampleMeasurement", + {}, // Passing an empty fields map is not allowed + std::chrono::system_clock::now(), + }); + } + catch (const opengemini::Exception& exception) { + // Simply print the exception information. + std::cout << exception << std::endl; + } + } + + return 0; +}