要求 Spark 停用 sql when/otherwise 子句中的完整句子评估

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

我不知道我的要求是否可行,但我需要找到解决方案。

我有一个数据框,我想添加一个带有 when otherwise 子句的列:

val delta = df
  .withColumn("PC", when(col("PartitionKey")=!="[]", lit(spark.sql(s"SHOW Partitions $TableName").count())).otherwise(null))

问题是火花是在所有情况下评估句子的两个部分所以当

PK=="[]"
它失败并出现
AnalysisException: SHOW PARTITIONS is not allowed on a table that is not partitioned
错误这是正常的。

我可以想到两种可能的解决方案:

  1. 在 when 子句中插入某种 try except 代码 ==> 至少如果我不将整个 if bloc 作为变量取出而不是简单地调用它,这似乎是不可能的。
  2. Order Spark not to evaluate the whole when otherwise ==> I don't know if it is possible.
apache-spark
© www.soinside.com 2019 - 2024. All rights reserved.