### Build and Run Example Application Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Commands to clean previous builds, configure the project with CMake, and build the example application. The resulting binary will be in the 'build' directory. ```sh rm -rf build && cmake -B build -S . && cmake --build build -j32 ``` -------------------------------- ### Example Application with ClickHouse C++ Client Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md An example C++ application demonstrating connection, table creation, data insertion, selection, and deletion using the clickhouse-cpp client. This snippet requires a running ClickHouse instance on localhost. ```cpp #include #include using namespace clickhouse; int main() { /// Initialize client connection. Client client(ClientOptions().SetHost("localhost")); /// Create a table. client.Execute("CREATE TABLE IF NOT EXISTS default.numbers (id UInt64, name String) ENGINE = Memory"); /// Insert some values. { Block block; auto id = std::make_shared(); id->Append(1); id->Append(7); auto name = std::make_shared(); name->Append("one"); name->Append("seven"); block.AppendColumn("id" , id); block.AppendColumn("name", name); client.Insert("default.numbers", block); } /// Select values inserted in the previous step. client.Select("SELECT id, name FROM default.numbers", [] (const Block& block) { for (size_t i = 0; i < block.GetRowCount(); ++i) { std::cout << block[0]->As()->At(i) << " " << block[1]->As()->At(i) << "\n"; } } ); /// Select values inserted in the previous step using external data feature /// See https://clickhouse.com/docs/engines/table-engines/special/external-data { Block block1, block2; auto id = std::make_shared(); id->Append(1); block1.AppendColumn("id" , id); auto name = std::make_shared(); name->Append("seven"); block2.AppendColumn("name", name); const std::string _1 = "_1"; const std::string _2 = "_2"; const ExternalTables external = {{_1, block1}, {_2, block2}}; client.SelectWithExternalData("SELECT id, name FROM default.numbers where id in (_1) or name in (_2)", external, [] (const Block& block) { for (size_t i = 0; i < block.GetRowCount(); ++i) { std::cout << block[0]->As()->At(i) << " " << block[1]->As()->At(i) << "\n"; } } ); } /// Delete table. client.Execute("DROP TABLE default.numbers"); return 0; } ``` -------------------------------- ### CMakeLists.txt for Example Application Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md CMake configuration file to build an application that uses the clickhouse-cpp library. It adds the library as a subdirectory and links against it. ```cmake cmake_minimum_required(VERSION 3.12) project(application-example) set(CMAKE_CXX_STANDARD 17) add_subdirectory(contribs/clickhouse-cpp) add_executable(${PROJECT_NAME} "app.cpp") target_include_directories(${PROJECT_NAME} PRIVATE contribs/clickhouse-cpp/ contribs/clickhouse-cpp/contrib/absl) target_link_libraries(${PROJECT_NAME} PRIVATE clickhouse-cpp-lib) ``` -------------------------------- ### Build ClickHouse C++ Client Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Basic build commands for the clickhouse-cpp library using CMake and Ninja. Ensure you have a C++17 compiler and CMake 3.12+ installed. ```sh $ mkdir build . $ cd build $ cmake .. [-DBUILD_TESTS=ON] $ make ``` -------------------------------- ### Batch Insertion with ClickHouse C++ Client Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Use this pattern for inserting large datasets in batches to manage memory effectively. Start the insertion, append data to blocks, send blocks, and finally end the insertion. ```cpp // Start the insertion. auto block = client->BeginInsert("INSERT INTO foo (id, name) VALUES"); // Grab the columns from the block. auto col1 = block[0]->As(); auto col2 = block[1]->As(); // Add a couple of records to the block. col1.Append(1); col1.Append(2); col2.Append("holden"); col2.Append("naomi"); // Send those records. block.RefreshRowCount(); client->SendInsertBlock(block); block.Clear(); // Add another record. col1.Append(3); col2.Append("amos"); // Send it and finish. block.RefreshRowCount(); client->EndInsert(); ``` -------------------------------- ### Configure Asynchronous Inserts with SetSetting Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Configure asynchronous insert settings programmatically using the `SetSetting` method on a `clickhouse::Query` object. This approach also requires data to be sent as SQL text format. ```cpp // Or by SetSetting clickhouse::Query query("INSERT INTO default.test VALUES(10,10)"); query.SetSetting("async_insert", clickhouse::QuerySettingsField{ "1", 1 }); query.SetSetting("wait_for_async_insert", clickhouse::QuerySettingsField{ "1", 1 }); // strong recommendation query.SetSetting("async_insert_busy_timeout_ms", clickhouse::QuerySettingsField{ "5000", 1 }); query.SetSetting("async_insert_max_data_size", clickhouse::QuerySettingsField{ "104857600", 1 }); query.SetSetting("async_insert_use_adaptive_busy_timeout", clickhouse::QuerySettingsField{ "0", 1 }); client.Execute(query); ``` -------------------------------- ### Asynchronous Inserts with Native Data Format (Not Available) Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Demonstrates that the `Insert` interface in the SDK does not support asynchronous inserts when using the native data format. Asynchronous inserts are only supported for SQL text format. ```cpp // Not available case. The Insert interface actually use the native data format clickhouse::Block block; client.Insert("default.test", block); ``` -------------------------------- ### Configure Asynchronous Inserts in clickhouse-server users.xml Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Enable asynchronous inserts for the native data format by configuring the `users.xml` file on the clickhouse-server. This requires server-side modification. ```xml 1 1 0 5000 104857600 1 ``` -------------------------------- ### Link Libraries to Target Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/tests/simple/CMakeLists.txt Links the 'clickhouse-cpp-lib' library to the 'simple-test' executable. This command specifies dependencies for the target. ```cmake TARGET_LINK_LIBRARIES (simple-test clickhouse-cpp-lib ) ``` -------------------------------- ### Define Executable Target Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/tests/simple/CMakeLists.txt Defines a new executable target named 'simple-test' and lists its source files. This is a standard CMake command for creating executables. ```cmake ADD_EXECUTABLE (simple-test ../../ut/utils.cpp main.cpp ) ``` -------------------------------- ### Add LZ4 Static Library Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/contrib/lz4/lz4/CMakeLists.txt Defines the LZ4 static library using source files. Ensure LZ4 is available in the build environment. ```cmake ADD_LIBRARY (lz4 STATIC lz4.c lz4hc.c ) ``` -------------------------------- ### Configure Asynchronous Inserts with SETTINGS Clause Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Specify asynchronous insert settings directly within the INSERT query using the SETTINGS clause. This method is suitable when sending data as SQL text format. ```cpp // You can specify the asynchronous insert settings by using the SETTINGS clause of insert queries clickhouse::Query query("INSERT INTO default.test SETTINGS async_insert=1,wait_for_async_insert=1,async_insert_busy_timeout_ms=5000,async_insert_use_adaptive_busy_timeout=0,async_insert_max_data_size=104857600 VALUES(10,10)"); client.Execute(query); ``` -------------------------------- ### Create Custom Target for Debug Info at Generate Time Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/CMakeLists.txt Creates a custom target to echo the target name and its resolved file path at generate time. This is a workaround for when the file path is not available at configure time. ```cmake string(REPLACE ":" "_" target_plain_name ${target}) add_custom_target(${target_plain_name}_print_debug_info COMMAND ${CMAKE_COMMAND} -E echo "${target} : $") add_dependencies(clickhouse-cpp-lib ${target_plain_name}_print_debug_info) ``` -------------------------------- ### Create CityHash Alias Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/contrib/cityhash/cityhash/CMakeLists.txt Creates an alias target 'cityhash::cityhash' for the 'cityhash' library. ```cmake ADD_LIBRARY (cityhash::cityhash ALIAS cityhash) ``` -------------------------------- ### Enable Asynchronous Inserts at User Level Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/README.md Enable asynchronous inserts for a specific user account by altering the user's settings. This requires appropriate ALTER USER privileges. ```sql ALTER USER insert_account SETTINGS async_insert=1,wait_for_async_insert=1,async_insert_use_adaptive_busy_timeout=0,async_insert_busy_timeout_ms=5000,async_insert_max_data_size=104857600 ``` -------------------------------- ### Define Function to Print Target Properties Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/CMakeLists.txt Defines a CMake function `print_target_properties` that accepts a target name and a list of properties to print. It uses `get_target_property` to retrieve and echo each specified property. ```cmake function(print_target_properties target) set(properties "${ARGC}") list(REMOVE_AT properties 0) foreach(prop ${properties}) get_target_property(value ${target} ${prop}) if(value) message(STATUS "${target} ${prop}: ${value}") endif() endforeach() endfunction() ``` -------------------------------- ### Create LZ4 Alias Target Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/contrib/lz4/lz4/CMakeLists.txt Creates an alias target 'lz4::lz4' for the 'lz4' static library, simplifying its usage in other CMake targets. ```cmake ADD_LIBRARY(lz4::lz4 ALIAS lz4) ``` -------------------------------- ### Conditional Library Linking for MSVC Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/tests/simple/CMakeLists.txt Conditionally links the 'Crypt32' library to the 'simple-test' executable when the build system is Microsoft Visual C++ (MSVC). This handles platform-specific dependencies. ```cmake IF (MSVC) TARGET_LINK_LIBRARIES (simple-test Crypt32) ENDIF() ``` -------------------------------- ### Define Function to Print Target Debug Info Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/CMakeLists.txt Defines a CMake function `print_target_debug_info` that calls `print_target_properties` with a predefined list of common properties relevant for debugging target configurations. ```cmake function(print_target_debug_info target) print_target_properties(${target} INCLUDE_DIRECTORIES BINARY_DIR INTERFACE_INCLUDE_DIRECTORIES INTERFACE_LINK_LIBRARIES LINK_LIBRARIES LINK_LIBRARIES_ONLY_TARGETS IMPORTED_LOCATION ) endfunction() ``` -------------------------------- ### Add CityHash Library Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/contrib/cityhash/cityhash/CMakeLists.txt Defines the CityHash library as a static library using source files. Sets the POSITION_INDEPENDENT_CODE property to ON. ```cmake ADD_LIBRARY (cityhash STATIC city.cc ) set_property(TARGET cityhash PROPERTY POSITION_INDEPENDENT_CODE ON) ``` -------------------------------- ### Call Debug Info Function for Specific Targets Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/CMakeLists.txt Calls the `print_target_debug_info` function for several common external libraries like absl::int128, cityhash::cityhash, lz4::lz4, and zstd::zstd to inspect their CMake configurations. ```cmake print_target_debug_info(absl::int128) print_target_debug_info(cityhash::cityhash) print_target_debug_info(lz4::lz4) print_target_debug_info(zstd::zstd) ``` -------------------------------- ### Set Position Independent Code for LZ4 Source: https://github.com/clickhouse/clickhouse-cpp/blob/master/contrib/lz4/lz4/CMakeLists.txt Configures the LZ4 target to generate position-independent code, which is often required for shared libraries. ```cmake set_property(TARGET lz4 PROPERTY POSITION_INDEPENDENT_CODE ON) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.