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

登録する (UDTFRegistration)

Pythonユーザー定義テーブル関数をSQLテーブル関数として登録します。

構文

register(name, f)

パラメーター

パラメーター

Type

説明

name

str

SQL文におけるユーザー定義テーブル関数の名前。

f

UserDefinedTableFunction

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

戻り値

UserDefinedTableFunction

注意

Sparkは、指定されたユーザー定義テーブル関数の戻り値の型を、登録された関数の戻り値の型として使用します。

非決定的Pythonテーブル関数を登録するには、まず非決定的ユーザー定義テーブル関数を構築し、それをSQL関数として登録します。

Python
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)]