Help Center/ AI DataLake/ devg/ Spark UDF Development Guide/ Calling UDFs in Spark SQL Jobs
Updated on 2026-11-06 GMT+08:00

Calling UDFs in Spark SQL Jobs

Scenario

AI DataLake allows you to use Hive user-defined functions (UDFs) to query data. UDFs take effect only on a single row of data and are applicable to inserting and deleting a single data record.

Constraints

If you use a static class or interface in a UDF, add try catch to capture exceptions. Otherwise, package conflicts may occur.

Environment Preparations

Before you start, set up the development environment.

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.

Development Process

The UDF development process is as follows:

Figure 1 UDF development process
Table 2 Process description

No.

Step

Operation Interface

Description

1

Create a Maven project and configure the POM file.

IntelliJ IDEA

Write UDF code by referring to steps in Procedure.

2

Write UDF code.

3

Debug, compile, and pack the code into a JAR package.

4

Upload the JAR package to OBS.

OBS console

Upload the UDF JAR file to an OBS directory.

5

Create a UDF.

DataArts Studio console

Create a UDF on the Develop Script page of the DataArts Studio console.

6

Verify and use the UDF.

DataArts Studio console

On the Develop Script page of the DataArts Studio console, use the created UDF in a Spark SQL job.

Procedure

  1. Create a Maven project and configure the POM file. This step uses IntelliJ IDEA 2020.2 as an example.
    1. Start IntelliJ IDEA and choose File > New > Project.
      Figure 2 Creating a project
    2. Choose Maven, set Project SDK to 1.8, and click Next.
      Figure 3 Choosing Maven
    3. Set the project name, configure the storage path, and click Finish.
      Figure 4 Creating a project
    4. Add the following content to the pom.xml file.
      <dependencies>
              <dependency>
                  <groupId>org.apache.hive</groupId>
                  <artifactId>hive-exec</artifactId>
                  <version>1.2.1</version>
              </dependency>
      </dependencies>
      Figure 5 Adding configurations to the POM file

    5. Choose src > main and right-click the java folder. Choose New > Package to create a package and a class file.
      Figure 6 Creating a package and a class file

      Set the package name as you need. In this example, set Package to com.huawei.demo. Then, press Enter.

      Figure 7 Customizing a package

      Create a Java class file in the package path. In this example, the Java Class file is SumUdfDemo.

      Figure 8 Creating a Java class file
  2. Write UDF code. Pay attention to the following requirements when you implement the UDF:
    1. The UDF must inherit org.apache.hadoop.hive.ql.exec.UDF.
    2. The evaluate function must be implemented, which can be reloaded.

    For details about how to implement the UDF, see the following sample code:

    package com.huawei.demo;
    import org.apache.hadoop.hive.ql.exec.UDF;
      public class SumUdfDemo extends UDF {
        public int evaluate(int a, int b) {
         return a + b;
      }
     }
  3. Use IntelliJ IDEA to compile the code and pack it into the JAR package.
    1. Click Maven in the tool bar on the right, and click clean and compile to compile the code.

      After the compilation is successful, click package.

      Figure 9 Compiling and packaging

      The generated JAR package is stored in the target directory. In this example, the generated JAR package is named MyUDF-1.0-SNAPSHOT.jar and stored in the D:\AIDataLakeTest\MyUDF\target directory.

      Figure 10 Generating a JAR file
  4. Log in to the OBS console and upload the JAR file to the OBS path.

    The region of the OBS bucket to which the JAR file is uploaded must be the same as the region of the AI DataLake workspace. Cross-region operations are not allowed.

  5. Create the UDF.
    1. On the AI DataLake console, click DataArts Studio in the lower left corner.
    2. On the Develop Script page, create a Serverless Spark SQL script and select the endpoint, catalog, and database.
      Figure 11 Creating a Serverless Spark SQL script
    3. In the SQL editing area, enter the following command for creating the UDF:
      CREATE FUNCTION TestSumUDF AS 'com.huawei.demo.SumUdfDemo' using jar 'obs://aidatalake-test-obs01/MyUDF-1.0-SNAPSHOT.jar';
    4. Click Execute.
  6. Call the UDF.

    Use the UDF created in 5 in the SELECT statement as follows:

    SELECT TestSumUDF(1,2);
  7. (Optional) Delete the UDF.

    If the UDF is no longer used, run the following statement to delete it:

    DROP FUNCTION TestSumUDF;