我想知道是否有某种方法可以为多列上的spark数据帧指定自定义聚合函数。
我有一个类似这样的表(名称,项目,价格):
john | tomato | 1.99
john | carrot | 0.45
bill | apple | 0.99
john | banana | 1.29
bill | taco | 2.59
为:
我想将每个人的项目和费用汇总到这样的列表中:
john | (tomato, 1.99), (carrot, 0.45), (banana, 1.29)
bill | (apple, 0.99), (taco, 2.59)
这在数据帧中是否可行?我最近了解了collect_list
,但似乎只适用于一列。
答案 0 :(得分:54)
在收集列表之前,请考虑使用struct
函数将列分组在一起:
import org.apache.spark.sql.functions.{collect_list, struct}
import sqlContext.implicits._
val df = Seq(
("john", "tomato", 1.99),
("john", "carrot", 0.45),
("bill", "apple", 0.99),
("john", "banana", 1.29),
("bill", "taco", 2.59)
).toDF("name", "food", "price")
df.groupBy($"name")
.agg(collect_list(struct($"food", $"price")).as("foods"))
.show(false)
输出:
+----+---------------------------------------------+
|name|foods |
+----+---------------------------------------------+
|john|[[tomato,1.99], [carrot,0.45], [banana,1.29]]|
|bill|[[apple,0.99], [taco,2.59]] |
+----+---------------------------------------------+
答案 1 :(得分:30)
以DataFrame
执行此操作的最简单方法是首先收集两个列表,然后将UDF
zip
两个列表一起使用。类似的东西:
import org.apache.spark.sql.functions.{collect_list, udf}
import sqlContext.implicits._
val zipper = udf[Seq[(String, Double)], Seq[String], Seq[Double]](_.zip(_))
val df = Seq(
("john", "tomato", 1.99),
("john", "carrot", 0.45),
("bill", "apple", 0.99),
("john", "banana", 1.29),
("bill", "taco", 2.59)
).toDF("name", "food", "price")
val df2 = df.groupBy("name").agg(
collect_list(col("food")) as "food",
collect_list(col("price")) as "price"
).withColumn("food", zipper(col("food"), col("price"))).drop("price")
df2.show(false)
# +----+---------------------------------------------+
# |name|food |
# +----+---------------------------------------------+
# |john|[[tomato,1.99], [carrot,0.45], [banana,1.29]]|
# |bill|[[apple,0.99], [taco,2.59]] |
# +----+---------------------------------------------+
答案 2 :(得分:5)
也许比zip
函数更好的方法(由于UDF和UDAF对性能非常不利,因此 )是将两列包装到Struct
中。
这可能也可以工作:
df.select('name, struct('food, 'price).as("tuple"))
.groupBy('name)
.agg(collect_list('tuple).as("tuples"))
答案 3 :(得分:2)
这是一个选项,可以将数据帧转换为Map的RDD,然后在其上调用groupByKey
。结果将是键值对列表,其中value是元组列表。
df.show
+----+------+----+
| _1| _2| _3|
+----+------+----+
|john|tomato|1.99|
|john|carrot|0.45|
|bill| apple|0.99|
|john|banana|1.29|
|bill| taco|2.59|
+----+------+----+
val tuples = df.map(row => row(0) -> (row(1), row(2)))
tuples: org.apache.spark.rdd.RDD[(Any, (Any, Any))] = MapPartitionsRDD[102] at map at <console>:43
tuples.groupByKey().map{ case(x, y) => (x, y.toList) }.collect
res76: Array[(Any, List[(Any, Any)])] = Array((bill,List((apple,0.99), (taco,2.59))), (john,List((tomato,1.99), (carrot,0.45), (banana,1.29))))
答案 4 :(得分:1)
collect_list似乎仅适用于一列:要使collect_list用于多个列,您必须将要聚合的列包装在结构中。 例如:
val aggregatedData = df.groupBy("name").agg(collect_list(struct("item", "price")) as("food"))
aggregatedData.show
+----+------------------------------------------------+
|name|foods |
+----+------------------------------------------------+
|john|[[tomato, 1.99], [carrot, 0.45], [banana, 1.29]]|
|bill|[[apple, 0.99], [taco, 2.59]] |
+----+------------------------------------------------+