All Products
Search
Document Center

MaxCompute:Develop a UDF

Last Updated:Dec 15, 2023

If the built-in functions provided by MaxCompute cannot meet your business requirements, you can develop user-defined functions (UDFs) in MaxCompute Studio to implement business features. This topic describes how to use MaxCompute Studio to develop and debug Java UDFs.

Prerequisites

Before you begin, make sure that you have completed the following operations:

Background information

You can follow the instructions in this topic to develop a UDF, or choose MaxCompute > Create UDF to directly create a UDF.

Write a UDF

  1. In the left-side navigation pane of the Project tab, choose src > main > java, right-click java, and then choose New > MaxCompute Java.

    11

  2. Specify Name, select the UDF class, and then press Enter.

    新建Class

    Name: the name of the MaxCompute Java class. If you need to create a package, enter packagename.classname. Then, the system automatically creates a package.

  3. After you create a MaxCompute Java class, develop a Java program in the editor.

    编写程序

Perform a local run to debug the UDF

Perform a local run to test the UDF and check whether the results meet your expectations.

  1. Right-click the Java script that you wrote and select Run.

  2. In the Run/Debug Configurations dialog box, configure the runtime parameters.

    debug

    • MaxCompute project: the MaxCompute project in which the UDF runs. To perform a local run, select local from the drop-down list.

      Note
      • If you want to use table data in a MaxCompute project, you must modify the endpoint and specify a project name in the MaxCompute project field. If the table data in the MaxCompute project is not downloaded to the warehouse directory, the data is downloaded first. If the data is already downloaded, skip this step.

      • If you select local from the drop-down list of MaxCompute project, interaction between MaxCompute data or resources and the local project is not required. This way, you can develop, test, and debug UDFs in the local environment.

    • MaxCompute table: the name of the MaxCompute table in which the UDF runs.

    • Table columns: the columns in the MaxCompute table in which the UDF runs.

  3. Click OK to run the UDF.

    Note
    • The system reads data from the specified table in warehouse as the input during the local run. You can view the log output in the console.

Perform unit testing to debug the UDF

Refer to the unit testing examples in the examples directory and write your test case.**

What to do next

After you develop and debug a UDF, you must package, upload, and register the UDF code to make the UDF available in MaxCompute. For more information, see Package a Java program, upload the package, and create a MaxCompute UDF.

References

For more information about how to use Python to develop UDFs in MaxCompute Studio, see Develop a Python UDF.