asTable

Konwertuje ramkę TableArg danych na obiekt, który może służyć jako argument tabeli w funkcji TVF (Table-Valued Function), w tym UDTF (funkcjaUser-Defined Table).

Składnia

asTable()

Zwroty

TableArg TableArg: obiekt reprezentujący argument tabeli.

Notatki

Po uzyskaniu tabeli TableArg z ramki danych przy użyciu tej metody można określić partycjonowanie i kolejność dla argumentu tabeli, wywołując metody, takie jak partitionBy, orderByi withSinglePartition w wystąpieniu TableArg .

Examples

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|
# +---+-----+