IntelliJ IDEA is an integrated development environment (IDE) for Java used to quickly develop Java programs. This topic shows you how to use MaxCompute Studio, a plug-in for IntelliJ IDEA, to develop a Java user-defined function (UDF) that converts uppercase letters to lowercase letters.
Prerequisites
Ensure that you have completed the following preparations in IntelliJ IDEA:
Procedure
Write a Java UDF.
In the Project area, right-click the module's source directory (i.e., ), and select .
In the Create new MaxCompute java class dialog box, click UDF, enter a Name, and then press Enter. For example, the Java class name is Lower.
The Name field specifies the name of the MaxCompute Java class. If you have not created a package, you can enter
packagename.classnameto automatically generate a package.In the code editor, enter the following code.
The following code is an example of the UDF.
package <packagename>; import com.aliyun.odps.udf.UDF; public final class Lower extends UDF { public String evaluate(String s) { if (s == null) { return null; } return s.toLowerCase(); } }
Debug the UDF to ensure that it runs correctly.
In the java directory, right-click your Java class and select Run.
In the Run/Debug Configurations dialog box, configure the run parameters.
MaxCompute project: The MaxCompute project in which to run the UDF. Select local to run the UDF locally.
MaxCompute table: The name of the MaxCompute table to use.
Table columns: The columns of the MaxCompute table to use.
Click OK. The result is shown in the following code.
package wd_udf; import com.aliyun.odps.udf.UDF; public class Lower extends UDF { public String evaluate(String s) { if ("null".equals(s) ) { return null; } return s.toLowerCase(); } }
Register the MaxCompute UDF.
Right-click the UDF Java file and select Deploy to server....
In the Package a jar, submit resource and register function dialog box, configure the following parameters.
MaxCompute project: The MaxCompute project that contains the UDF. Keep the default value, as the plug-in automatically selects the current project.
Resource file: The path of the resource file required by the UDF. You can keep the default value.
Resource name: The resource required by the UDF. You can keep the default value.
Function name: The name used to call the UDF in SQL statements. For example,
Lower_test.
Click OK to complete the UDF registration.
Call the UDF.
In the left-side navigation pane, click Project Explorer. Right-click your target MaxCompute project and select Open in Console. In the Console, enter an SQL statement to call the UDF and press Enter.
package com.aliyun.odps.udf.example; import com.aliyun.odps.udf.UDF; public final class Lower extends UDF { public String evaluate(String s) { if (s == null) { return null; } return s.toLowerCase(); } }Run the following command in the Console:
select lower_test('ABC');The following SQL statement is an example.
select Lower_test('ALIYUN');The result shows that the
Lower_testJava UDF is ready to use.+------------+ | _c0 | +------------+ | aliyun | +------------+ 1 records (at most 10000 supported) fetched by instance tunnel.