Делает ли это то, что вы хотите?
scala> case class itemData (iid: Int, color: String)
defined class itemData
scala> case class userRecs (uid: Int, recommendations: Array[itemData])
defined class userRecs
scala> val df = spark.createDataset(Seq(
(41344966,1305,"red"),
(41344966,1305,"green"),
(41344966,2174,"red"),
(41345063,2174,"green"),
(41346177,2996,"orange"),
(41349171,2174,"purple")
)).toDF("uid", "iid", "color")
df: org.apache.spark.sql.DataFrame = [uid: int, iid: int ... 1 more field]
scala> (df.select($"uid", struct($"iid",$"color").as("itemData"))
.groupBy("uid")
.agg(collect_list("itemData").as("recommendations"))
.as[userRecs]
.show())
+--------+--------------------+
| uid| recommendations|
+--------+--------------------+
|41344966|[[1305, red], [13...|
|41345063| [[2174, green]]|
|41346177| [[2996, orange]]|
|41349171| [[2174, purple]]|
+--------+--------------------+