2

我注意到我的代码仓库警告我在 for/while 循环中使用 withColumn 是一种反模式。为什么不建议这样做?这不是 PySpark API 的正常使用吗?

4

1 回答 1

3

我们在实践中注意到,在withColumnfor/while 循环中使用会导致查询计划性能不佳,正如此处所讨论的那样。这在 Foundry 中首次编写代码时并不明显,因此我们构建了一个功能来警告您这种行为。

我们建议您遵循Scala 文档建议

withColumn(colName: String, col: Column): DataFrame
Returns a new Dataset by adding a column or replacing the existing column that has the same name.

Since
2.0.0

Note
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.

IE

my_other_columns = [...]

df = df.select(
  *[col_name for col_name in df.columns if col_name not in my_other_columns],
  *[F.col(col_name).alias(col_name + "_suffix") for col_name in my_other_columns]
)

my_other_columns = [...]

for col_name in my_other_columns:
  df = df.withColumn(
    col_name + "_suffix",
    F.col(col_name)
  )

虽然这在技术上可能是 PySpark API 的正常使用,但如果在您的工作中多次调用 withColumn 会导致查询计划性能不佳,因此我们希望您完全避免这个问题。

于 2021-12-16T15:46:11.603 回答