メインコンテンツまでスキップ

ユーザー定義テーブル関数

Python のユーザー定義テーブル関数。

このクラスのコンストラクターは直接呼び出されることは想定されていません。インスタンスを作成するにはpyspark.sql.functions.udtfを使用します。

構文

Python
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 を決定論的に更新します。

注意

この API は進化しています。

Python
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()
Output
+---+---+
| c1| c2|
+---+---+
| 1| 2|
+---+---+
Python
_ = spark.udtf.register(name="plus_one", f=PlusOne)
spark.sql("SELECT * FROM plus_one(1)").collect()
Output
[Row(c1=1, c2=2)]
Python
spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").collect()
Output
[Row(x=0, y=1, c1=0, c2=1), Row(x=1, y=2, c1=1, c2=2)]