针对数据进行构建和运行查询
项目描述
DataFusion在Python中
这是一个绑定到Apache Arrow内存查询引擎DataFusion的Python库。
DataFusion的Python绑定可以用作构建新数据系统的基石。以下是一些示例
- Dask SQL使用DataFusion的Python绑定进行SQL解析、查询规划和逻辑计划优化,然后将逻辑计划转换为Dask操作以执行。
- DataFusion Ballista是一个分布式SQL查询引擎,它扩展了DataFusion的Python绑定以用于分布式场景。
还可以直接使用这些Python绑定进行DataFrame和SQL操作,但您可能会发现Polars和DuckDB更适合此用例,因为它们更侧重于最终用户,并且比这些Python绑定维护得更积极。
功能
- 使用SQL或DataFrame执行针对CSV、Parquet和JSON数据源的查询。
- 查询使用DataFusion的查询优化器进行优化。
- 从SQL中执行用户定义的Python代码。
- 与支持PyArrow的Pandas和其他DataFrame库交换数据。
- 在Substrait格式中序列化和反序列化查询计划。
- 实验支持将SQL查询转换为Polars、Pandas和cuDF的DataFrame调用。
示例用法
以下示例演示了使用DataFusion运行SQL查询以针对Parquet文件,将结果存储在Pandas DataFrame中,然后绘制图表。
此示例中使用的Parquet文件可以从以下页面下载
from datafusion import SessionContext
# Create a DataFusion context
ctx = SessionContext()
# Register table with context
ctx.register_parquet('taxi', 'yellow_tripdata_2021-01.parquet')
# Execute SQL
df = ctx.sql("select passenger_count, count(*) "
"from taxi "
"where passenger_count is not null "
"group by passenger_count "
"order by passenger_count")
# convert to Pandas
pandas_df = df.to_pandas()
# create a chart
fig = pandas_df.plot(kind="bar", title="Trip Count by Number of Passengers").get_figure()
fig.savefig('chart.png')
这将生成以下图表
配置
在创建上下文时,可以配置运行时(内存和磁盘设置)和配置设置。
runtime = (
RuntimeConfig()
.with_disk_manager_os()
.with_fair_spill_pool(10000000)
)
config = (
SessionConfig()
.with_create_default_catalog_and_schema(True)
.with_default_catalog_and_schema("foo", "bar")
.with_target_partitions(8)
.with_information_schema(True)
.with_repartition_joins(False)
.with_repartition_aggregations(False)
.with_repartition_windows(False)
.with_parquet_pruning(False)
.set("datafusion.execution.parquet.pushdown_filters", "true")
)
ctx = SessionContext(config, runtime)
有关更多信息,请参阅API文档。
打印上下文将显示当前配置设置。
print(ctx)
更多示例
有关更多信息,请参阅示例。
使用DataFusion执行查询
- 使用SQL查询Parquet文件
- 使用DataFrame API查询Parquet文件
- 运行SQL查询并将结果存储在Pandas DataFrame中
- 运行带有Python用户定义函数(UDF)的SQL查询
- 运行带有Python用户定义聚合函数(UDAF)的SQL查询
- 查询PyArrow数据
- 创建DataFrame
- 导出DataFrame
运行用户定义的Python代码
Substrait支持
如何安装(从pip)
Pip
pip install datafusion
# or
python -m pip install datafusion
Conda
conda install -c conda-forge datafusion
您可以通过运行以下命令来验证安装
>>> import datafusion
>>> datafusion.__version__
'0.6.0'
如何开发
这假设您已安装rust和cargo。我们使用pyo3和maturin推荐的流程。
此流程中使用的Maturin工具可以通过Conda或Pip安装。两种方法都应提供相同的使用体验。提供多种方法只是为了满足开发者的偏好。以下是Conda和Pip的引导过程。
引导(Conda)
# fetch this repo
git clone git@github.com:apache/datafusion-python.git
# create the conda environment for dev
conda env create -f ./conda/environments/datafusion-dev.yaml -n datafusion-dev
# activate the conda environment
conda activate datafusion-dev
引导(Pip)
# fetch this repo
git clone git@github.com:apache/datafusion-python.git
# prepare development environment (used to build wheel / install in development)
python3 -m venv venv
# activate the venv
source venv/bin/activate
# update pip itself if necessary
python -m pip install -U pip
# install dependencies (for Python 3.8+)
python -m pip install -r requirements.in
测试依赖于git子模块中的测试数据。
git submodule init
git submodule update
每当rust代码更改时(您的更改或通过git pull
)
# make sure you activate the venv using "source venv/bin/activate" first
maturin develop
python -m pytest
运行和安装 pre-commit 钩子
arrow-datafusion-python 利用 pre-commit 帮助开发者进行代码格式检查,以减少由于格式检查错误而导致 CI 失败的提交次数。对于开发者来说,使用 pre-commit 钩子是可选的,但无疑有助于保持 PR 清晰简洁。
可以通过运行 pre-commit install
来安装我们的 pre-commit 钩子,它将在 ARROW_DATAFUSION_PYTHON_ROOT/.github 目录中安装配置,并在每次提交时运行,如果在代码中发现违规的格式检查错误,将无法完成提交,这样你就可以在推送之前在本地进行修改。
也可以通过简单地运行 pre-commit run --all-files
来在不安装的情况下运行 pre-commit 钩子。
不使用 pre-commit 运行代码格式检查器
在 ci/scripts
目录中存在用于运行 Rust 和 Python 格式检查器的脚本。
./ci/scripts/python_lint.sh
./ci/scripts/rust_clippy.sh
./ci/scripts/rust_fmt.sh
./ci/scripts/rust_toml_fmt.sh
如何更新依赖项
要更改测试依赖项,请修改 requirements.in
并运行
# install pip-tools (this can be done only once), also consider running in venv
python -m pip install pip-tools
python -m piptools compile --generate-hashes -o requirements-310.txt
要更新依赖项,请使用 -U
运行
python -m piptools compile -U --generate-hashes -o requirements-310.txt
更多详情 这里
项目详情
datafusion-41.0.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b2124bcd976520a8dbcb456c200f2cb8b1343285e9329fe757aa628bbd0b08f7 |
|
MD5 | 2ab3d5c329bc3b5a21b3efbf51481912 |
|
BLAKE2b-256 | 3cf2b7c6fb37c54e4d4297adc18616e151236ddc6abff88557d55a851eefe6bd |
datafusion-41.0.0-cp38-abi3-win_amd64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a862f6667979a367c30ae58d8770bba044bab09d1da9012ee37cb3bb86fccdf2 |
|
MD5 | bcd1e1c46c850ce293c172160d46bdd8 |
|
BLAKE2b-256 | 6fce9be848dea0649e42ef073780ef0507ea74ca2c3f610c51480e47343f0842 |
datafusion-41.0.0-cp38-abi3-manylinux_2_28_aarch64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a10179610c8d211d215ff3533bbd8f5faf3b47b00c0e8371ca9656e98c420380 |
|
MD5 | e1307cfe0a1c02a8519b09fd2a676f7a |
|
BLAKE2b-256 | e4f6911d8ecd1a2a9ef7342a79805fbaa1192c37ea175311e33c023db0cd4356 |
datafusion-41.0.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b7c6987ad20b238a555fac09851f2329cd8b7e829de98446159ea27a172a5f1f |
|
MD5 | 72d7b9d240ba2f03f9f14ec8a105fee4 |
|
BLAKE2b-256 | 81ae9f0ec2e2d6e704e94630f6453ed443d4d34d8b8de79ee9c9dd7cddce2b31 |
datafusion-41.0.0-cp38-abi3-macosx_11_0_arm64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fe324048a63bf462d49cca3b046821fcb546cdab3a13b1fe860aab038c4e4ad4 |
|
MD5 | a4857fd34eb539d7d5760383bb3ca18c |
|
BLAKE2b-256 | 385b9d1c4de0b6b9af73899e40af07ed4a13019fa5e3e92e74bbbd6ce758a9fe |
datafusion-41.0.0-cp38-abi3-macosx_10_12_x86_64.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4b484035765a4f239737d6313af3cc3822448dfa86738ec44db02dfc4e08057f |
|
MD5 | d911d9fddf47ac5e16eb92f0706f1607 |
|
BLAKE2b-256 | 38cb12657a3c08e99bf6214d6990c987b0b5fb9441d49aae01477d7e9dea5ef0 |