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

UDTFRegistration

ユーザー定義テーブル関数登録用のラッパー。このインスタンスはspark.udtfからアクセスできます。

構文

Python
# Access through SparkSession
spark.udtf

方法

手法

説明

register(name, f)

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

注意

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