【发布时间】:2021-02-18 16:11:57
【问题描述】:
我有一个大的压缩 json 文件,未压缩的单个文件约为 128GB。使用 .gz 压缩文件大约为 21GB。我想利用 pyarrow 分块读取文件并转换为 parquet 数据集。我想模仿 panda 的阅读器功能,但遇到了一些问题。
我有以下代码工作,其中通过 pandas read_json 将压缩的 json 文件读入块,然后将这些块转换为 apache 箭头表,然后写入 parquet 数据集:
reader = pd.read_json("file.json.gz", lines=True, chunksize=5000000)
for chunk in reader:
arrow_table = pa.Table.from_pandas(chunk,nthreads=4)
pq.write_to_dataset(arrow_table,root_path="dir")
这段代码得到了我期望的结果,但是我想直接使用 apache 箭头,而不必先拉入分块的 pandas 数据帧,然后再拉出 apache 箭头表。考虑到 apache arrow 与 pandas read_json 的多线程读取功能,我主要想在性能上有所提升。
我尝试使用 pyarrow.json 类 (https://arrow.apache.org/docs/python/generated/pyarrow.json.ReadOptions.html#pyarrow.json.ReadOptions) 的 ReadOptions 但是,当我运行以下代码时,在我看来 apache 箭头首先解压缩内存中的整个文件,然后再按我在 block_size 参数中设置的块大小,考虑到文件的大小,如果我让代码运行,则会出现内存不足错误。
from pyarrow import json
opts = json.ReadOptions(block_size=4096)
with json.read_json('file.json.gz',opts) as f:
table = f
pq.write_to_dataset(table, root_path='dir')
代替with json.read_json,我正在研究类似于文件流阅读器的输入流功能,但不确定这是否是正确的路线。
欢迎任何建议。
【问题讨论】:
标签: pyarrow apache-arrow