【发布时间】:2021-03-26 05:08:04
【问题描述】:
正如标题所述,我想通过使用 pyarrow 按大小(或行组大小)对 pyarrow 表进行重新分区并写入多个 parquet 文件。
我查看了 pyarrow 文档,并确定了分区数据集章节,这似乎是一个方向。不幸的是,它表明可以按列内容进行分区,但不能按大小(或行组大小)。
那么,从一张表开始,我如何控制写入步骤,以便以受控大小 x MB 写入多个文件? (或行组大小)
import pandas as pd
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
file = 'example.parquet'
file_res = 'example_res'
# Generate a random df
df = pd.DataFrame(np.random.randint(100,size=(100000, 20)),columns=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T'])
table = pa.Table.from_pandas(df)
# With this command, I can write a single parquet file that contains 2 row groups.
pq.write_table(table, file, version='2.0', row_group_size=50000)
# I can read it back and try to write it as a partitioned dataset, but a single parquet file is then written.
table_new = pq.ParquetFile(file).read()
pq.write_to_dataset(table_new, file_res)
感谢您的帮助! 最好的,
【问题讨论】:
-
您想为每组 50000 行创建一个文件吗?
-
嗨@0x26res,是的,这就是想法。我想把它们作为一个数据集,这样我就可以用 pyarrow 懒惰地阅读,但是在几个文件中。
-
啊啊,这似乎正是我正在寻找的东西 :)
标签: python parquet partition pyarrow