From d930ec8b8af680fc4924f34aa23bfd027c7245ea Mon Sep 17 00:00:00 2001 From: Andrew Slabko Date: Mon, 24 Aug 2026 18:07:52 +0200 Subject: [PATCH 1/2] Add basic usage example --- .../Basic_001_SimpleConnectInsertSelect.cpp | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 examples/Basic_001_SimpleConnectInsertSelect.cpp diff --git a/examples/Basic_001_SimpleConnectInsertSelect.cpp b/examples/Basic_001_SimpleConnectInsertSelect.cpp new file mode 100644 index 00000000..e526a4ff --- /dev/null +++ b/examples/Basic_001_SimpleConnectInsertSelect.cpp @@ -0,0 +1,106 @@ +/*************************************************************************************************** + * + * Basic clickhouse-cpp usage: connect to a ClickHouse server, create a table, insert a block of + * data, then select and print the inserted rows. + * + * Before running, configure the host, username, and password below. TLS requires building + * clickhouse-cpp with WITH_OPENSSL=ON. For a plaintext connection, remove SetSSLOptions() and use + * the server's plaintext native-protocol port. + * +***************************************************************************************************/ + +#include +#include +#include +#include + +namespace ch = clickhouse; + +int main() +{ + + try { + +/*************************************************************************************************** + * Setting up the connection +***************************************************************************************************/ + + // Setup the client configuration options + auto client_options = ch::ClientOptions{} + .SetHost("you.clickhouse.host.com") // Set your clickhouse host here + .SetUser("the_user") // Set your username here + .SetPassword("the#password1") // Set your password here + .SetSSLOptions({}) // Remove this line for plain-text connection, + .SetPort(9440); // and set port to 9000 + + // Create the client instance with the options above + auto client = ch::Client{client_options}; + + +/*************************************************************************************************** + * Create a table and insert data +***************************************************************************************************/ + // Create a test table + client.Execute(R"( + CREATE OR REPLACE TABLE greetings ( + id UInt64, + message String, + language String) + ENGINE = MergeTree ORDER BY id)"); + + // Create the columns to insert into the newly created table + auto id = std::make_shared(); + auto message = std::make_shared(); + auto language = std::make_shared(); + + // Add new the records to the column + id->Append(1); + message->Append("Hello, World!"); + language->Append("English"); + + id->Append(2); + message->Append("¡Hola, Mundo!"); + language->Append("Spanish"); + + id->Append(3); + message->Append("Hallo wereld!"); + language->Append("Dutch"); + + // Form a block to send it with the insert statement + ch::Block block{}; + block.AppendColumn("id", id); + block.AppendColumn("message", message); + block.AppendColumn("language", language); + + // Insert the data into the table using ClickHouse Native protocol + client.Insert("greetings", block); + + +/*************************************************************************************************** + * Select and print data +***************************************************************************************************/ + + client.BeginSelect("SELECT id, message, language FROM greetings"); + while (auto block = client.NextBlock()) { + auto id = block->At(0)->AsStrict(); + auto message = block->At(1)->AsStrict(); + auto language = block->At(2)->AsStrict(); + + for (size_t i = 0; i < block->GetRowCount(); ++i) { + std::cout << id->At(i) << "\t" + << message->At(i) << "\t" + << language->At(i) << "\n"; + } + } + + return EXIT_SUCCESS; + } + catch (const std::exception & ex) { + std::cerr << "Exception: " << ex.what() << "\n"; + return EXIT_FAILURE; + } + catch (...) { + std::cerr << "Unknown exception\n"; + return EXIT_FAILURE; + } +} From aa1541e46e30795731c408b4ef742b6b5a034d63 Mon Sep 17 00:00:00 2001 From: Andrew Slabko Date: Mon, 24 Aug 2026 18:17:23 +0200 Subject: [PATCH 2/2] Strip comments from basic usage example (for automated processing) --- .../Basic_001_SimpleConnectInsertSelect.cpp | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/examples/Basic_001_SimpleConnectInsertSelect.cpp b/examples/Basic_001_SimpleConnectInsertSelect.cpp index e526a4ff..47efbeff 100644 --- a/examples/Basic_001_SimpleConnectInsertSelect.cpp +++ b/examples/Basic_001_SimpleConnectInsertSelect.cpp @@ -1,14 +1,3 @@ -/*************************************************************************************************** - * - * Basic clickhouse-cpp usage: connect to a ClickHouse server, create a table, insert a block of - * data, then select and print the inserted rows. - * - * Before running, configure the host, username, and password below. TLS requires building - * clickhouse-cpp with WITH_OPENSSL=ON. For a plaintext connection, remove SetSSLOptions() and use - * the server's plaintext native-protocol port. - * -***************************************************************************************************/ - #include #include #include @@ -21,10 +10,6 @@ int main() try { -/*************************************************************************************************** - * Setting up the connection -***************************************************************************************************/ - // Setup the client configuration options auto client_options = ch::ClientOptions{} .SetHost("you.clickhouse.host.com") // Set your clickhouse host here @@ -37,9 +22,6 @@ int main() auto client = ch::Client{client_options}; -/*************************************************************************************************** - * Create a table and insert data -***************************************************************************************************/ // Create a test table client.Execute(R"( CREATE OR REPLACE TABLE greetings ( @@ -76,10 +58,6 @@ int main() client.Insert("greetings", block); -/*************************************************************************************************** - * Select and print data -***************************************************************************************************/ - client.BeginSelect("SELECT id, message, language FROM greetings"); while (auto block = client.NextBlock()) { auto id = block->At(0)->AsStrict();