共用方式為


UserDefinedTableFunction

一個 Python 中由使用者定義的表格函式。

此類別的建構子不應直接被呼叫。 用來 pyspark.sql.functions.udtf 建立一個實例。

語法

from pyspark.sql.functions import udtf

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

屬性

房產 說明
returnType 使用者定義資料表的回傳型別會以 StructType 形式運作,若未指定則為 None。

方法

方法 說明
asDeterministic() 將 UserDefinedTableFunction 更新為確定性。

Notes

這個 API 正在演進中。

Examples

from pyspark.sql.functions import lit, udtf

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

PlusOne(lit(1)).show()
+---+---+
| c1| c2|
+---+---+
|  1|  2|
+---+---+
_ = spark.udtf.register(name="plus_one", f=PlusOne)
spark.sql("SELECT * FROM plus_one(1)").collect()
[Row(c1=1, c2=2)]
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)]