组合 n 个 Pyspark 数据帧的最有效方法

问题描述 投票:0回答:1

我有一个特定的函数需要使用这个基本结构进行优化:

list customer_dfs = []

for customer in customer_list
   df = // Pyspark Transformation Functions

   {10-15 lines of customer specific transformations/aggregations}

   customer_dfs.append(df)


combined_df = spark.createDataFrame([], customer_dfs[0].schema)

for df in customer_dfs:
   combined_df = combined_df.union(df)

return combined_df

尽管每个数据帧都相对较小,但这种迭代联合的性能显然会随着每次迭代而下降,并很快变得难以维持。

是否有更快/更高效的方法来达到相同的结果?这是我们希望在 AWS Glue 4.0 作业上下文中执行的操作。

apache-spark pyspark optimization union aws-glue
1个回答
0
投票

您可以尝试使用

reduce
模块中的
functools
函数以及
Dataframe.unionByName
,因为每个数据帧都相对较小。 我希望列名也相同:

from functools import reduce
from pyspark.sql import DataFrame

# Apply reduce using unionByName on the list of customer dataframes
combined_df = reduce(DataFrame.unionByName, customer_dfs)
© www.soinside.com 2019 - 2024. All rights reserved.