Nota:
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
Convierte el DataFrame en un TableArg objeto , que se puede usar como argumento de tabla en un TVF (Table-Valued Function), incluido UDTF (User-Defined Table Function).
Sintaxis
asTable()
Devoluciones
TableArg: objeto TableArg que representa un argumento de tabla.
Notas
Después de obtener tableArg de un dataFrame mediante este método, puede especificar la creación de particiones y el orden para el argumento de tabla llamando a métodos como partitionBy, orderByy withSinglePartition en la TableArg instancia de .
Ejemplos
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|
# +---+-----+