我有以下代码段:
package org.test.test.datahelper
import org.apache.spark.rdd.RDD
import org.apache.spark.sql._
class WeatherHelper(sparkSession: SparkSession, weather: DataFrame) {
def prepareRRRColumn: DataFrame = {
import org.apache.spark.sql.functions
weather.withColumn("Year", year(col("DateTime")))
weather
}
}
问题是,尽管有必要的导入,但Scala(或可能是IntelliJ IDEA)却没有像year
(分别为col
和Cannot resolve symbol year
)看到方法col
。仅在上方一行(但是,即使导入是全局的,也行不通)。遵循org.apache.spark.sql.functions
的源代码,我发现了以下几行:
def col(colName : scala.Predef.String) : org.apache.spark.sql.Column = { /* compiled code */ }
def year(e : org.apache.spark.sql.Column) : org.apache.spark.sql.Column = { /* compiled code */ }
即两种方法确实存在。我在做什么错了?
答案 0 :(得分:2)
这更多是scala导入语法问题。
要在类/包function
中导入方法(col,year),则必须使用。
import org.apache.spark.sql.functions._
// Or import only specific functions
import org.apache.spark.sql.functions.{col, year}
代替
import org.apache.spark.sql.functions