【发布时间】:2023-05-23 07:01:01
【问题描述】:
考虑一个 DataFrame df 有 4 列 c0、c1、c2 和 c3 其中 c0 和 c1 是嵌套列(结构类型),另外两个是字符串类型:
root
|-- c0: struct (nullable = true)
| |-- x: string (nullable = true)
| |-- y: string (nullable = true)
|-- c1: struct (nullable = true)
| |-- x: string (nullable = true)
| |-- y: string (nullable = true)
|-- c2: string (nullable = true)
|-- c3: string (nullable = true)
我想根据c3 的值选择c0 或c1 的所有值。
例子:如果c3的值为“d”,我想选择c0.*否则c1.*
这是我迄今为止尝试过的,但没有运气:
方法:在 select 子句中使用 when 和 else。
.select(
col("c3"),
col("c4"),
when(col("c3") === "d", col("c0.*").otherwise(col("c1.*"))))
这给出了以下异常:
org.apache.spark.sql.AnalysisException: Invalid usage of '*' in expression 'casewhen';
然后我尝试使用df,而不是使用col:
.select(
col("c3"),
col("c4"),
when(col("c3") =!= "d", df("c0").otherwise(df("c1"))))
这给出了以下异常:
otherwise() can only be applied on a Column previously generated by when()
对此的任何帮助将不胜感激!
PS:我是 Spark 的初学者 :)
【问题讨论】:
标签: scala apache-spark apache-spark-sql