Skip to content

Commit

Permalink
docs: add example code for synchronously query and write (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: ik <[email protected]>
  • Loading branch information
hiiiik authored Aug 12, 2024
1 parent 8ab6c3a commit 74f3414
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

add_subdirectory(sync)
21 changes: 21 additions & 0 deletions examples/sync/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
60 changes: 60 additions & 0 deletions examples/sync/Query.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

#include <opengemini/Client.hpp>
#include <opengemini/ClientConfigBuilder.hpp>
#include <opengemini/Exception.hpp>

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 <command> is not allowed
});
}
catch (const opengemini::Exception& exception) {
// Simply prints the exception information.
std::cout << exception << std::endl;
}
}

return 0;
}
97 changes: 97 additions & 0 deletions examples/sync/Write.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

#include <opengemini/Client.hpp>
#include <opengemini/ClientConfigBuilder.hpp>
#include <opengemini/Exception.hpp>

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 <command> 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;
}

0 comments on commit 74f3414

Please sign in to comment.