【发布时间】:2021-12-09 22:38:53
【问题描述】:
我的架构如下所示:
|-- contributors: map (nullable = true)
| | |-- key: string
| | |-- value: array (valueContainsNull = true)
| | | |-- element: struct (containsNull = true)
| | | | |-- type: string (nullable = true)
| | | | |-- name: string (nullable = true)
| | | | |-- id: string (nullable = true)
我想要一个包含 key、name 和 id 列的数据框
我已使用以下代码获取name 和id,但如何获取key 列?
df.select(explode(col("contributors")))
.select(explode(col("value")))
.select(col("col.*"))
更新
我尝试将第一个解决方案应用于以下架构,但编译器不喜欢它。我想得到value._name和subgenres.element.value._name
|-- mainGenre: struct (nullable = true)
| |-- value: struct (nullable = true)
| | |-- _name: string (nullable = true)
| |-- subgenres: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- value: struct (nullable = true)
| | | | |-- type: string (nullable = true))
| | | | |-- _name: string (nullable = true)
| | | |-- name: map (nullable = true)
| | | | |-- key: string
| | | | |-- value: string (valueContainsNull = true)
我尝试使用value._name 创建一个变量,然后像这样将其插入到我的第二个变量中。
val col_mainGenre_name = df_r.select(col("mainGenre.*"))
.select(col("value.*"))
.select(col("_name"))
.drop("readableName")
.drop("description")
val df_exploded = df_r.select(col("mainGenre.*"))
.select(col_mainGenre_name, col("value.*"))
【问题讨论】:
标签: scala apache-spark apache-spark-sql