将一个非常非常大的csv文件转换为parquet格式。

5

我正在尝试将一个csv文件转换为parquet格式(无论是用Python还是命令行都可以)。这个问题回答了我的疑问,但是答案似乎要求先读入csv文件,而在我的情况下,csv文件大小为17GB,这种方法不太可行,所以我需要一种“离线”或流式处理的方法。


是的,我有将近300GB的CSV需要转换。 - Ian Sudbery
1个回答

2
我成功地使用 csv2parquet 将一个超过7GB(270万行)的 CSV 文件转换成了 Parquet 文件。
该过程很简单:
  • 首先,我使用 csvkit 的 csvclean 工具清洗了我的 CSV 文件(但您可能不需要这个步骤)
  • 使用 csv2parquet 生成 JSON 模式
  • 按需手动编辑模式
  • 使用 csv2parquet 生成 Parquet 文件
  • 额外福利:使用 DuckDB 直接在 Parquet 文件上测试简单的 SQL 查询
如果您下载我们在https://world.openfoodfacts.org/data 上提供的 CSV 导出文件,则可以重现此过程。
# Not needed for you, just in case you want to reproduce
wget https://static.openfoodfacts.org/data/en.openfoodfacts.org.products.csv
csvclean -t en.openfoodfacts.org.products.csv

# Generate the schema
./csv2parquet --header true -p -n en.openfoodfacts.org.products_out.csv products_zstd.pqt > parquet.shema
# It has to be modified because column detection is sometimes wrong.
# From Open Food Facts CSV, for example, the code column is detected as a an Int64, but it's in fact a "Utf8".
nano parquet.shema

# Generate parquet file.
# Using -c for compression is optional.
# -c zstd appears to be the best option regarding speed/compression.
./csv2parquet --header true -c zstd -s parquet.schema en.openfoodfacts.org.products_out.csv products_zstd.pqt

# Try a query thanks to DuckDB. It's as fast as a database!
time ./duckdb test-duck.db "select * FROM (select count(data_quality_errors_tags) as products_with_issues from read_parquet('products_zstd.pqt') where data_quality_errors_tags != ''), (select count(data_quality_errors_tags) as products_with_issues_but_without_images from $db where data_quality_errors_tags != '' and last_image_datetime == '');"
┌──────────────────────┬─────────────────────────────────────────┐
│ products_with_issues │ products_with_issues_but_without_images │
├──────────────────────┼─────────────────────────────────────────┤
│ 29333                │ 4897                                    │
└──────────────────────┴─────────────────────────────────────────┘

real    0m0,211s
user    0m0,645s
sys 0m0,053s


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接