
本文字数:8540;估计阅读时间:22 分钟
作者:Mark Needham
本文在公众号【ClickHouseInc】首发

Ibis 是一个开源的数据帧库,旨在兼容任何数据系统使用。它支持超过 20 种后端,包括 Polars、DataFusion 和 ClickHouse,并提供一个 Python 风格的接口,可以将关系操作翻译为 SQL 在底层数据库上执行。
在这篇博客文章中,我们将学习如何在 ClickHouse 中使用 Ibis【https://ibis-project.org/】。
可组合数据生态系统
Ibis 是可组合数据生态系统的一部分。下图展示了这一生态系统:

图中显示 Ibis 作为用户界面。与大多数其他 DataFrame 库不同,Ibis 使用 SQL 作为中间表示语言,这使得它能更轻松地与不同的后端进行通信。
安装 Ibis 和 ClickHouse
首先,我们来安装 Ibis、其示例数据和 ClickHouse。
pip install 'ibis-framework[clickhouse,examples]'
如果还没有运行 ClickHouse 服务器,我们需要先启动一个 ClickHouse 服务器:
curl https://clickhouse.com/ | sh
./clickhouse server
ClickHouse 启动后,我们就可以开始了!
导入 Ibis 示例数据集到 ClickHouse
Ibis 附带了多种示例数据集。我们将导入 nycflights13_flights 数据集到 ClickHouse 中。
首先,我们需要导入 Ibis 并创建与 ClickHouse 的连接:
import ibis
from ibis import _
con = ibis.connect("clickhouse://")
如果想使用远程的 ClickHouse 服务器,可以在连接字符串中提供相应的 URL 和凭证。下一步是创建表:
con.create_table(
"flights",
ibis.examples.nycflights13_flights.fetch().to_pyarrow(),
overwrite=True
)
此命令将数据集导入名为 flights 的表中,并在表已经存在时进行替换。
接下来,我们在另一个标签页中连接到 ClickHouse 查看此命令的效果:
./clickhouse client -m
ClickHouse client version 24.7.1.2215 (official build).
Connecting to localhost:9000 as user default.
Connected to ClickHouse server version 24.7.1.
连接后,我们可以获取表的列表:
SHOW TABLES
┌─name────┐
1. │ flights │
└─────────┘
1 row in set. Elapsed: 0.002 sec.
探索 Ibis 的 flights 数据集
首先来看一下 flights 表中的字段:
DESCRIBE TABLE flights
SETTINGS describe_compact_output = 1
Query id: 7d497dee-ea8d-4b07-8b32-3f32f775ca32
┌─name───────────┬─type────────────────────┐
1. │ year │ Nullable(Int64) │
2. │ month │ Nullable(Int64) │
3. │ day │ Nullable(Int64) │
4. │ dep_time │ Nullable(String) │
5. │ sched_dep_time │ Nullable(Int64) │
6. │ dep_delay │ Nullable(String) │
7. │ arr_time

最低0.47元/天 解锁文章
316

被折叠的 条评论
为什么被折叠?



