我想在spark (Spark2.1)中的一列上应用一个sum。我有两种方法:
1-具有窗口功能:
val windowing = Window.partitionBy("id")
dataframe
.withColumn("sum", sum(col("column_1")) over windowing)2-具有agg职能:
dataframe
.groupBy("id")
.agg(sum(col("column_1")).alias("sum"))就表演而言,最好的方法是什么?这两种方法有什么不同?
发布于 2019-04-03 11:55:53
您可以在窗口内使用聚合函数(第一个案例),也可以在分组时使用聚合函数(第二个案例)。不同之处在于,对于一个窗口,每个行将与在其整个窗口上计算的聚合结果相关联。但是,当分组时,每个组将与该组的聚合结果相关联(一组行仅成为一行)。
在你的情况下,你会得到这个。
val dataframe = spark.range(6).withColumn("key", 'id % 2)
dataframe.show
+---+---+
| id|key|
+---+---+
| 0| 0|
| 1| 1|
| 2| 0|
| 3| 1|
| 4| 0|
| 5| 1|
+---+---+案例1:窗口
val windowing = Window.partitionBy("key")
dataframe.withColumn("sum", sum(col("id")) over windowing).show
+---+---+---+
| id|key|sum|
+---+---+---+
| 0| 0| 6|
| 2| 0| 6|
| 4| 0| 6|
| 1| 1| 9|
| 3| 1| 9|
| 5| 1| 9|
+---+---+---+案例2:分组
dataframe.groupBy("key").agg(sum('id)).show
+---+-------+
|key|sum(id)|
+---+-------+
| 0| 6|
| 1| 9|
+---+-------+发布于 2021-04-10 10:24:24
正如@Oli提到的,聚合函数可以在窗口中使用(第一种情况),也可以用于分组(第二种情况)。在性能上,“带分组的聚合函数”要比“带窗口的聚合函数”快得多。我们可以通过分析物理计划来可视化这一点。
df.groupBy("id").agg(sum($"expense").alias("total_expense")).explain()
df.show
+---+----------+
| id| expense|
+---+----------+
| 1| 100|
| 2| 300|
| 1| 100|
| 3| 200|
+---+----------+1-通过窗口进行聚合:
df.withColumn("total_expense", sum(col("expense")) over window).show
+---+----------+-------------------+
| id| expense| total_expense|
+---+----------+-------------------+
| 3| 200| 200|
| 1| 100| 200|
| 1| 100| 200|
| 2| 300| 300|
+---+----------+-------------------+
df.withColumn("total_expense", sum(col("expense")) over window).explain
== Physical Plan ==
Window [sum(cast(expense#9 as bigint)) windowspecdefinition(id#8, specifiedwindowframe(RowFrame, unboundedpreceding$(), unboundedfollowing$())) AS total_expense#265L], [id#8]
+- *(2) Sort [id#8 ASC NULLS FIRST], false, 0
+- Exchange hashpartitioning(id#8, 200), true, [id=#144]
+- *(1) Project [_1#3 AS id#8, _2#4 AS expense#9]
+- *(1) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, knownnotnull(assertnotnull(input[0, scala.Tuple2, true]))._1, true, false) AS _1#3, knownnotnull(assertnotnull(input[0, scala.Tuple2, true]))._2 AS _2#4]
+- Scan[obj#2]2-使用GroupBy进行聚合:
df.groupBy("id").agg(sum($"expense").alias("total_expense")).show
+---+------------------+
| id| total_expense|
+---+------------------+
| 3| 200|
| 1| 200|
| 2| 300|
+---+------------------+
df.groupBy("id").agg(sum($"expense").alias("total_expense")).explain()
== Physical Plan ==
*(2) HashAggregate(keys=[id#8], functions=[sum(cast(expense#9 as bigint))])
+- Exchange hashpartitioning(id#8, 200), true, [id=#44]
+- *(1) HashAggregate(keys=[id#8], functions=[partial_sum(cast(expense#9 as bigint))])
+- *(1) Project [_1#3 AS id#8, _2#4 AS expense#9]
+- *(1) SerializeFromObject [staticinvoke(class org.apache.spark.unsafe.types.UTF8String, StringType, fromString, knownnotnull(assertnotnull(input[0, scala.Tuple2, true]))._1, true, false) AS _1#3, knownnotnull(assertnotnull(input[0, scala.Tuple2, true]))._2 AS _2#4]
+- Scan[obj#2]根据执行计划,我们可以看到,在windows情况下,有一个总的洗牌和一个类型,而在groupby情况下,有一个减少的洗牌(在本地聚合partial_sum之后的洗牌)。
https://stackoverflow.com/questions/55493937
复制相似问题