PySpark,避免多次使用Column方法调用dataframe后出现StackOverflowException

2024-05-12 15:32:16 发布

您现在位置:Python中文网/ 问答频道 /正文

AWS胶水 Spark2.4 Python3 胶水版本2.0

我在多次使用Column方法调用dataframe后发现StackOverflowException

datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "database_name", 
table_name = "table_name", transformation_ctx = "datasource0")
df = datasource0.toDF()

df = df.withColumn('item_name', F.regexp_replace(F.col('item_name'), '^foo$', 'bar'))
df = df.withColumn('item_name', F.regexp_replace(F.col('item_name'), '^foo$', 'bar'))
df = df.withColumn('item_name', F.regexp_replace(F.col('item_name'), '^foo$', 'bar'))
df = df.withColumn('item_name', F.regexp_replace(F.col('item_name'), '^foo$', 'bar'))
... # and call hundreds times

文件说

Blockquote This method introduces a projection internally. Therefore, calling it multiple times, for instance, via loops in order to add multiple columns can generate big plans which can cause performance issues and even StackOverflowException. To avoid this, use select() with the multiple columns at once.

所以我知道我需要同时对多个列使用select()

但我不知道如何编写代码


Tags: namedffootablebarcolmultipleitem
1条回答
网友
1楼 · 发布于 2024-05-12 15:32:16

根据thisyes withColumn将导致与内存相关的问题,这可以通过使用select来防止,如下所示:

df.select(F.regexp_replace(F.col('item_name')))

如果要对多个列应用相同的设置,则可以如下所示:

df.select(F.regexp_replace(F.col('item_name')),F.regexp_replace(F.col('item_name')),.........upto n number of columns)

相关问题 更多 >