コール_udf
ユーザー定義関数を呼び出します。
構文
Python
import pyspark.sql.functions as sf
sf.call_udf(udfName=<udfName>, *cols)
パラメーター
パラメーター | Type | 説明 |
|---|---|---|
|
| ユーザー定義関数 (UDF) の名前。 |
|
| UDF で使用される列名または列。 |
戻り値
pyspark.sql.Column: 実行された UDF の結果。
例
例 1 : 整数 UDF で call_udf を使用する。
Python
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()
Output
+---------+
|intX2(id)|
+---------+
| 2|
| 4|
| 6|
+---------+
例 2 : 文字列 UDF で call_udf を使用する。
Python
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()
Output
+-----------+
|strX2(name)|
+-----------+
| aa|
| bb|
| cc|
+-----------+