百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

clickhouse-cpp

> 数据库
开源

用于 ClickHouse 的 C++ 客户端库

376 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

用于 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.

Building

Here is an example with recommended settings;

bash
$ mkdir build .
$ cd build
$ cmake .. -DCH_USE_3X_API=YES
$ make

CH_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

  • https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/linux.yml
  • https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/windows_msvc.yml
  • https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/windows_mingw.yml
  • https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/macos.yml
  • https://github.com/ClickHouse/clickhouse-cpp/blob/master/.github/workflows/bazel.yml

Backwards compatibility

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.

Including clickhouse-cpp in your project

Here a simple example (app.cpp) of an application using clickhouse-cpp

…

Thread-safety

⚠ 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. ⚠

Retries

If you wish to implement some retry logic atop of clickhouse::Client there are few simple rules to make you life easier:

  • If previous attempt threw an exception, then make sure to call clickhouse::Client::ResetConnection() before the next try.
  • For 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.

Asynchronous inserts

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.

  • Only use the SDK, do not need to change the clickhouse-server config. Asynchronous inserts only work if the data is sent as SQL text format. Here is the example.
…
  • Change the clickhouse-server users.xml, enable asynchronous inserts (available for the native data format). Here is the example.
xml

        
        
            1
            1
            0
            5000
            104857600
        

        
        
            1
        
    
  • Enabling asynchronous inserts at the user level. Ensure your login account has the privileges about ALTER USER. Then you can use insert_account for asynchronous inserts.
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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Cclickhousecpp

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库