【问题标题】:Extracting value from nested array and struct spark从嵌套数组和结构火花中提取值
【发布时间】:2019-09-11 23:09:41
【问题描述】:

我有一个如下所示的架构。我想知道在火花中选择元素座椅和驱动器然后将其铸成绳子的最佳方法是什么。我正在使用 spark 1.6 的数据框中阅读此内容。

|-- cars: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- carId: string (nullable = true)
 |    |    |-- carCode: string (nullable = true)
 |    |    |-- carNumber: string (nullable = true)
 |    |    |-- features: array (nullable = true)
 |    |    |    |-- element: struct (containsNull = true)
 |    |    |    |    |-- seat: string (nullable = true)
 |    |    |    |    |-- drive: string (nullable = true)

cars.features 的输出为 json 中的 car_features:

"cars_features":[[{"seat":"Auto","drive":"Manual"}]]

我正在尝试选择“自动”并将其放入数据框列和“手动”并放入另一列。

当前尝试将整个结构返回为:

+-------------------+
|car_features       |
+-------------------+
|  [[Auto,Manual]]  |
+-------------------+

col("car.features").getItem(0).as("car_features_seat")

【问题讨论】:

  • 所以您想选择seatdrive 作为数组的数组还是仅作为数组或作为行?
  • 我编辑了问题以使其更清楚。
  • getItem("key") 不是 0。

标签: apache-spark hadoop apache-spark-sql


【解决方案1】:

我不得不钻入两次数组:

col("car.features").getItem(0).getItem(0).getItem("seat").cast("String").as("car_features_seat")

这会提取“自动”

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2021-07-31
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多