Compartilhar via


asTable

Converte o DataFrame em um TableArg objeto, que pode ser usado como um argumento de tabela em um TVF (funçãoTable-Valued), incluindo UDTF ( função de tabelaUser-Defined).

Sintaxe

asTable()

Devoluções

TableArg: um TableArg objeto que representa um argumento de tabela.

Observações

Depois de obter um TableArg de um DataFrame usando esse método, você pode especificar particionamento e ordenação para o argumento de tabela chamando métodos como partitionBy, orderBye withSinglePartition na TableArg instância.

Exemplos

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