Spark SQL provides built-in standard Aggregate functions defines in DataFrame API, these come in handy when we need to make aggregate operations on DataFrame columns. Aggregate functions operate on a group of rows and calculate a single return value for every group.

All these aggregate functions accept input as, Column type or column name in a string and several other arguments based on the function and return Column type.

When possible try to leverage standard library as they are little bit more compile-time safety, handles null and perform better when compared to UDF’s. If your application is critical on performance try to avoid using custom UDF at all costs as these are not guarantee on performance.

Spark Aggregate Functions

Spark SQL Aggregate functions are grouped as “agg_funcs” in spark SQL. Below is a list of functions defined under this group. Click on each link to learn with a Scala example.

Note that each and every below function has another signature which takes String as a column name instead of Column .

Aggregate Functions Examples

First, let’s create a DataFrame to work with aggregate functions. All example provided here is also available at GitHub project.

import spark.implicits._ val simpleData = Seq(("James", "Sales", 3000), ("Michael", "Sales", 4600), ("Robert", "Sales", 4100), ("Maria", "Finance", 3000), ("James", "Sales", 3000), ("Scott", "Finance", 3300), ("Jen", "Finance", 3900), ("Jeff", "Marketing", 3000), ("Kumar", "Marketing", 2000), ("Saif", "Sales", 4100) ) val df = simpleData.toDF("employee_name", "department", "salary") df.show()

Yields below output.

+-------------+----------+------+ |employee_name|department|salary| +-------------+----------+------+ | James| Sales| 3000| | Michael| Sales| 4600| | Robert| Sales| 4100| | Maria| Finance| 3000| | James| Sales| 3000| | Scott| Finance| 3300| | Jen| Finance| 3900| | Jeff| Marketing| 3000| | Kumar| Marketing| 2000| | Saif| Sales| 4100| +-------------+----------+------+

approx_count_distinct Aggregate Function

approx_count_distinct() function returns the count of distinct items in a group.

//approx_count_distinct() println("approx_count_distinct: "+ df.select(approx_count_distinct("salary")).collect()(0)(0)) //Prints approx_count_distinct: 6

avg (average) Aggregate Function

avg() function returns the average of values in the input column.

//avg println("avg: "+ df.select(avg("salary")).collect()(0)(0)) //Prints avg: 3400.0

collect_list Aggregate Function

collect_list() function returns all values from an input column with duplicates.

//collect_list df.select(collect_list("salary")).show(false) +------------------------------------------------------------+ |collect_list(salary) | +------------------------------------------------------------+ |[3000, 4600, 4100, 3000, 3000, 3300, 3900, 3000, 2000, 4100]| +------------------------------------------------------------+

collect_set Aggregate Function

collect_set() function returns all values from an input column with duplicate values eliminated.

//collect_set df.select(collect_set("salary")).show(false) +------------------------------------+ |collect_set(salary) | +------------------------------------+ |[4600, 3000, 3900, 4100, 3300, 2000]| +------------------------------------+

countDistinct Aggregate Function

countDistinct() function returns the number of distinct elements in a columns

//countDistinct val df2 = df.select(countDistinct("department", "salary")) df2.show(false) println("Distinct Count of Department & Salary: "+df2.collect()(0)(0))

count function()

count() function returns number of elements in a column.

println("count: "+ df.select(count("salary")).collect()(0)) Prints county: 10

grouping function()

grouping() Indicates whether a given input column is aggregated or not. returns 1 for aggregated or 0 for not aggregated in the result. If you try grouping directly on the salary column you will get below error.

Exception in thread "main" org.apache.spark.sql.AnalysisException: // grouping() can only be used with GroupingSets/Cube/Rollup

first function()

first() function returns the first element in a column when ignoreNulls is set to true, it returns the first non-null element.

//first df.select(first("salary")).show(false) +--------------------+ |first(salary, false)| +--------------------+ |3000 | +--------------------+

last()

last() function returns the last element in a column. when ignoreNulls is set to true, it returns the last non-null element.

//last df.select(last("salary")).show(false) +-------------------+ |last(salary, false)| +-------------------+ |4100 | +-------------------+

kurtosis()

kurtosis() function returns the kurtosis of the values in a group.

df.select(kurtosis("salary")).show(false) +-------------------+ |kurtosis(salary) | +-------------------+ |-0.6467803030303032| +-------------------+

max()

max() function returns the maximum value in a column.

