UDF全稱爲User Defined Function,即用戶自定義函數。
編寫一個簡單函數
首先登錄DataWorks控制檯
,
在左側導航欄,單擊工作空間列表,
單擊相應工作空間後的進入數據開發。
新建Python資源。
右鍵單擊業務流程,選擇新建 > MaxCompute > 資源 > Python。
在新建資源對話框中,填寫資源名稱,並勾選上傳爲ODPS資源,單擊確定。
在Pyodps資源中輸入如下代碼。
如下圖
定義一個new-style class
,並實現evaluate方法,即可實現Python UDF。
如下
from odps.udf import annotate
@annotate("bigint,bigint->bigint")
class MyPlus(object):
def evaluate(self, arg0, arg1):
if None in (arg0, arg1):
return None
return arg0 + arg1
說明 Python UDF必須通過annotate指定函數簽名。
單擊提交。
新建函數
。
右鍵單擊已創的業務流程,選擇新建 > MaxCompute > 函數。
在新建函數對話框中,輸入函數名稱,單擊提交。
編輯函數配置,單擊提交併解鎖。
SELECT MyPlus(id,gdp) as t
FROM pivot_test;
t |
---|
11 |
14 |
18 |
23 |
17 |
21 |
25 |
28 |