跳到主要内容

Functions

Functions 类用于管理 MaxCompute 项目中的用户自定义函数 (UDF)。通过此类可以创建、更新、删除和查询函数。

获取实例​

Functions functions = odps.functions();

方法​

create​

创建函数。

public void create(Function function) throws OdpsException
public void create(String projectName, Function function) throws OdpsException
public void create(String projectName, String schemaName, Function function) throws OdpsException

参数:

参数类型说明
functionFunction函数定义对象
projectNameString目标项目名称(可选)
schemaNameString目标 Schema 名称(可选)

Function 对象构建:

Function function = new Function();
function.setName("my_function");
function.setClassPath("com.example.MyFunction");
function.setResources(Arrays.asList("resource1.jar", "resource2.py"));

get​

获取函数对象。

public Function get(String functionName)
public Function get(String projectName, String functionName)
public Function get(String projectName, String schemaName, String functionName)

参数:

参数类型说明
functionNameString函数名称
projectNameString项目名称(可选)
schemaNameStringSchema 名称(可选)

返回值:Function 对象


exists​

检查函数是否存在。

public boolean exists(String functionName) throws OdpsException
public boolean exists(String projectName, String functionName) throws OdpsException
public boolean exists(String projectName, String schemaName, String functionName) throws OdpsException

返回值:存在返回 true


update​

更新函数定义。

public void update(Function function) throws OdpsException
public void update(String projectName, Function function) throws OdpsException
public void update(String projectName, String schemaName, Function function) throws OdpsException

delete​

删除函数。

public void delete(String functionName) throws OdpsException
public void delete(String projectName, String functionName) throws OdpsException
public void delete(String projectName, String schemaName, String functionName) throws OdpsException

iterator​

获取函数迭代器。

public Iterator<Function> iterator()
public Iterator<Function> iterator(String projectName)
public Iterator<Function> iterator(String projectName, String schemaName)

iterable​

获取函数 Iterable(支持 foreach 语法)。

public Iterable<Function> iterable()
public Iterable<Function> iterable(String projectName)
public Iterable<Function> iterable(String projectName, String schemaName)

Function 对象属性​

方法返回类型说明
getName()String函数名称
getClassPath()String函数实现类路径
getResources()List<String>依赖资源列表
getOwner()String函数所有者
getCreatedTime()Date创建时间

使用示例​

Functions functions = odps.functions();

// 创建函数
Function func = new Function();
func.setName("my_udf");
func.setClassPath("com.example.MyUDF");
func.setResources(Arrays.asList("my_udf.jar"));
functions.create(func);

// 遍历所有函数
for (Function f : functions.iterable()) {
System.out.println(f.getName() + " -> " + f.getClassPath());
}

// 删除函数
functions.delete("my_udf");