读取和写入 Apache ORC 格式#
Apache ORC 项目提供了一种标准化的开源列式存储格式,用于数据分析系统。它最初是为 Apache Hadoop 中的 Apache Drill、Apache Hive、Apache Impala 和 Apache Spark 等系统创建的,并被这些系统采纳为高性能数据 I/O 的共享标准。
Apache Arrow 是用于读写 ORC 文件的数据的理想内存表示层。
获取支持 ORC 的 pyarrow#
如果您使用 pip 或 conda 安装 pyarrow,它应该会内置 ORC 支持。
>>> from pyarrow import orc
如果您从源代码构建 pyarrow,则在编译 C++ 库时必须使用 -DARROW_ORC=ON,并在构建 pyarrow 时启用 ORC 扩展。有关更多详细信息,请参阅Python 开发页面。
读取和写入单个文件#
函数 read_table() 和 write_table() 分别读取和写入 pyarrow.Table 对象。
我们来看一个简单的表
>>> import numpy as np
>>> import pyarrow as pa
>>> table = pa.table(
... {
... 'one': [-1, np.nan, 2.5],
... 'two': ['foo', 'bar', 'baz'],
... 'three': [True, False, True]
... }
... )
我们使用 write_table 将其写入 ORC 格式
>>> from pyarrow import orc
>>> orc.write_table(table, 'example.orc')
这会创建一个单个 ORC 文件。实际上,一个 ORC 数据集可能包含许多目录中的许多文件。我们可以使用 read_table 读取单个文件
>>> table2 = orc.read_table('example.orc')
您可以传递列的子集进行读取,这比读取整个文件要快得多(由于列式布局)
>>> orc.read_table('example.orc', columns=['one', 'three'])
pyarrow.Table
one: double
three: bool
----
one: [[-1,nan,2.5]]
three: [[true,false,true]]
我们不需要使用字符串来指定文件的来源。它可以是以下任何一种:
作为字符串的文件路径
一个 Python 文件对象
一个 pathlib.Path 对象
PyArrow 中的一个 NativeFile
一般来说,Python 文件对象的读取性能最差,而字符串文件路径或 NativeFile 实例(尤其是内存映射)的性能最佳。
我们还可以通过 pyarrow.dataset 接口读取包含多个 ORC 文件的分区数据集。
另请参阅
ORC 文件写入选项#
write_table() 有许多选项可以控制写入 ORC 文件时的各种设置。
file_version,要使用的 ORC 格式版本。'0.11'确保与旧版读取器兼容,而'0.12'是较新的版本。stripe_size,用于控制列条带内数据的近似大小。目前默认为 64MB。
有关更多详细信息,请参阅 write_table() 的文档字符串。
更精细的读写#
read_table 使用 ORCFile 类,它具有其他功能
>>> orc_file = orc.ORCFile('example.orc')
>>> orc_file.metadata
-- metadata --
>>> orc_file.schema
one: double
two: string
three: bool
>>> orc_file.nrows
3
有关更多详细信息,请参阅 ORCFile 的文档字符串。
正如您在 Apache ORC 格式中了解到的,ORC 文件由多个条带组成。read_table 将读取所有条带并将它们连接成一个表。您可以使用 read_stripe 读取单个条带。
>>> orc_file.nstripes
1
>>> orc_file.read_stripe(0)
pyarrow.RecordBatch
one: double
two: string
three: bool
我们可以使用 ORCWriter 写入 ORC 文件
>>> with orc.ORCWriter('example2.orc') as writer:
... writer.write(table)
压缩#
行组中列内的数据页面可以在编码通过(字典、RLE 编码)后进行压缩。在 PyArrow 中,我们默认不使用压缩,但也支持 Snappy、ZSTD、Zlib 和 LZ4
>>> orc.write_table(table, where, compression='uncompressed')
>>> orc.write_table(table, where, compression='zlib')
>>> orc.write_table(table, where, compression='zstd')
>>> orc.write_table(table, where, compression='snappy')
Snappy 通常会带来更好的性能,而 Zlib 可能会生成更小的文件。
从云存储读取#
除了本地文件,pyarrow 还通过 filesystem 关键字支持其他文件系统,例如云文件系统
>>> from pyarrow import fs
>>> s3 = fs.S3FileSystem(region="us-east-2")
>>> table = orc.read_table("bucket/object/key/prefix", filesystem=s3)
另请参阅