【发布时间】:2021-02-22 16:56:43
【问题描述】:
我有一个如下所示的 spark 数据框。它在 zipped_feature 列中有数组结构数组。
+--------------------+
|zipped_feature |
+--------------------+
|[[A, 1], [ABC, 33]] |
|[[A, 1], [ABS, 24]] |
|[[B, 2], [ABE, 17]] |
|[[C, 3], [ABC, 33]] |
+--------------------+
我尝试使用 index 在这个数组结构数组上获取一个项目(这也是一个数组)。我尝试在 udf 下面根据索引获取值。如果第一行的索引为 0,那么我应该检索“[A, 1]”作为数组。
val getValueUdf = udf { (zippedFeature: Seq[Seq[String]], index: Int) => zippedFeature(index) }
但是我遇到了错误
data type mismatch: argument 1 requires array<array<string>> type, however, '`zipped_feature`' is of array<struct<_1:string,_2:string>> type.
当我打印出如下图所示的架构时
|-- zipped_feature: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- _1: string (nullable = true)
| | |-- _2: string (nullable = true)
有人可以帮助确定我在这里做错了什么。我想根据索引获取值(又是一个数组)。
【问题讨论】:
标签: scala apache-spark apache-spark-sql