apache-spark - 我可以在 Spark Dataframe udf 中使用 java.time.LocalDate 吗?
问题描述
我必须调用一个采用java.time.LocalDate
as 输入参数的方法。
我在 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 中调用它。该错误似乎意味着java.time.LocalDate
udf 中不允许这样做。
java.time.LocalDate
不允许的原因是什么?- 我怎样才能调用
complexMethod
udf? - 如果真的不可能,最好的打电话方式是
complexMethod
什么?使用 RDD,数据集?
解决方案
推荐阅读
- powerbi - Power BI 计算不包括周末和非工作时间的时差
- angular - Angular HttpParams 对象为空
- python - 有没有办法比较python中最新的字母表
- javascript - 是否推荐使用 Typescript 中的下划线?
- javascript - $.getJSON 未捕获的引用错误 $ 未定义
- c# - 如何存储/查询坐标以计算米之间的距离?
- python - 遍历字典以创建实例
- c++ - 使用一个或多个配置文件的多个程序
- node.js - Redis 会话不可用
- android - 获取 React 本机构建 apk 后出现“Java.io.FileNotFoundException”错误