关注

编译用rust访问chdb的接口chdb-rust

项目地址:https://github.com/chdb-io/chdb-rust

  1. 拉取源代码
git clone https://github.com/chdb-io/chdb-rust
Cloning into 'chdb-rust'...
remote: Enumerating objects: 311, done.
remote: Counting objects: 100% (90/90), done.
remote: Compressing objects: 100% (60/60), done.
remote: Total 311 (delta 35), reused 40 (delta 28), pack-reused 221 (from 1)
Receiving objects: 100% (311/311), 128.00 KiB | 205.00 KiB/s, done.
Resolving deltas: 100% (122/122), done.
  1. 编译库文件
cd chdb-rust
export PATH=$PATH:/par/mold240/bin
cargo build --release
   Compiling proc-macro2 v1.0.106
   Compiling quote v1.0.44
   Compiling libc v0.2.182
...
   Compiling hyper-tls v0.5.0
   Compiling reqwest v0.11.27
   Compiling chdb-rust v1.3.0 (/par/chdb-rust)
warning: [email protected]: libchdb not found locally, attempting to download...
warning: [email protected]: Downloading libchdb from: https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz
warning: [email protected]: libchdb downloaded successfully to OUT_DIR
error: failed to run custom build command for `chdb-rust v1.3.0 (/par/chdb-rust)`

Caused by:
  process didn't exit successfully: `/par/chdb-rust/target/release/build/chdb-rust-c511778f0f9faad0/build-script-build` (exit status: 101)
  --- stdout
  cargo:warning=libchdb not found locally, attempting to download...
  cargo:warning=Downloading libchdb from: https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz
  cargo:warning=libchdb downloaded successfully to OUT_DIR
  cargo:rustc-link-search=/par/chdb-rust/target/release/build/chdb-rust-91a96fe4441444e6/out
  cargo:rustc-link-search=./
  cargo:rustc-link-search=/usr/local/lib
  cargo:rustc-link-lib=chdb
  cargo:rerun-if-changed=wrapper.h
  cargo:rerun-if-changed=build.rs
  cargo:rerun-if-env-changed=TARGET
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
  cargo:rerun-if-changed=/par/chdb-rust/target/release/build/chdb-rust-91a96fe4441444e6/out/temp_wrapper.h

  --- stderr

  thread 'main' panicked at /usr/local/cargo/registry/src/mirrors.tuna.tsinghua.edu.cn-4dc01642fd091eda/bindgen-0.70.1/lib.rs:622:27:
  Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

找不到clang库,在项目中发帖提问。被告知要安装clang库, 命令行如下

apt install libclang-dev clang

直接安装报错,apt update更新目录后可以了

