spark 3.1 中的日期和 inferSchema 选项问题
Issue with date and inferSchema option in spark 3.1
我有一个包含日期列的 CSV 文件,如下所示,
datecol
----------
2021-01-11
2021-02-15
2021-02-10
2021-04-22
如果我通过在 spark 版本 2.4.5 中启用 inferSchema
来读取此文件,我将低于架构,
root
|-- datecol: timestamp (nullable = true)
但在下面的 spark 3.1 中是输出。
root
|-- datecol: string (nullable = true)
我已从 spark 文档中查看 migration guide,但没有获得任何相关信息。
任何人都可以确认这是一个错误还是我需要使用其他配置?
这是自 Spark 3+ 以来 Spark 迁移到 Java 8 new Date API 的结果。您可以阅读 migration guide:
Parsing/formatting of timestamp/date strings. This effects on CSV/JSON
datasources [...]. New implementation performs strict checking of its input. For example,
the 2015-07-22 10:00:00
timestamp cannot be parse if pattern is
yyyy-MM-dd
because the parser does not consume whole input. Another
example is the 31/01/2015 00:00
input cannot be parsed by the
dd/MM/yyyy hh:mm
pattern because hh
supposes hours in the range 1-12.
In Spark version 2.4 and below, java.text.SimpleDateFormat is used for
timestamp/date string conversions [...].
其实是inferSchema
does not detect DateType
but only TimestampType
. And since by default in CSV Data Source,参数timestampFormat
是yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX]
所以没有转换成时间戳,上面说的原因
您可以尝试在加载csv时添加选项:
val df = spark.read.option("inferSchema", "true").option("timestampFormat", "yyyy-MM-dd").csv("/path/csv")
我有一个包含日期列的 CSV 文件,如下所示,
datecol
----------
2021-01-11
2021-02-15
2021-02-10
2021-04-22
如果我通过在 spark 版本 2.4.5 中启用 inferSchema
来读取此文件,我将低于架构,
root
|-- datecol: timestamp (nullable = true)
但在下面的 spark 3.1 中是输出。
root
|-- datecol: string (nullable = true)
我已从 spark 文档中查看 migration guide,但没有获得任何相关信息。
任何人都可以确认这是一个错误还是我需要使用其他配置?
这是自 Spark 3+ 以来 Spark 迁移到 Java 8 new Date API 的结果。您可以阅读 migration guide:
Parsing/formatting of timestamp/date strings. This effects on CSV/JSON datasources [...]. New implementation performs strict checking of its input. For example, the
2015-07-22 10:00:00
timestamp cannot be parse if pattern isyyyy-MM-dd
because the parser does not consume whole input. Another example is the31/01/2015 00:00
input cannot be parsed by thedd/MM/yyyy hh:mm
pattern becausehh
supposes hours in the range 1-12. In Spark version 2.4 and below, java.text.SimpleDateFormat is used for timestamp/date string conversions [...].
其实是inferSchema
does not detect DateType
but only TimestampType
. And since by default in CSV Data Source,参数timestampFormat
是yyyy-MM-dd'T'HH:mm:ss[.SSS][XXX]
所以没有转换成时间戳,上面说的原因
您可以尝试在加载csv时添加选项:
val df = spark.read.option("inferSchema", "true").option("timestampFormat", "yyyy-MM-dd").csv("/path/csv")