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

Writing the UDTF Code and Exporting the JAR File

Step 1: Writing the UDTF Code

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

  • The UDTF must inherit UDTFHandler.
  • 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 is a Spark scheduling parameter that indicates which a function is deterministic. A deterministic function always returns the same result for the same input. This annotation can be applied at both the class level and the function level, with the function-level annotation taking precedence over the class-level annotation.

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

    • Function description:

      The function splits a string and generates a result set. It takes two string parameters: body and split.

      The main functionality is to split a body string using the delimiter specified by split, and combine each result substring with a fixed version tag (version, with the value tag).

      The function returns a two-dimensional array of objects, where each element is an array containing a split substring and a version tag.

    • Example code
      package com.huawei.demo;
      
      import com.huawei.spark.function.handlers.UDTFHandler;
      import com.huawei.spark.function.types.FunctionType;
      import com.huawei.spark.function.types.TypeResolve;
      
      
      public class UDTFDemo extends UDTFHandler {
          @FunctionType(deterministic = true)
          @TypeResolve("string,string->string,string")
          public Object[][] splitString(Object[] args) {
              String body = (String) args[0];
              String split = (String) args[1];
              String[] value = body.split(split);
              Object[][] result = new Object[value.length][];
              String version = "tag";
              for (int i = 0; i < value.length; i++) {
                  result[i] = new Object[] {value[i], version};
              }
              return result;
          }
      }

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 MyRemoteUDTF.jar file is the JAR file to be uploaded to FunctionGraph.

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

    In this example, the MyRemoteUDTF.jar file is generated in the D:\UDTFTest\MyRemoteUDTF\out\artifacts\MyRemoteUDTF_jar\ directory.

    Figure 4 Copying the file path