如何查询数组[struct]中结构字段的子集?

7bsow1i6  于 2021-06-27  发布在  Hive
关注(0)|答案(1)|浏览(311)

我在配置单元中有一个表,它有一个模式:

root
 |-- startdate: string (nullable = true)
 |-- enddate: string (nullable = true)
 |-- items: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _id: string (nullable = true)
 |    |    |-- name: string (nullable = true)
 |    |    |-- .......: string (nullable = true)
 |    |    |-- otherfields: string (nullable = true)

我只想从items数组字段中获取\u id和name列,即:

|-- items: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- _id: string (nullable = true)
 |    |    |-- name: string (nullable = true)

有没有一种方法可以做到这一点,而不必在spark本身进行额外的转换,这样就只从配置单元中检索实际的列?
我用的是spark 2.2。

lo8azlld

lo8azlld1#

您可以尝试以下操作:

data.select("items._id", "items.name")

尽管可能会导致:

root
 |-- _id: array (nullable = true)
 |    |-- element: string (containsNull = true)
 |-- name: array (nullable = true)
 |    |-- element: string (containsNull = true)

在spark 2.4+中,您可以尝试使用U-zip

相关问题