【发布时间】:2022-08-03 13:45:58
【问题描述】:
问题:当我调用User Defined Function (UDF) 时,似乎我在以下代码中没有做正确的事情。为什么输出不是“这是一个测试”?
评论:我正在使用python notebook in Azure Databricks`。
笔记本单元格1:
def TestFunction(myVal):
return \"this is a \" + myVal
笔记本单元格2:
from pyspark.sql import functions as F
from pyspark.sql.types import IntegerType,DateType,StringType
new_name = F.udf(TestFunction, StringType())
s = new_name(\"test\")
print(s)
输出:
Column<\'TestFunction(test)\'>
期望的输出:
This is a test
-
在
select()或withColumn()中使用new_test()作为pyspark 函数。它返回具有所需值的列 -
您需要将列名传递到此 udf。然后列的值将在 udf 中使用。结果将是一列。
标签: python python-3.x apache-spark pyspark azure-databricks