Spark Dataframe - 使用用户定义函数添加列

Spark Dataframe - using User Defined Function to add a column

我还处于python的学习阶段。在以下示例中(取自 Method 3 of this article),用户定义函数 (UDF) 的名称是 Total(...,...)。但是作者用名字叫它 new_f(...,...).

问题:在下面的代码中,我们如何知道函数调用new_f(...,...)应该调用函数Total(...,...)?如果还有另一个 UDF 函数,比如 Sum(...,...),会怎样?在那种情况下,代码如何知道调用 new_f(...,...) 意味着调用 Total(...,...) 还是 Sum(...,...)?

# import the functions as F from pyspark.sql
import pyspark.sql.functions as F
from pyspark.sql.types import IntegerType
  
# define the sum_col
def Total(Course_Fees, Discount):
    res = Course_Fees - Discount
    return res
  
# integer datatype is defined
new_f = F.udf(Total, IntegerType())
  
# calling and creating the new
# col as udf_method_sum
new_df = df.withColumn(
  "Total_price", new_f("Course_Fees", "Discount"))
  
# Showing the Dataframe
new_df.show()
new_f = F.udf(Total, IntegerType()) 

将名称new_f分配给该用户定义的函数