layout | title | displayTitle | license |
---|---|---|---|
global |
User Defined Aggregate Functions (UDAFs) |
User Defined Aggregate Functions (UDAFs) |
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
|
User-Defined Aggregate Functions (UDAFs) are user-programmable routines that act on multiple rows at once and return a single aggregated value as a result. This documentation lists the classes that are required for creating and registering UDAFs. It also contains examples that demonstrate how to define and register UDAFs in Scala and invoke them in Spark SQL.
A base class for user-defined aggregations, which can be used in Dataset operations to take all of the elements of a group and reduce them to a single value.
IN - The input type for the aggregation.
BUF - The type of the intermediate value of the reduction.
OUT - The type of the final output result.
-
bufferEncoder: Encoder[BUF]
Specifies the Encoder for the intermediate value type.
-
finish(reduction: BUF): OUT
Transform the output of the reduction.
-
merge(b1: BUF, b2: BUF): BUF
Merge two intermediate values.
-
outputEncoder: Encoder[OUT]
Specifies the Encoder for the final output value type.
-
reduce(b: BUF, a: IN): BUF
Aggregate input value
a
into current intermediate value. For performance, the function may modifyb
and return it instead of constructing new object forb
. -
zero: BUF
The initial value of the intermediate result for this aggregation.
User-defined aggregations for strongly typed Datasets revolve around the Aggregator abstract class. For example, a type-safe user-defined average can look like:
Typed aggregations, as described above, may also be registered as untyped aggregating UDFs for use with DataFrames. For example, a user-defined average for untyped DataFrames can look like:
SHOW USER FUNCTIONS; +------------------+ | function| +------------------+ | default.myAverage| +------------------+
CREATE TEMPORARY VIEW employees USING org.apache.spark.sql.json OPTIONS ( path "examples/src/main/resources/employees.json" );
SELECT * FROM employees; +-------+------+ | name|salary| +-------+------+ |Michael| 3000| | Andy| 4500| | Justin| 3500| | Berta| 4000| +-------+------+
SELECT myAverage(salary) as average_salary FROM employees; +--------------+ |average_salary| +--------------+ | 3750.0| +--------------+
</div>
</div>
### Related Statements
* [Scalar User Defined Functions (UDFs)](sql-ref-functions-udf-scalar.html)
* [Integration with Hive UDFs/UDAFs/UDTFs](sql-ref-functions-udf-hive.html)