docker start rust
rust
docker exec -it rust bash
export PATH=$PATH:/par/mold240/bin
apt install libclang-dev clang
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
E: Unable to locate package libclang-dev
E: Unable to locate package clang
root@DESKTOP-59T6U68:/# apt update
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:3 http://deb.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8792 kB]
Get:5 http://deb.debian.org/debian bookworm-updates/main amd64 Packages [6924 B]
Get:6 http://deb.debian.org/debian-security bookworm-security/main amd64 Packages [297 kB]
Fetched 9350 kB in 1min 29s (105 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
112 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@DESKTOP-59T6U68:/# apt install libclang-dev clang
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  binfmt-support clang-14 curl lib32gcc-s1 lib32stdc++6 libc-bin libc-dev-bin libc6 libc6-dev libc6-i386
  libclang-14-dev libclang-common-14-dev libclang-cpp14 libclang-rt-14-dev libclang1-14 libcurl3-gnutls libcurl3-nss
  libcurl4 libcurl4-openssl-dev libgc1 libllvm14 libnspr4 libnss3 libobjc-12-dev libobjc4 libpfm4 libpipeline1 libz3-4
  libz3-dev llvm-14 llvm-14-dev llvm-14-linker-tools llvm-14-runtime llvm-14-tools nss-plugin-pem
  python3-pkg-resources python3-pygments python3-yaml
Suggested packages:
  clang-14-doc wasi-libc glibc-doc libc-l10n locales libnss-nis libnss-nisplus manpages-dev libcurl4-doc libidn-dev
  libldap2-dev librtmp-dev libssh2-1-dev llvm-14-doc python3-setuptools python-pygments-doc ttf-bitstream-vera
Recommended packages:
  manpages manpages-dev libc-devtools
The following NEW packages will be installed:
  binfmt-support clang clang-14 lib32gcc-s1 lib32stdc++6 libc6-i386 libclang-14-dev libclang-common-14-dev
  libclang-cpp14 libclang-dev libclang-rt-14-dev libclang1-14 libcurl3-nss libgc1 libllvm14 libnspr4 libnss3
  libobjc-12-dev libobjc4 libpfm4 libpipeline1 libz3-4 libz3-dev llvm-14 llvm-14-dev llvm-14-linker-tools
  llvm-14-runtime llvm-14-tools nss-plugin-pem python3-pkg-resources python3-pygments python3-yaml
The following packages will be upgraded:
  curl libc-bin libc-dev-bin libc6 libc6-dev libcurl3-gnutls libcurl4 libcurl4-openssl-dev
8 upgraded, 32 newly installed, 0 to remove and 104 not upgraded.
Need to get 136 MB of archives.
After this operation, 901 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://deb.debian.org/debian bookworm/main amd64 libc6-dev amd64 2.36-9+deb12u13 [1904 kB]
Get:2 http://deb.debian.org/debian bookworm/main amd64 libc-dev-bin amd64 2.36-9+deb12u13 [47.4 kB]
Get:3 http://deb.debian.org/debian bookworm/main amd64 libc6 amd64 2.36-9+deb12u13 [2758 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 libc-bin amd64 2.36-9+deb12u13 [609 kB]
...
Get:40 http://deb.debian.org/debian bookworm/main amd64 llvm-14-dev amd64 1:14.0.6-12 [33.9 MB]
Fetched 136 MB in 19min 22s (117 kB/s)
debconf: delaying package configuration, since apt-utils is not installed
(Reading database ... 23250 files and directories currently installed.)
Preparing to unpack .../libc6-dev_2.36-9+deb12u13_amd64.deb ...
Unpacking libc6-dev:amd64 (2.36-9+deb12u13) over (2.36-9+deb12u10) ...
...
Setting up binfmt-support (2.2.2-2) ...
invoke-rc.d: could not determine current runlevel
invoke-rc.d: policy-rc.d denied execution of start.
Setting up libgc1:amd64 (1:8.2.2-3) ...
Setting up libllvm14:amd64 (1:14.0.6-12) ...
...
Processing triggers for libc-bin (2.36-9+deb12u13) ...
  1. 重新编译库文件
cargo build --release
   Compiling proc-macro2 v1.0.106
   Compiling quote v1.0.44
   Compiling libc v0.2.182
   Compiling icu_normalizer_data v2.1.1
   Compiling icu_properties_data v2.1.2
...
   Compiling reqwest v0.11.27
   Compiling chdb-rust v1.3.0 (/par/chdb-rust)
warning: [email protected]: libchdb not found locally, attempting to download...
warning: [email protected]: Downloading libchdb from: https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz
warning: [email protected]: Failed to find libchdb. Please install manually using './update_libchdb.sh --local' or '--global'
error: failed to run custom build command for `chdb-rust v1.3.0 (/par/chdb-rust)`

Caused by:
  process didn't exit successfully: `/par/chdb-rust/target/release/build/chdb-rust-c511778f0f9faad0/build-script-build` (exit status: 1)
  --- stdout
  cargo:warning=libchdb not found locally, attempting to download...
  cargo:warning=Downloading libchdb from: https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz
  cargo:warning=Failed to find libchdb. Please install manually using './update_libchdb.sh --local' or '--global'

  --- stderr
  Failed to find or download libchdb: error decoding response body: operation timed out
echo $OUT_DIR

vi ./update_libchdb.sh
bash: vi: command not found
ls /usr/local/lib/libchdb.so
ls: cannot access '/usr/local/lib/libchdb.so': No such file or directory

可能是更新了libc库的原因,它又重新编译,重新下载libchdb。
网络不稳定,下载libchdb失败,用手工方式下载, 然后解压到linux-x86_64-libchdb目录,再按照build.rs的写法建立软链接到系统目录

wget https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz
linux-x86_64-libchdb.tar.gz   100%[=================================================>] 140.46M  85.3KB/s    in 18m 19s

2026-03-01 10:12:52 (131 KB/s) - 'linux-x86_64-libchdb.tar.gz' saved [147287185/147287185]

ln -s linux-x86_64-libchdb/libchdb.so /usr/local/lib/libchdb.so
ln -s linux-x86_64-libchdb/chdb.h /usr/local/include/chdb.h
ls -l linux-x86_64-libchdb
total 497356
-rwxrwxrwx 1 root root     15695 Feb  2 16:41 chdb.h
-rwxrwxrwx 1 root root     16821 Feb  2 16:41 chdb.hpp
-rwxrwxrwx 1 root root 509254328 Feb  2 15:55 libchdb.so

重新编译,报同样的错,而whereis命令是能找到的libchdb.so,再建立软链接到当前目录(–local方式),通过了。

cargo build --release
...
  Failed to find or download libchdb: error sending request for url (https://github.com/chdb-io/chdb/releases/download/v4.0.1/linux-x86_64-libchdb.tar.gz): operation timed out
whereis libchdb.so
libchdb.so: /usr/local/lib/libchdb.so
whereis chdb.h
chdb.h:
ls /usr/local/include/chdb.h
/usr/local/include/chdb.h
ln -s linux-x86_64-libchdb/chdb.h ./chdb.h
ln -s linux-x86_64-libchdb/libchdb.so ./libchdb.so
cargo build --release
   Compiling chdb-rust v1.3.0 (/par/chdb-rust)
    Finished `release` profile [optimized] target(s) in 3.81s
  1. 编译例子
cargo test -- --test-threads=1
  Downloaded remove_dir_all v0.5.3 (registry `mirror`)
  Downloaded tempdir v0.3.7 (registry `mirror`)
  Downloaded rand v0.4.6 (registry `mirror`)
  Downloaded 3 crates (94.8KiB) in 0.55s

   Compiling chdb-rust v1.3.0 (/par/chdb-rust)
error: failed to run custom build command for `chdb-rust v1.3.0 (/par/chdb-rust)`

Caused by:
  process didn't exit successfully: `/par/chdb-rust/target/debug/build/chdb-rust-704a39af27e325de/build-script-build` (exit status: 101)
  --- stdout
  cargo:rustc-link-search=.
  cargo:rustc-link-search=./
  cargo:rustc-link-search=/usr/local/lib
  cargo:rustc-link-lib=chdb
  cargo:rerun-if-changed=wrapper.h
  cargo:rerun-if-changed=build.rs
  cargo:rerun-if-env-changed=TARGET
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64-unknown-linux-gnu
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_linux_gnu
  cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS
  cargo:rerun-if-changed=/par/chdb-rust/target/debug/build/chdb-rust-bdf962f0e48319ab/out/temp_wrapper.h

  --- stderr
  /par/chdb-rust/target/debug/build/chdb-rust-bdf962f0e48319ab/out/temp_wrapper.h:1:10: fatal error: './chdb.h' file not found

  thread 'main' panicked at build.rs:122:10:
  Unable to generate bindings: ClangDiagnostic("/par/chdb-rust/target/debug/build/chdb-rust-bdf962f0e48319ab/out/temp_wrapper.h:1:10: fatal error: './chdb.h' file not found\n")
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace  

查看生成的临时文件/par/chdb-rust/target/debug/build/chdb-rust-bdf962f0e48319ab/out/temp_wrapper.h,里面写的是#include "./chdb.h",而临时目录中并没有这个文件。手工把chdb.h文件复制过去,编译通过了。

cp chdb.h /par/chdb-rust/target/debug/build/chdb-rust-bdf962f0e48319ab/out/
cargo build --examples
   Compiling chdb-rust v1.3.0 (/par/chdb-rust)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.20s


/par/chdb-rust/target/debug/examples/01_stateless_queries
/par/chdb-rust/target/debug/examples/01_stateless_queries: error while loading shared libraries: libchdb.so: cannot open shared object file: No such file or directory

找不到动态链接库,设置环境变量就可以了。

export LD_LIBRARY_PATH=./
/par/chdb-rust/target/debug/examples/01_stateless_queries
=== Stateless Query Examples ===

1. Simple query with default format:
Result: 2


2. Query with JSON output format:
JSON Result: {"greeting":"Hello","answer":42}


3. Complex query with calculations:
   ┏━━━━━━━━┳━━━━━━━━━┳━━━━━━━━━┓
   ┃ number ┃ doubled ┃ squared ┃
   ┡━━━━━━━━╇━━━━━━━━━╇━━━━━━━━━┩
1. │      0 │       0 │       0 │
   ├────────┼─────────┼─────────┤
2. │      1 │       2 │       1 │
   ├────────┼─────────┼─────────┤
3. │      2 │       4 │       4 │
   ├────────┼─────────┼─────────┤
4. │      3 │       6 │       9 │
   ├────────┼─────────┼─────────┤
5. │      4 │       8 │      16 │
   └────────┴─────────┴─────────┘

/par/chdb-rust/target/debug/examples/07_analytics
=== Analytics Example ===

1. Creating database and table...
2. Inserting sample events...
3. Event statistics by type:

   ┏━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┓
   ┃ event_type ┃ count ┃ total_value ┃          avg_value ┃
   ┡━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━┩
1. │ page_view  │     5 │           5 │                  1 │
   ├────────────┼───────┼─────────────┼────────────────────┤
2. │ click      │     3 │           7 │ 2.3333333333333335 │
   ├────────────┼───────┼─────────────┼────────────────────┤
3. │ purchase   │     2 │      149.98 │              74.99 │
   └────────────┴───────┴─────────────┴────────────────────┘


4. Hourly event distribution:

{"hour":"2024-01-01 10:00:00","events_per_hour":7}
{"hour":"2024-01-01 11:00:00","events_per_hour":3}


5. Conversion funnel:

   ┏━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━┓
   ┃ event_type ┃ count ┃ percentage ┃
   ┡━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━┩
1. │ page_view  │     5 │         50 │
   ├────────────┼───────┼────────────┤
2. │ click      │     3 │         30 │
   ├────────────┼───────┼────────────┤
3. │ purchase   │     2 │         20 │
   └────────────┴───────┴────────────┘

root@DESKTOP-59T6U68:/par/chdb-rust#

编译出来7个示例,随便运行2个,都成功了。

转载自CSDN-专业IT技术社区

原文链接:https://blog.csdn.net/l1t/article/details/158581714

评论

赞0

评论列表

微信小程序
QQ小程序

关于作者

点赞数:0
关注数:0
粉丝:0
文章:0
关注标签:0
加入于:--