Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache spark 我可以在Spark Dataframe udf中使用java.time.LocalDate吗?_Apache Spark_Apache Spark Sql_User Defined Functions_Java Time - Fatal编程技术网

Apache spark 我可以在Spark Dataframe udf中使用java.time.LocalDate吗?

Apache spark 我可以在Spark Dataframe udf中使用java.time.LocalDate吗?,apache-spark,apache-spark-sql,user-defined-functions,java-time,Apache Spark,Apache Spark Sql,User Defined Functions,Java Time,我必须调用一个将java.time.LocalDate作为输入参数的方法 我在Spark Dataframe中执行,并在udf中调用该方法 import org.apache.spark.sql.Row import java.time.format.DateTimeFormatter import java.time.ZonedDateTime import java.time.LocalDate val df = Seq((1, "2018-02-11T09:40:00+08:0

我必须调用一个将
java.time.LocalDate
作为输入参数的方法

我在Spark Dataframe中执行,并在udf中调用该方法

import org.apache.spark.sql.Row
import java.time.format.DateTimeFormatter
import java.time.ZonedDateTime
import java.time.LocalDate

val df = Seq((1, "2018-02-11T09:40:00+08:00")).toDF("id", "date_time")
df.show

+---+-------------------------+
|id |date_time                |
+---+-------------------------+
|1  |2018-02-11T09:40:00+08:00|
+---+-------------------------+

def formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME
val dateTime = "2018-06-10T09:30:00+02:00"
def complexMethod(d: LocalDate) = {
  d
  // do really complex thing
  // imagine there could be many other input params, localDate is just the one blocking here
}
我试着做:

val transformer = udf((dateTime: String) => {
  val localDate = ZonedDateTime.from(formatter.parse(dateTime)).toLocalDate;
  complexMethod(localDate)
})

df.withColumn("transformed", transformer(col("date_time"))).show
它将有以下错误:

// java.lang.UnsupportedOperationException: Schema for type java.time.LocalDate is not supported

我必须将
java.time.LocalDate
作为输入传递给
complexMethod
(假设这个
complexMethod
来自另一个库),并在udf中调用它。该错误似乎意味着udf中不允许使用
java.time.LocalDate


  • 不允许
    java.time.LocalDate
    的原因是什么
  • 如何在udf中调用
    complexMethod
  • 如果确实不可能,那么调用
    complexMethod
    的最佳方法是什么?使用RDD,数据集

  • java.time.LocalDate
    Spark 2.2
    之前不受支持。您必须转换
    java.sql.Timestamp
    java.sql.Date
    Hi@SašaZejnilović感谢您的评论,我尝试了Spark 2.4,我有相同的错误。如果我只处理日期时间,我可以使用
    java.sql.Date
    ,但假设我做的不止这些,调用一个有许多输入参数的api,而Date只是其中的一个。问题更在于理解背后的原因,为什么不支持?这是什么意思?