【问题标题】:Read in large compressed json file with pyarrow similar to pandas reader functionality使用类似于 pandas 阅读器功能的 pyarrow 读取大型压缩 json 文件
【发布时间】: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


    【解决方案1】:

    这听起来类似于https://issues.apache.org/jira/browse/ARROW-10372。从 2.0.0 版本开始,数据集功能不支持读取压缩的 CSV 或 JSON 文件。您介意评论一下您在该问题上的用例吗?

    【讨论】:

    • 嘿尼尔,我会补充这个问题,感谢您的回复!我想知道是否根本没有办法在 pyarrow 中执行此操作,无论它是否使用更新的数据集功能?我想我可以以类似的方式将文件分块成箭头表而不是数据框,但也许不是这样。
    • 箭头中的 CSV 阅读器有一个“流式传输”选项(请参阅 arrow.apache.org/docs/python/csv.html#incremental-reading),但是,我不喜欢目前为 JSON 实现的东西。
    • @joris 对此表示感谢,正如您提到的当前不是 JSON 并且始终是单线程的,所以我不确定我是否会从 pandas 中看到很多性能提升,但可能。我现在取得了一些进展,在阅读时出现了超出最大行数的异常。
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2019-08-10
    • 2012-01-11
    • 2012-05-18
    • 2023-03-17
    • 2015-06-04
    相关资源
    最近更新 更多