scala - 如何将 df 列作为参数传递给函数?
问题描述
我写了下面的函数
object AgeClassification {
def AgeCategory(age:Int) : String = {
if(age<=30)
return "Young"
else if(age>=65)
return "Older"
else
return "Mid-age"
}
}
我正在尝试将数据框列作为参数传递
val df_new = df
.withColumn("Age_Category", AgeClassification.AgeCategory(df("age")))
但得到错误
:33: 错误:类型不匹配;
找到: org.apache.spark.sql.Column
required: Int
val df_new = df.withColumn("Age_Category",AgeClassification.AgeCategory(df("age")))
如何将列作为参数传递?
val df_new = df
.withColumn("Age_Category",AgeClassification.AgeCategory(df.age.cast(IntegerType)))
:33: 错误:值 age 不是 org.apache.spark.sql.DataFrame
val df_new = df.withColumn("Age_Category",AgeClassification.AgeCategory(df.age.cast(IntegerType))) 的成员
val df_new = df
.withColumn("Age_Category", AgeClassification.AgeCategory(df("age").cast(Int)))
:33:错误:使用替代方法重载方法值转换:(
至:String)org.apache.spark.sql.Column
(至:org.apache.spark.sql.types.DataType)org.apache.spark.sql.Column
不能应用于 (Int.type)
val df_new = df.withColumn("Age_Category",AgeClassification.AgeCategory(df("age").cast(Int)))
解决方案
使用 SparkSQL API 操作数据帧时,不能直接使用 scala 函数。您只能使用在Column
类中或在functions
类中定义的“列”函数。他们基本上将列转换为列。实际计算在 Spark 中处理。
为了说明这一点,您可以在 REPL 中尝试:
scala> df("COL1").cast("int")
res6: org.apache.spark.sql.Column = CAST(COL1 AS INT)
类型是Column
,不是int
,这就是为什么 scala 拒绝在这样的对象上应用你的函数(它是一个整数)。
要使用自定义函数,您需要将其包装在 UDF 中,如下所示:
val ageUDF = udf((age : Int) => AgeClassification.AgeCategory(age))
// or shorter
val ageUDF = udf(AgeClassification.AgeCategory _)
// The you may use it this way:
df.withColumn("classif", ageUDF(df("age")))
另请注意,df.age
它在 pyspark 中有效,但在 ni scala 中无效。对于按名称访问列的简短方法,您可以 importspark.implicits._
和 write$"age"
甚至更短的'age
.
推荐阅读
- javascript - 当我尝试使用 Firebase 使用 React 使用 Google 登录时,为什么会出现网络错误
- c# - 从触发器运行并将一些参数传递给存储过程
- typescript - TypeScript Compiler API 中 Symbol 和 Node 的区别
- epplus - 使用 EPPlus 在 ubuntu 18.04 上读取 excel 文件时出错
- ios - iOS Fabric 每日新增用户最近几乎为 0
- api - 如何在 Google Maps Web API 上设置集群
- mysql - SQL 查询 from 和 to 之间的预订日期
- aws-amplify - 连接失败:缓冲区未定义 - 更新到 v4.7.0 后订阅 AWS Amplify (graphql) 订阅时出错
- reactjs - 在 React,js 中遍历 HTMLcollection
- laravel - 在运行时更改包的视图路径