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

Writing the UDF Code and Exporting the JAR File

Step 1: Writing the UDF Code

Pay attention to the following requirements when you implement the UDF:

  • The UDF must inherit UDFHandler.
  • The @TypeResolve annotation is added to the method to indicate the input and output parameters of the function.
    Input and output parameters are separated by an arrow (->). For details about the input and output parameter types (resolveType) and the corresponding SQL and Java data types, see Table 1.
    Table 1 resolveType

    SQL type

    Java type

    resolveType

    BooleanType

    Boolean.class

    BOOLEAN

    ByteType

    Byte.class

    BYTE

    ShortType

    Short.class

    SHORT

    IntegerType

    Integer.class

    INT

    LongType

    Long.class

    LONG

    FloatType

    Float.class

    FLOAT

    DoubleType

    Double.class

    DOUBLE

    StringType

    String.class

    STRING

    BinaryType

    byte[].class

    BINARY

    DecimalType.Fixed(precision, scale)

    BigDecimal.class

    DECIMAL

    DateType

    LocalDate.class

    DATE

    TimestampType

    ZonedDateTime.class

    TIMESTAMP

    TimestampNTZType

    LocalDateTime.class

    TIMESTAMP_NTZ

    YearMonthIntervalType

    Period.class

    INTERVAL_YEAR_MONTH

    DayTimeIntervalType

    Duration.class

    INTERVAL_DAY_TIME

    List

    List.class

    LIST

    Map

    Map.class

    MAP

    Struct

    Map.class

    STRUCT

  • Add the @deterministic annotation.

    The @deterministic annotation indicates that the function is deterministic, that is, it always returns the same result for the same input.

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

    • Description: Implements the addition of two integers and returns the result.
    • Example code:
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      package com.huawei.demo;
      
      import com.huawei.spark.function.types.FunctionType;
      import com.huawei.spark.function.types.TypeResolve;
      import com.huawei.spark.function.handlers.UDFHandler;
      
      @FunctionType(deterministic = true)
      public class UDFDemo extends UDFHandler {
          @FunctionType(deterministic = false)
          @TypeResolve("INT, INT->INT")
          public Integer testIntegerTypeAdd(Integer col1, Integer col2) {
              return col1 + col2;
          }
      }
      
    • Parameters:

      col1: The first integer parameter (of integer type)

      col2: The second integer parameter (of integer type)

    • Return value: The arithmetic sum of col1 and col2

Step 2: Exporting the JAR Package

Use IntelliJ IDEA to compile the code and pack it into the JAR package.

  1. Click Build. On the displayed page, click Build Project and Build Artifacts... to compile and pack the code, respectively.
    Figure 1 Compiling and packing
  1. In the displayed dialog box, click Build.
    Figure 2 Compiling and packing
  1. After the preceding steps are complete, locate the out folder in the project directory and expand it.

    The MyRemoteUDF.jar file is the JAR file to be uploaded to FunctionGraph.

    Figure 3 Viewing MyRemoteUDF.jar
  2. Right-click MyRemoteUDF.jar and choose Copy Path/Reference from the shortcut menu to copy its path.

    In this example, the MyRemoteUDF.jar file is generated in the D:\UDFTest\MyRemoteUDF\out\artifacts\MyRemoteUDF_jar\ directory.

    Figure 4 Copying the file path