Updated on 2026-11-06 GMT+08:00

Overview

Function Description

Remote User-Defined Aggregate Functions (Remote UDAFs) apply to multiple-in-one-out service scenarios. That is, remote UDAFs aggregate multiple input records into one output value.

This section explains the steps to develop a remote UDAF and call it from a computing engine, using FunctionGraph as the execution platform for the remote UDAF.

Solution Benefits

The advantages of remote UDAFs include risk isolation, resource decoupling, and cost optimization.

  • Risk isolation: Although technically feasible, executing UDAFs in critical production environments can introduce execution risks.

    Remote UDAFs offload the computing logic to FunctionGraph, maintaining service functionality while isolating risks through a serverless architecture.

  • Resource decoupling: For resource-intensive tasks like large model inference, remote UDFs avoid long-term resource occupation in Spark clusters. This ensures stability, cost-effectiveness, and functionality by offloading heavy computations to FunctionGraph.
  • Cost optimization: Remote UDAFs turn the default queue into a pure data channel, removing risks caused by UDAFs and offering more cost-effective and secure core data flow services to users.

Environment Preparations

Before developing and debugging a remote UDAF using FunctionGraph, you must install IDEA, configure Maven, download required JAR files, and set up the FunctionGraph service.

Table 1 Development environment

Preparation Item

Description

Operating system (OS)

Windows 7 or a later version is required.

JDK

JDK 1.8 is required.

IntelliJ IDEA

IntelliJ IDEA 2019.1 or a compatible version is required.

Maven

Basic configurations of the development environment. Maven is used for project management throughout the lifecycle of software development.

Dependency JAR file download

Download the dependency JAR file spark-function-sdk_2.13-jar-with-dependencies.jar

from https://repo.huaweicloud.com/repository/maven/huaweicloudsdk/.

Comparison of UDAF and Remote UDAF

  • UDAF: Ideal for high-performance scenarios where the development team is familiar with the query engine's language. It offers high execution efficiency but has lower flexibility and security.
  • Remote UDAF: Best for scenarios that require external service calls, multi-language development, or high flexibility in the development language. It provides better security and greater development flexibility.
Table 2 Comparison of UDAF and remote UDAF

Feature

UDAF

Remote UDAF

Execution method

Runs locally on the query engine.

Executes through a function hosting service.

Security

Instability in UDAF code may affect the query engine process.

Execution failures only impact the UDAF itself, not the overall job process.

Performance

High execution efficiency

Comparable to local UDAFs in vectorized and batch modes.

Development flexibility

Highly coupled with query engine code, with limited flexibility.

Highly flexible and applicable to external services or libraries.

Development cost

Requires familiarity with the query engine, with high development cost.

Lower development cost due to reduced complexity.

Use case

High-performance scenarios where the team knows the query engine languages.

Scenarios needing flexible development languages or external service calls.

Comparison of UDF, UDAF, and UDTF

  • UDF: A function that computes the sum of two numbers.
  • UDAF: A function that computes an aggregate value, such as an average.
  • UDTF: A function that splits a string containing multiple values into multiple rows.
Table 3 Comparison of UDF, UDAF, and UDTF

Feature

User-Defined Function (UDF)

User-Defined Aggregate Function (UDAF)

User-Defined Table-Generating Function (UDTF)

Input parameters

One or more fields of a single row

Multiple fields from multiple rows

One or more fields of a single or multiple rows

Output result

Single scalar value

Single aggregate result

Multiple rows, each with multiple fields

Main use

Simple data transformation and computation (e.g., string manipulation, math)

Data aggregation (e.g., sum, average, count)

Expanding data or parsing complex structures (e.g., splitting strings, flattening arrays)

  • UDF example:

    Creates a function that adds 1 to the input number.

    CREATE FUNCTION add_one AS 'com.example.AddOneUDF';
    SELECT add_one(age) FROM users; 

    If the age column in the users table is 25, the output will be 26.

  • UDAF example:

    Calculates the maximum value of a column.

    CREATE FUNCTION max AS 'com.example.MaxUDAF';
    SELECT max(age) FROM users;

    If the age values in the users table are 25, 30, and 35, the output will be 35.

  • UDTF example:

    Parses a JSON string into multiple rows.

    CREATE FUNCTION parse_json AS 'com.example.ParseJsonUDTF';
    SELECT parse_json(json_column) FROM users;

    If the json_column column in the users table contains '{"name":"Alice","age":25},{"name":"Bob","age":30}', the output will be two rows, each containing name and age fields.