Lưu ý
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử đăng nhập hoặc thay đổi thư mục.
Cần có ủy quyền mới truy nhập được vào trang này. Bạn có thể thử thay đổi thư mục.
Call a user-defined function.
Syntax
import pyspark.sql.functions as sf
sf.call_udf(udfName=<udfName>, *cols)
Parameters
| Parameter | Type | Description |
|---|---|---|
udfName |
str |
Name of the user defined function (UDF). |
cols |
pyspark.sql.Column or str |
Column names or Columns to be used in the UDF. |
Returns
pyspark.sql.Column: result of executed udf.
Examples
Example 1: Using call_udf with an integer UDF.
from pyspark.sql.functions import call_udf, col
from pyspark.sql.types import IntegerType, StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("intX2", lambda i: i * 2, IntegerType())
df.select(call_udf("intX2", "id")).show()
+---------+
|intX2(id)|
+---------+
| 2|
| 4|
| 6|
+---------+
Example 2: Using call_udf with a string UDF.
from pyspark.sql.functions import call_udf, col
from pyspark.sql.types import IntegerType, StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("strX2", lambda s: s * 2, StringType())
df.select(call_udf("strX2", col("name"))).show()
+-----------+
|strX2(name)|
+-----------+
| aa|
| bb|
| cc|
+-----------+