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:
A connection to a MaxCompute project is created on MaxCompute Studio. For more information, see Manage project connections.
A MaxCompute Java module is created. For more information, see Create a MaxCompute Java module.
Background information
You can develop a UDF by following the steps in this topic, or create one directly by selecting .
Write a UDF
In the Project pane, right-click the source code directory of the module (that is, ), and select .
Enter a Name and select the type as UDF, then press Enter.
In the Create new MaxCompute java class dialog box, you can also select other class types, including UDAF, UDTF, Driver, mapper, reducer, StorageHandler, Extractor, and Outputer.
Name: The name of the MaxCompute Java class. To create a package, you can enter the name in the format of packagename.classname. This action automatically creates a package.
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.
Right-click the Java script and select Run.
On the Run/Debug Configurations page, configure the run parameters.
For this example, set MaxCompute table to
wc_in1, Table columns tocol1, and Download Record limit to100.MaxCompute project: Specifies the MaxCompute project to use for the UDF. For a local run, select local.
NoteIf 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.
Click OK to start running.
NoteThe 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
You can write test cases based on the unit testing examples in the examples directory.
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.