spark scala创建键值对

spark scala创建键值对,scala,apache-spark,Scala,Apache Spark,我有这样的模式: root |-- id: string (nullable = true) |-- info: array (nullable = true) | |-- element: struct (containsNull = true) | | |-- _1: string (nullable = true) | | |-- _2: long (nullable = false)

我有这样的模式:

       root
      |-- id: string (nullable = true)
      |-- info: array (nullable = true)
      |    |-- element: struct (containsNull = true)
      |    |    |-- _1: string (nullable = true)
      |    |    |-- _2: long (nullable = false)
      |    |    |-- _3: string (nullable = true)
Info是一个结构数组。在按
id
分组后,我想将
info\u 1
作为键,将
info\u 2
info\u 3
作为值。因此,o/p应如下所示:

id,[[info[0]_1:{info[0]_2,info[0]_3}],[info[1]_1:{{info[1]_2,info[1]_3},...]

请提供帮助。

这应该让您开始(UDF方法):


我真的不明白你说的“分组后”是什么意思。如果要在按id分组后连接数组,则需要
收集\u列表
,然后使用udf首先连接(并展平)数组

是否为此尝试了代码?请与我们分享你的错误。不,我还没有尝试过。你能分享一些示例输入吗?
val df = Seq(
      ("1", Seq(("a", 1L, "b"), ("c", 2L, "d"))
  )
).toDF("id", "info")


df.show()

+---+------------------+
| id|              info|
+---+------------------+
|  1|[[a,1,b], [c,2,d]]|
+---+------------------+


val transformStructToMap = udf((structarray : Seq[Row]) => {
    structarray.map(r =>
        (r.getString(0), // key
        (r.getLong(1),r.getString(2))) // values
     ).toMap
 })

df.select(
   $"id",
   transformStructToMap($"info").as("info")
 ).show()

+---+---------------------------+
|id |info                       |
+---+---------------------------+
|1  |Map(a -> [1,b], c -> [2,d])|
+---+---------------------------+