Ubuntu 22.04 安装 Apache DataFusion 指南
面向 Ubuntu 22.04 服务器的 Apache DataFusion 集成安装指南,涵盖 Python 与 Rust 运行时准备、pip/cargo 两种安装方式、版本验证、最小 SQL 与 CLI 查询实测回显以及完整卸载步骤。
Ubuntu 22.04 安装 Apache DataFusion 指南
Apache DataFusion 是 Rust 编写的可扩展查询引擎,基于 Apache Arrow 内存格式,提供 Rust crate、Python 绑定(datafusion)和命令行工具 datafusion-cli。它属于库/引擎类组件,没有常驻守护进程,安装即「引入依赖 + 运行示例」。本文在 Ubuntu 22.04(x86_64)服务器上完成 Python 绑定与 CLI 的安装、验证与卸载,命令与回显均为该服务器实测结果。
一、前置条件
1.1 系统与账号
- 操作系统:Ubuntu 22.04 LTS(x86_64),已配置可用的 apt 源。
- 账号:以
root执行(或具备 sudo 权限的等价账号)。 - 硬件:建议 ≥ 2 vCPU、≥ 4 GB 内存;本文实测环境为 12 vCPU、约 32 GB 内存。
1.2 版本约束
Ubuntu 22.04 自带 Python 3.10,满足 DataFusion Python 绑定(以 48.0.0 为例为 cp39-abi3、要求 Python ≥ 3.9)的要求,无需额外安装解释器:
$ python3 --version
Python 3.10.12
1.3 网络与镜像
直连 PyPI 较慢时可加 -i https://pypi.tuna.tsinghua.edu.cn/simple;构建 CLI 需访问 crates.io(或对应镜像)。
二、准备 Python 运行时
安装 python3 与 pip:
$ apt-get update -qq && apt-get install -y -qq python3 python3-pip
$ python3 --version
Python 3.10.12
$ pip3 --version
pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)
如后续需用 Rust 方式构建 CLI,再准备 C 工具链:
$ apt-get install -y build-essential curl
三、安装 DataFusion(pip 方式,推荐)
3.1 安装绑定及其依赖
$ pip3 install --no-input -i https://pypi.tuna.tsinghua.edu.cn/simple "datafusion==48.0.0"
Installing collected packages: typing-extensions, pyarrow, datafusion
Successfully installed datafusion-48.0.0 pyarrow-25.0.1 typing-extensions-4.16.0
pip 会自动安装匹配当前 Python 的 pyarrow wheel(此处为 25.0.1)。
3.2 可选:安装 pandas(数据互操作)
$ pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
Successfully installed pandas-2.3.3 ...
四、安装 DataFusion CLI(cargo 方式)
datafusion-cli 未发布到 PyPI,也无官方预编译二进制,需通过 Rust 的 cargo 从源码构建。
4.1 安装 Rust 工具链
$ curl -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain stable
$ . "$HOME/.cargo/env"
$ rustc --version
rustc 1.99.0 (b940084d7 2026-09-28)
$ cargo --version
cargo 1.99.0 (5f94df478 2026-08-27)
4.2 从源码构建 CLI
$ cargo install datafusion-cli --locked
Compiling datafusion-cli v55.2.0
Finished `release` profile [optimized] target(s)
Installed package `datafusion-cli v55.2.0` (executable `datafusion-cli`)
$ ~/.cargo/bin/datafusion-cli --version
datafusion-cli 55.2.0
提示:首次编译约需 15 分钟(12 核),可用 crates.io 镜像加速;产物位于 ~/.cargo/bin/datafusion-cli,建议将其加入 PATH。
五、版本验证
$ python3 -c "import datafusion; print(datafusion.__version__)"
48.0.0
$ pip3 show datafusion
Name: datafusion
Version: 48.0.0
Summary: Build and run queries against data
Home-page: https://datafusion.apache.org/python
$ datafusion-cli -q -c "SELECT version();"
+-------------------------------------------+
| version() |
+-------------------------------------------+
| Apache DataFusion 55.2.0, x86_64 on linux |
+-------------------------------------------+
六、最小 SQL 查询示例
6.1 Python 绑定
准备两个小 CSV(/root/employees.csv、/root/depts.csv),随后运行:
from datafusion import SessionContext
ctx = SessionContext()
ctx.register_csv("employees", "/root/employees.csv", has_header=True)
ctx.register_csv("depts", "/root/depts.csv", has_header=True)
ctx.sql("SELECT e.name, d.dept_name FROM employees e JOIN depts d "
"ON e.dept_id = d.dept_id ORDER BY e.name").show()
真实回显:
+-------+-------------+
| name | dept_name |
+-------+-------------+
| Alice | Engineering |
| Bob | Sales |
| Carol | Engineering |
| David | Marketing |
+-------+-------------+
6.2 datafusion-cli
$ datafusion-cli -q -c "SELECT 1+1 AS a, upper('datafusion') AS b;"
+---+------------+
| a | b |
+---+------------+
| 2 | DATAFUSION |
+---+------------+
七、卸载
7.1 卸载 Python 绑定
$ pip3 uninstall -y datafusion pyarrow pandas
7.2 卸载 CLI
$ cargo uninstall datafusion-cli
7.3 彻底移除 Rust 工具链(可选)
$ rustup self uninstall
卸载后可用 python3 -c "import datafusion" 与 datafusion-cli --version 验证命令已不可用。