Overview
Function Description
A remote user-defined function (UDF) refers to separating the execution logic of a UDF from the main computing engine. The main engine calls this service via standard interfaces such as HTTP, gRPC, or Thrift to complete the computation. By decoupling the computation logic from the main engine, remote UDF improves flexibility and reusability.
This section explains the steps to develop a remote UDF and call it from a computing engine, using FunctionGraph as the execution platform for the remote UDF.
Learn more:
Solution Benefits
The advantages of remote UDFs include risk isolation, resource decoupling, and cost optimization.
- Risk isolation: Modifying UDFs in critical production environments can introduce execution risks.
Remote UDFs 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 UDFs turn the default queue into a pure data channel, removing risks caused by UDFs and offering more cost-effective and secure core data flow services to users.
Notes and Constraints
Remote UDFs only affect single-row data and are suitable for single-input, single-output scenarios.
Environment Preparations
Before developing and debugging a remote UDF using FunctionGraph, you must install IDEA, configure Maven, download required JAR files, and set up the FunctionGraph service.
| 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/. |
Development Process
| No. | Step | Operation Interface | Description |
|---|---|---|---|
| 1 | Create a Maven project and configure the project structure. | IntelliJ IDEA | Write, debug, and compile the remote UDF code. |
| 2 | Write the remote UDF code. | ||
| 3 | Debug, compile, and pack the code into a JAR package. | ||
| 4 | Create a FunctionGraph function and upload the JAR file. | OBS console | Upload the generated JAR file to FunctionGraph. |
| 5 | Create a remote UDF. | DataArts Studio console | Create a UDF on the Develop Script page of the DataArts Studio console. |
| 6 | Verify and use the remote UDF. | DataArts Studio console | On the Develop Job page of the DataArts Studio console, use the created UDF in a Spark SQL job. |
Comparison of UDF and Remote UDF
- UDF: 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 UDF: 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.
| Feature | UDF | Remote UDF |
|---|---|---|
| Execution method | Runs locally on the query engine. | Executes through a function hosting service. |
| Security | Instability in UDF code may affect the query engine process. | Execution failures only impact the UDF itself, not the overall job process. |
| Performance | High execution efficiency | Comparable to local UDFs 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.
| 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) | Data expansion and complex structure parsing (e.g., splitting strings, expanding 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 users table has a column age with a value of 25, the function will output 26.
- UDAF example:
Calculates the maximum value of a column.
CREATE FUNCTION max AS 'com.example.MaxUDAF'; SELECT max(age) FROM users;
If the users table has a column age with values 25, 30, and 35, the function outputs 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 users table has a column json_column with values '{"name":"Alice","age":25},{"name":"Bob","age":30}', the function will output two rows, each containing name and age fields.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot