我从同一文件夹中的多个 Parquet 文件创建了一个 Parquet 文件。每个文件对应一个分区。
Parquet 文件是在不同的进程中创建的(使用 Python concurrent.futures
)。这是我在一个进程中运行的代码示例:
# `df` is a standard Pandas DataFrame with
# 22 columns of different types and at most 100e3 rows.
# Set the index
df.set_index("cid", inplace=True)
# Write to single file
fastparquet.write(fpath, df, compression='snappy, file_scheme='simple)
df
最多包含 100e3
行(和 22 列)并在整数索引(称为 cid
)上进行索引。
然后我使用以下方法创建了两个元数据文件:
# `data_paths` contains the list of all the Parquet data files
# created in multiple processes.
fastparquet.writer.merge(data_paths, verify_schema=True)
并且确实在包含所有 Parquet 文件的文件夹中正确创建了 _metadata
和 _common_metadata
。
我天真地认为,因为数据被索引和/或它有元数据文件,所以获取数据大小等基本信息应该很快。例如,以下需要永远:
import dask.dataframe as ds
# `dataset_path` is the path to the folder
# containing all the Parquet files created above
# and the metadata files.
# It contains ~100-200 individual Parquet files
# for a total of ~60,000,000 rows
data = df.read_parquet(dataset_path)
data.shape[0].compute()
那是异常(exception)吗?
另请注意,大多数列是int64
、float64
,少数是object
(string
大小不一。
最佳答案
不幸的是,从元数据中提取数据帧长度的优化尚不存在。相反,dask 将每个分区加载到内存中并测量其长度。您会注意到,如果您选择单个列(或索引),这种情况发生得更快:
len(data[onecolumn])
但是,对于 parquet 的特殊情况,您是完全正确的,长度是预先从一组或多组元数据中知道的,并且能够一次获得它会很好。请随时在 Dask 问题跟踪器上请求此功能。现在,您可以使用 fastparquet.ParquetFile
的 count
和 columns
属性。
关于dask - 为什么在 dask 中计算索引 Parquet 文件的形状如此缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59023793/