我正在尝试逐组聚合。使用Spark 1.5.2
你能告诉我为什么不起作用吗。
中是一个数据帧。
scala> in
res28: org.apache.spark.sql.DataFrame = [id: int, city: string]
scala> in.show
+---+--------+
| id| city|
+---+--------+
| 10|Bathinda|
| 20|Amritsar|
| 30|Bathinda|
+---+--------+
scala>in.groupBy("city").agg(Map{
| "id" -> "sum"
| }).show(true)
+----+-------+
|city|sum(id)|
+----+-------+
+----+-------+
谢谢,
我预计输出应该有城市和id 的总和
编辑:我不知道为什么下次我创建新的火花壳时它会起作用
考虑以下数据帧:
val in = sc.parallelize(Seq(
(10, "Bathinda"), (20, "Amritsar"), (30, "Bathinda"))).toDF("id", "city")
你可以看到,这些代码行将给出相同的输出
scala> in.groupBy("city").agg(Map("id" -> "sum")).show
+--------+-------+
| city|sum(id)|
+--------+-------+
|Bathinda| 40|
|Amritsar| 20|
+--------+-------+
scala> in.groupBy("city").agg(Map{ "id" -> "sum"}).show
+--------+-------+
| city|sum(id)|
+--------+-------+
|Bathinda| 40|
|Amritsar| 20|
+--------+-------+
scala> in.groupBy("city").agg(Map{ "id" -> "sum"}).show(true)
+--------+-------+
| city|sum(id)|
+--------+-------+
|Bathinda| 40|
|Amritsar| 20|
+--------+-------+
scala> in.groupBy("city").agg(sum($"id")).show(true)
+--------+-------+
| city|sum(id)|
+--------+-------+
|Bathinda| 40|
|Amritsar| 20|
+--------+-------+
scala> in.groupBy("city").agg(sum(in("id"))).show(true)
+--------+-------+
| city|sum(id)|
+--------+-------+
|Bathinda| 40|
|Amritsar| 20|
+--------+-------+
注意:show参数默认为false,它只关心是否显示整个字段值。(有时字段太长,您只需要预览)