Python의 사용자 정의 테이블 함수입니다.
이 클래스의 생성자를 직접 호출할 수 없습니다. 인스턴스를 만드는 데 사용합니다 pyspark.sql.functions.udtf .
문법
from pyspark.sql.functions import udtf
@udtf(returnType="c1: int, c2: int")
class MyUDTF:
def eval(self, x: int):
yield x, x + 1
속성
| 재산 | 설명 |
|---|---|
returnType |
사용자 정의 테이블의 반환 형식은 StructType으로 작동하거나, 지정하지 않으면 None입니다. |
메서드
| 메서드 | 설명 |
|---|---|
asDeterministic() |
UserDefinedTableFunction을 결정적으로 업데이트합니다. |
Notes
이 API는 진화하고 있습니다.
예제
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)]