0

我有以下代码片段:

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)并没有yearcolCannot 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 */ }

即这两种方法都存在。我究竟做错了什么?

4

1 回答 1

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
于 2018-12-26T11:39:33.787 回答