Develop Java UDFs with IntelliJ IDEA

更新时间:
复制 MD 格式

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:

  1. Install MaxCompute Studio

  2. Create a MaxCompute project connection

  3. Create a MaxCompute Java module

Procedure

  1. Write a Java UDF.

    1. In the Project area, right-click the module's source directory (i.e., src > main > java), and select New > MaxCompute Java.

      新建Java Class

    2. 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.classname to automatically generate a package.

    3. In the code editor, enter the following code.

      代码编辑区域

      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();
          }
      }
  2. Debug the UDF to ensure that it runs correctly.

    1. In the java directory, right-click your Java class and select Run.

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

      debug

      • 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.

    3. Click OK. The result is shown in the following figure.

  3. Register the MaxCompute UDF.

    1. Right-click the UDF Java file and select Deploy to server....

    2. 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.

    3. Click OK to complete the UDF registration.

  4. 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.调用UDFThe following SQL statement is an example.

    select Lower_test('ALIYUN');

    The result shows that the Lower_test Java UDF is ready to use.