首页 > 解决方案 > 有效地折叠 Parquet 中的行组

问题描述

我有一个带有许多小行组的大型 Parquet 文件。我想生成一个带有单个(更大)行组的新 Parquet 文件,并且我正在使用 Python 进行操作。我可以做类似的事情:

import pyarrow.parquet as pq
table = pq.read_table('many_tiny_row_groups.parquet')
pq.write_table(table, 'one_big_row_group.parquet')

# Lots of row groups...
print (pq.ParquetFile('many_tiny_row_groups.parquet').num_row_groups)
# Now, only 1 row group...
print (pq.ParquetFile('one_big_row_group.parquet').num_row_groups)

但是,这需要我一次将整个 Parquet 文件读入内存。我想避免这样做。是否有某种“流式处理”方法可以使内存占用保持较小?

标签: pythonmemorycompressionparquet

解决方案


文档fastparquet记录了迭代大到适合内存的数据集的可能性。对于阅读,您可以使用:

pf = ParquetFile('myfile.parquet')
for df in pf.iter_row_groups():
    print(df.shape)
    # process sub-data-frame df

对于写入,您可以写入append文件。


推荐阅读