【发布时间】:2018-02-27 17:28:23
【问题描述】:
我有这种结构的数据框
val df = Seq(
("john", "tomato", 1),
("john", "carrot", 4),
("bill", "apple", 1),
("john", "tomato", 2),
("bill", "taco", 2)
).toDF("name", "food", "price")
我需要像这样聚合嵌套列表
name | acc |
-----+---------------------------+
john |[(tomato, 3), (carrot, 4)] |
bill |[(apple, 1), (taco,2 )] |
我尝试过这种方式,但它不正确。
dff.groupBy($"name")
.agg(collect_list(struct($"food", $"price")).as("foods"))
.show(false)
+----+------------------------------------+
|name|set |
+----+------------------------------------+
|john|[[tomato,1], [carrot,4], [tomato,2]]|
|bill|[[apple,1], [taco,2]] |
+----+------------------------------------+
我怎样才能得到它?
【问题讨论】:
标签: scala apache-spark apache-spark-sql