我可以在spark dataframe udf中使用java.time.localdate吗?

yhived7q  于 2021-05-26  发布在  Spark
关注(0)|答案(0)|浏览(262)

我必须调用一个方法 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 来自另一个lib),并在udf中调用它。这个错误似乎意味着 java.time.LocalDate 在自定义项中不允许。
是什么原因 java.time.LocalDate 不允许吗?
我怎么打电话 complexMethod 在自定义项中?
如果真的不可能,打电话最好的方式是什么 complexMethod ? 使用rdd,数据集?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题