You can create user-defined functions (UDFs) and use them in MaxCompute SQL.
Object model
PyODPS represents UDFs with two types of objects:
-
MaxCompute entry object (
o): exposes project-level methods —create_function(),delete_function(),get_function(),list_functions(), andexist_function(). -
Function object: exposes instance-level methods —
update()anddrop()— and holds properties such asclass_typeandresources.
Understanding which object owns which method helps you write correct PyODPS code: always call project-level operations on o, and call instance-level operations on a function object you retrieved first.
Create a UDF
Call create_function() on the MaxCompute entry object. Pass the function name, the implementing class (class_type), and a list of resources.
The following examples create a UDF that references a resource in the current project and in another project:
# Reference a resource in the current project.
resource = o.get_resource('my_udf.py')
function = o.create_function('test_function', class_type='my_udf.Test', resources=[resource])
# Reference a resource in another project.
resource2 = o.get_resource('my_udf.py', project='another_project')
function2 = o.create_function('test_function2', class_type='my_udf.Test', resources=[resource2])
List UDFs
Call list_functions() on the MaxCompute entry object to get all UDFs in a specific project.
for func in o.list_functions():
print(func.name)
Check whether a UDF exists
Call exist_function() on the MaxCompute entry object.
o.exist_function('test_function')
Get a UDF
Call get_function() on the MaxCompute entry object to retrieve a function object.
function = o.get_function('test_function')
Update a UDF
Get the function object first, then modify its class_type and resources properties, and call update().
function = o.get_function('test_function')
new_resource = o.get_resource('my_udf2.py')
function.class_type = 'my_udf2.Test'
function.resources = [new_resource, ]
function.update() # Update the function.
Delete a UDF
Two methods are available:
-
delete_function()on the entry object: use this when you only have the function name. -
drop()on the function object: use this when you already hold a function object.
# Delete by name.
o.delete_function('test_function')
# Delete using the function object.
function.drop() # Call the drop() method if the function exists.