Updated on 2022-07-04 GMT+08:00

Using UDFs

DLI allows you to query data by using the user-defined functions (Hive UDF).

Procedure

  1. Compile a UDF.

    Create or modify the content of SumUdfDemo.java in the sample code based on service requirements.

  2. Generate a JAR package, set the output JAR package to TestUDF.jar, and run the Build Artifacts command.

    A file named TestUDF.jar is generated in the corresponding path, for example, udfDemo\target\artifacts\TestUDF.

  3. Upload TestUDF.jar to OBS. For details about how to upload data to OBS, see Step 2: Upload Data to OBS in Submitting a SQL Job.
  4. Create a function.

    Run the following command on the management console to create a function:

    CREATE FUNCTION fun1 AS 'com.demo.SumUdfDemo' using jar 'obs://udf/TestUDF.jar';
  5. Use the created function.

    Run the following statement to query using the function created in 4.

    select fun1(ip) from ip_tables;
  6. Delete the created function.

    If this function is no longer used, run the following statement to delete the function:

    Drop FUNCTION fun1;

Sample Code

The sample code in SumUdfDemo.java is as follows:

package com.demo;
  import org.apache.hadoop.hive.ql.exec.UDF;
  public class SumUdfDemo extends UDF {
    public Int evaluate(Int a, Int b) {
     return a + b;
  }
 }