用于 ClickHouse 的 C++ 客户端库
C++17 client library for ClickHouse using the native ClickHouse protocol.
clickhouse-cpp provides a small, direct API for connecting to ClickHouse, executing SQL queries,
inserting and selecting columnar Block data, and working with ClickHouse data types from C++. It
builds on Linux, macOS, and Windows with CMake or Bazel. It supports TLS and compression.
Here is an example with recommended settings;
$ mkdir build .
$ cd build
$ cmake .. -DCH_USE_3X_API=YES
$ makeCH_USE_3X_API selects the new 3.x API. As the project evolves we bring new changes to the
API; sometimes these require breaking changes. When that happens we keep the old API
unchanged by default and hide the changes behind specific flags. To enable all of these changes
at once, we recommend enabling CH_USE_3X_API, which will be the default API in version 3.0.
Enabling this option is equivalent to setting the following individual options:
| Option | 2.x default | 3.x value | Effect when set to the 3.x value |
|---|---|---|---|
CH_MAP_BOOL_TO_UINT8 |
ON |
OFF |
ClickHouse Bool maps to clickhouse::ColumnBool instead of ColumnUInt8 |
CH_USE_ABSEIL_FOR_BIGNUM |
ON |
OFF |
128-bit integers use the self-contained implementation instead of Abseil |
CH_NON_OPTIONAL_CURRENT_ENDPOINT |
OFF |
ON |
Client::GetCurrentEndpoint() returns Endpoint by value instead of std::optional |
The individual options can still be set one at a time for a gradual migration; however, when
CH_USE_3X_API=ON is given, setting any of them to a conflicting value is a configuration error.
Please refer to the workflows for the reference on dependencies/build options
We aim to keep the public API compatible across releases within the same major version, so regular source-level upgrades should not require application code changes unless explicitly documented.
ABI compatibility is not guaranteed yet, because the library is still actively evolving. If you upgrade clickhouse-cpp, rebuild your application against the new library version, including for minor version updates.
Here a simple example (app.cpp) of an application using clickhouse-cpp
…⚠ Please note that Client instance is NOT thread-safe. I.e. you must create a separate Client for each thread or utilize some synchronization techniques. ⚠
If you wish to implement some retry logic atop of clickhouse::Client there are few simple rules to make you life easier:
clickhouse::Client::ResetConnection() before the next try.clickhouse::Client::Insert() you can reuse a block from previous try, no need to rebuild it from scratch.See https://github.com/ClickHouse/clickhouse-cpp/issues/184 for details.
See https://clickhouse.com/docs/en/cloud/bestpractices/asynchronous-inserts for details.
⚠ The asynchronous setting is different according to the clickhouse-server version. The under example with clickhouse-server version 24.8.4.13. ⚠
Our strong recommendation is to use async_insert=1,wait_for_async_insert=1 if using asynchronous inserts. Using wait_for_async_insert=0 is very risky because your INSERT client may not be aware if there are errors, and also can cause potential overload if your client continues to write quickly in a situation where the ClickHouse server needs to slow down the writes and create some backpressure in order to ensure reliability of the service.
…
1
1
0
5000
104857600
1
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暂无开放 Issues,或尚未同步最近议题。