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
的原因是什么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只是其中的一个。问题更在于理解背后的原因,为什么不支持?这是什么意思?