Condividi tramite


UserDefinedTableFunction

Funzione di tabella definita dall'utente in Python.

Il costruttore di questa classe non deve essere chiamato direttamente. Usare pyspark.sql.functions.udtf per creare un'istanza di .

Sintassi

from pyspark.sql.functions import udtf

@udtf(returnType="c1: int, c2: int")
class MyUDTF:
    def eval(self, x: int):
        yield x, x + 1

Proprietà

Proprietà Descrizione
returnType Tipo restituito della funzione di tabella definita dall'utente come StructType o Nessuno, se non specificato.

Methods

metodo Descrizione
asDeterministic() Aggiorna UserDefinedTableFunction in modo deterministico.

Note

Questa API è in continua evoluzione.

Examples

from pyspark.sql.functions import lit, udtf

@udtf(returnType="c1: int, c2: int")
class PlusOne:
    def eval(self, x: int):
        yield x, x + 1

PlusOne(lit(1)).show()
+---+---+
| c1| c2|
+---+---+
|  1|  2|
+---+---+
_ = spark.udtf.register(name="plus_one", f=PlusOne)
spark.sql("SELECT * FROM plus_one(1)").collect()
[Row(c1=1, c2=2)]
spark.sql("SELECT * FROM VALUES (0, 1), (1, 2) t(x, y), LATERAL plus_one(x)").collect()
[Row(x=0, y=1, c1=0, c2=1), Row(x=1, y=2, c1=1, c2=2)]