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

テーブルとして

DataFrameをTableArgオブジェクトに変換します。このオブジェクトは、UDTF(ユーザー定義テーブル関数)を含むTVF(テーブル値関数)のテーブル引数として使用できます。

構文

asTable()

戻り値

TableArgテーブル引数を表すTableArgオブジェクト。

注意

このメソッドを使用して DataFrame から TableArg を取得した後、 TableArgインスタンスに対してpartitionByorderBywithSinglePartitionなどのメソッドを呼び出すことで、テーブル引数のパーティショニングと順序を指定できます。

Python
from pyspark.sql.functions import udtf

@udtf(returnType="id: int, doubled: int")
class DoubleUDTF:
def eval(self, row):
yield row["id"], row["id"] * 2

df = spark.createDataFrame([(1,), (2,), (3,)], ["id"])

result = DoubleUDTF(df.asTable())
result.show()
# +---+-------+
# | id|doubled|
# +---+-------+
# | 1| 2|
# | 2| 4|
# | 3| 6|
# +---+-------+

df2 = spark.createDataFrame(
[(1, "a"), (1, "b"), (2, "c"), (2, "d")], ["key", "value"]
)

@udtf(returnType="key: int, value: string")
class ProcessUDTF:
def eval(self, row):
yield row["key"], row["value"]

result2 = ProcessUDTF(df2.asTable().partitionBy("key").orderBy("value"))
result2.show()
# +---+-----+
# |key|value|
# +---+-----+
# | 1| a|
# | 1| b|
# | 2| c|
# | 2| d|
# +---+-----+
このページの見出し