【发布时间】:2019-01-22 20:09:20
【问题描述】:
sparkSession.sql("select struct(col1,col2) as myStruct from table1")
返回具有以下架构的数据框
root
|-- myStruct : struct (nullable = false)
| |-- col1: string (nullable = true)
| |-- col2: string (nullable = true)
但我需要 col1 作为 myCol1 和 col2 作为 myCol2?
当我在结构函数中使用 as 关键字时,它会失败
sparkSession.sql("select struct(col1 as myCol1,col2 as myCol2) as myStruct from table1")
给出以下错误信息
mismatched input 'as' expecting {')', ','}(line 1, pos 19)
如何在 struct 字段中获取列别名?
【问题讨论】:
-
您使用哪个版本的 Spark?我刚刚在 2.3 上尝试过,对我来说工作得很好: scala> sql("select struct(user as l, movie as p) as mystruct from table").printSchema root |-- mystruct: struct (nullable = false) | |-- l: 整数(可为空=真)| |-- p: 整数(可为空=真)
-
Spark 2.1.0 版本
标签: apache-spark apache-spark-sql