gpt4 book ai didi

apache-spark - 非分区 Parquet 数据的谓词下推

转载 作者:行者123 更新时间:2023-12-03 23:01:37 27 4
gpt4 key购买 nike

我在 S3 中有一个包含 Parquet 数据的文件夹:
存储桶名称/文件夹名称/年/月/日

eg:
s3://bucket_name/folder_name/2020/12/10
我正在使用 Apache sparkAWS EMR读取 Parquet 文件。
由于数据没有分区,有没有办法实现 谓词下推过滤而不分区数据?
可以使用哪些性能改进最佳实践。

最佳答案

我将在代码中描述我的解决方案:

import pyspark.sql.functions as f
from pyspark.shell import spark

# Read absolute path and put "/*/*/*" to read all partitions
df = spark.read.parquet("s3://bucket_name/folder_name/*/*/*")

# Get absolute file path
df = df.withColumn('path', f.split(f.input_file_name(), '/'))

# Slice path and recover year / month / day in an array
df = df.withColumn('year_month_day', f.slice(f.col('path'), -4, 3))

# Transform array values to respective columns
df = df.withColumn('year', f.col('year_month_day').getItem(0))
df = df.withColumn('month', f.col('year_month_day').getItem(1))
df = df.withColumn('day', f.col('year_month_day').getItem(2))

# Drop temporary columns
df = df.drop('path', 'year_month_day')

df.show()

# TODO : Make your transformations
# .
# .
# .
# Save partitioned by year, month and day (if you want)
# df.write.partitionBy('year', 'month', 'day').parquet('...')

我的目录:
Directory
输出:
+--------+--------+----+-----+---+
|column_a|column_b|year|month|day|
+--------+--------+----+-----+---+
| hello_1| hello_2|2019| 06| 10|
| world_1| world_2|2020| 12| 31|
+--------+--------+----+-----+---+

关于apache-spark - 非分区 Parquet 数据的谓词下推,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65386616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com