登記器(UDTFRegistration)

將 Python 使用者定義的表格函式註冊為 SQL 表格函式。

語法

register(name, f)

參數

參數 類型 說明
name str SQL 語句中使用者定義的表格函式名稱。
f UserDefinedTableFunction 使用者自訂的表格功能。

退貨

UserDefinedTableFunction

Notes

Spark 使用給定使用者定義的表格函式的回傳型別作為註冊函式的回傳型別。

要註冊一個非確定性的 Python 表格函式,首先建立一個非確定性的使用者定義表格函式,然後將其註冊為 SQL 函式。

Examples

from pyspark.sql.functions import udtf

@udtf(returnType="c1: int, c2: int")
class PlusOne:
    def eval(self, x: int):
        yield x, x + 1

spark.udtf.register(name="plus_one", f=PlusOne)
spark.sql("SELECT * FROM plus_one(1)").collect()
# [Row(c1=1, c2=2)]

# Use it with a lateral join.
spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").collect()
# [Row(x=0, y=1, c1=0, c2=1), Row(x=1, y=2, c1=1, c2=2)]