我不知道我的要求是否可行,但我需要找到解决方案。
我有一个数据框,我想添加一个带有 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
错误这是正常的。
我可以想到两种可能的解决方案: