【问题标题】:apache spark agg( ) functionapache spark agg() 函数
【发布时间】:2023-04-09 06:59:01
【问题描述】:

对于示例数据框scholor

scala> scholor.show

| id|  name|age|sal|base|

对于上面,下面都给出相同的输出。那么 agg() 将有什么用处。它只是为了名字。

scala> scholor.groupBy("age").sum("base").show      /*with out agg */

scala> scholor.groupBy("age").agg(sum("base")).show        /* with agg */
+---+---------+
|age|sum(base)|
+---+---------+

agg() 是否需要任何可变参数作为参数? agg()需要什么?

提前致谢。

【问题讨论】:

  • 你需要聚合什么?结果是每个年龄分组的碱基总和。这已经是一个聚合了。
  • @JoostdenBoer:是的。 sum , min,max,mean 等......已经是聚合函数。那么为什么 dataframe api 提供 agg() 方法。我想,它意味着什么......搜索那个东西!

标签: scala apache-spark-sql


【解决方案1】:

为了写.sum,这个方法必须存在。它在 API 上是硬编码的。使用.agg可以提供其他聚合函数,sum("column")只是其中之一。

【讨论】:

    【解决方案2】:

    aggsource code,“ds.agg(...) 是 ds.groupBy().agg(...) 的简写”。


    聚合函数 avgmaxminsumcount 不是可以在 DataFrame 上调用的方法:

    scala> my_df.min("column")
    <console> error: value min is not a member of org.apache.spark.sql.DataFrame
    

    agg 是一个 DataFrame 方法,它接受这些聚合函数作为参数:

    scala> my_df.agg(min("column"))
    res0: org.apache.spark.sql.DataFrame = [min(column): double]
    

    在 DataFrame 上调用 groupBy() 会返回一个 RelationalGroupedDataset,其中包含这些聚合函数作为方法(groupBy 的源代码):

    scala> my_df.groupBy().min("column")
    res1: org.apache.spark.sql.DataFrame = [min(column): double]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 2016-12-27
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多