df.select(max("salary")).show(false) +-----------+ |max(salary)| +-----------+ |4600 | +-----------+

min()

min() function

df.select(min("salary")).show(false) +-----------+ |min(salary)| +-----------+ |2000 | +-----------+

mean()

mean() function returns the average of the values in a column. Alias for Avg

df.select(mean("salary")).show(false) +-----------+ |avg(salary)| +-----------+ |3400.0 | +-----------+

skewness()

skewness() function returns the skewness of the values in a group.

df.select(skewness("salary")).show(false) +--------------------+ |skewness(salary) | +--------------------+ |-0.12041791181069571| +--------------------+

stddev(), stddev_samp() and stddev_pop()

stddev() alias for stddev_samp .

stddev_samp() function returns the sample standard deviation of values in a column.

stddev_pop() function returns the population standard deviation of the values in a column.

df.select(stddev("salary"), stddev_samp("salary"), stddev_pop("salary")).show(false) +-------------------+-------------------+------------------+ |stddev_samp(salary)|stddev_samp(salary)|stddev_pop(salary)| +-------------------+-------------------+------------------+ |765.9416862050705 |765.9416862050705 |726.636084983398 | +-------------------+-------------------+------------------+

sum()

sum() function Returns the sum of all values in a column.

df.select(sum("salary")).show(false) +-----------+ |sum(salary)| +-----------+ |34000 | +-----------+

sumDistinct()

sumDistinct() function returns the sum of all distinct values in a column.

df.select(sumDistinct("salary")).show(false) +--------------------+ |sum(DISTINCT salary)| +--------------------+ |20900 | +--------------------+

variance(), var_samp(), var_pop()

variance() alias for var_samp

var_samp() function returns the unbiased variance of the values in a column.

var_pop() function returns the population variance of the values in a column.

df.select(variance("salary"),var_samp("salary"),var_pop("salary")) .show(false) +-----------------+-----------------+---------------+ |var_samp(salary) |var_samp(salary) |var_pop(salary)| +-----------------+-----------------+---------------+ |586666.6666666666|586666.6666666666|528000.0 | +-----------------+-----------------+---------------+

Source code of Spark SQL Aggregate Functions examples

package com.sparkbyexamples.spark.dataframe.functions.aggregate import org.apache.spark.sql.SparkSession import org.apache.spark.sql.functions._ object AggregateFunctions extends App { val spark: SparkSession = SparkSession.builder() .master("local[1]") .appName("SparkByExamples.com") .getOrCreate() spark.sparkContext.setLogLevel("ERROR") import spark.implicits._ val simpleData = Seq(("James", "Sales", 3000), ("Michael", "Sales", 4600), ("Robert", "Sales", 4100), ("Maria", "Finance", 3000), ("James", "Sales", 3000), ("Scott", "Finance", 3300), ("Jen", "Finance", 3900), ("Jeff", "Marketing", 3000), ("Kumar", "Marketing", 2000), ("Saif", "Sales", 4100) ) val df = simpleData.toDF("employee_name", "department", "salary") df.show() //approx_count_distinct() println("approx_count_distinct: "+ df.select(approx_count_distinct("salary")).collect()(0)(0)) //avg println("avg: "+ df.select(avg("salary")).collect()(0)(0)) //collect_list df.select(collect_list("salary")).show(false) //collect_set df.select(collect_set("salary")).show(false) //countDistinct val df2 = df.select(countDistinct("department", "salary")) df2.show(false) println("Distinct Count of Department & Salary: "+df2.collect()(0)(0)) println("count: "+ df.select(count("salary")).collect()(0)) //first df.select(first("salary")).show(false) //last df.select(last("salary")).show(false) //Exception in thread "main" org.apache.spark.sql.AnalysisException: // grouping() can only be used with GroupingSets/Cube/Rollup; //df.select(grouping("salary")).show(false) df.select(kurtosis("salary")).show(false) df.select(max("salary")).show(false) df.select(min("salary")).show(false) df.select(mean("salary")).show(false) df.select(skewness("salary")).show(false) df.select(stddev("salary"), stddev_samp("salary"), stddev_pop("salary")).show(false) df.select(sum("salary")).show(false) df.select(sumDistinct("salary")).show(false) df.select(variance("salary"),var_samp("salary"), var_pop("salary")).show(false) }

Conclusion

In this article, I’ve consolidated and listed all Spark SQL Aggregate functions with scala examples and also learned the benefits of using Spark SQL functions.

Happy Learning !!