首页 > 解决方案 > PySpark + AWS EMR:df.count() 需要很长时间才能完成

问题描述

我正在使用该操作count()来触发我的 udf 函数运行。这可行,但是在我的 udf 函数完成运行很久之后, df.count() 需要几天才能完成。数据框本身并不大,大约有 30k 到 100k 行。

AWS 集群设置:

Spark 变量和设置(这些是用于运行脚本的 spark 变量)

伪代码

这是我们脚本的实际结构。自定义 pandas udf 函数为每一行调用 PostGres 数据库。

from pyspark.sql.functions import pandas_udf, PandasUDFType

# udf_schema: A function that returns the schema for the dataframe

def main():
    # Define pandas udf for calculation
    # To perform this calculation, every row in the 
    # dataframe needs information pulled from our PostGres DB
    # which does take some time, ~2-3 hours
    @pandas_udf(udf_schema(), PandasUDFType.GROUPED_MAP)
    def calculate_values(local_df):
        local_df = run_calculation(local_df)
        return local_df

    # custom function that pulls data from our database and
    # creates the dataframe
    df = get_df()

    df = df\
        .groupBy('some_unique_id')\
        .apply(calculate_values)

    print(f'==> finished running calculation for {df.count()} rows!')

    return

标签: apache-sparkpysparkamazon-emr

解决方案


推荐阅读