You can use built-in functions or user-defined functions (UDFs) of MaxCompute for data computing. Built-in functions can be called directly. UDFs can only be called after being customized. This topic describes common operations on functions, such as create, delete, and view functions.
Operation | Description | Role | Operation platform |
---|---|---|---|
Create a function | Creates UDFs in a MaxCompute project. | Users who have the WRITE permission on functions | You can execute the statements described in this topic on the following platforms: |
Delete a function | Deletes an existing UDF from a MaxCompute project. | Users who have the DELETE permission on functions | |
View a function | Views the information of a specified function in a MaxCompute project. | Users who have the READ permission on functions | |
View the function list | Views the information of all UDFs in a MaxCompute project. | Users who have the LIST permission on objects in a project |
Create a function
Creates a UDF in a MaxCompute project.
- Limits
- Function names must be unique. Each function name can be used only once.
- UDFs cannot overwrite built-in functions of MaxCompute. Only the project owner is allowed to overwrite built-in functions with UDFs. If you use a UDF that overwrites a built-in function, warning information is displayed in Summary of the Logview of your job after the SQL statement is executed.
- Syntax
create function <function_name> as <'package_to_class'> using <'resource_list'>;
- Parameters
- function_name: required. The name of the UDF that you want to create.
- package_to_class: required. The name of the package. It must be enclosed in single quotation marks
(').
- For a Java UDF, specify this name as a fully qualified class name from the top-level package name to the UDF class name.
- For a Python UDF, specify this name in the format of Python script name.Class name.
- resource_list: required. The list of resources used by the UDF.
- The resource list must include the resource that contains UDF code. Make sure that this resource is uploaded to MaxCompute.
- If the code calls the Distributed Cache API to read resource files, this resource list must also contain the list of resource files that are read by the UDF.
- The resource list consists of multiple resource names and must be enclosed in single quotation marks ('). The resource names are separated by commas (,).
- To specify the project that contains the resource, write the expression in the
<project_name>/resources/<resource_name>
format.
- Examples
- Example 1: Create the
my_lower
function. In this example, the Java UDF classorg.alidata.odps.udf.examples.Lower
is in my_lower.jar.create function my_lower as 'org.alidata.odps.udf.examples.Lower' using 'my_lower.jar';
- Example 2: Create the
my_lower
function. In this example, the Python UDF class MyLower is in the pyudf_test.py script of thetest_project
project.create function my_lower as 'pyudf_test.MyLower' using 'test_project/resources/pyudf_test.py';
- Example 3: Create the
test_udtf
function. In this example, the Java UDF classcom.aliyun.odps.examples.udf.UDTFResource
is in udtfexample1.jar. The function depends on the file resource file_resource.txt, the table resource table_resource1, and the archive resource test_archive.zip.create function test_udtf as 'com.aliyun.odps.examples.udf.UDTFResource' using 'udtfexample1.jar, file_resource.txt, table_resource1, test_archive.zip';
- Example 1: Create the
Delete a function
Deletes an existing UDF from a MaxCompute project.
- Syntax
drop function <function_name>;
- Parameters
function_name: required. The name of an existing function.
- Example
-- Delete the my_lower function. drop function my_lower;
View a function
Views the information of a specified function in a MaxCompute project. The information includes the function name, owner, creation time, class name, and resource list.
- Syntax
desc function <function_name>;
- Parameters
function_name: required. The name of an existing function.
- Example
Returned results:-- View the information of the my_lower function. desc function my_lower;
Name my_lower Owner ALIYUN$**** Created Time 2020-06-18 15:50:19 Class org.alidata.odps.udf.examples.Lower Resources project_name/my_lower.jar
View the function list
Views the information of all UDFs in a MaxCompute project.
- Syntax
list functions [-p <project_name>];
- Parameters
project_name: optional. The name of a MaxCompute project.
- Example
Returned results:list functions;
Name Owner Create Time Class Resources 0 functions