【问题标题】:Spark 3.0 change in behaviour when using partitionBy on Timestamp DataType column?Spark 3.0 在 Timestamp DataType 列上使用 partitionBy 时的行为变化?
【发布时间】:2020-09-19 13:38:03
【问题描述】:

我目前正在将基于 Spark 的服务从 2.4.5 迁移到 3.0.0。

我注意到在 Timestamp 值的 DataType 上应用 partition 时,行为发生了变化。

在写入数据帧(镶木地板)时,一切都按预期工作(数据是根据请求的分区写入的,但在读取保存的数据帧时,我看到Date DataType 对象的截断,这当然是影响下游逻辑。

示例代码:

val sparkImplicits = spark.implicits
import sparkImplicits._
val simpleData = Seq(
  (1, "abd", Timestamp.valueOf("2020-01-01 00:00:01")),
  (2, "def", Timestamp.valueOf("2019-01-01 00:00:02"))
)

val df = simpleData.toDF("id", "str", "timestamp")

df.printSchema()
df.show()
df.write.partitionBy("timestamp").parquet("partition_by_timestamp")

val readDF = spark.read.parquet("partition_by_timestamp")
readDF.printSchema()
readDF.show(2)

提供的 sn-p 的输出:

root
 |-- id: integer (nullable = false)
 |-- str: string (nullable = true)
 |-- timestamp: timestamp (nullable = true)

+---+---+-------------------+
| id|str|          timestamp|
+---+---+-------------------+
|  1|abd|2020-01-01 00:00:01|
|  2|def|2019-01-01 00:00:02|
+---+---+-------------------+

root
 |-- id: integer (nullable = true)
 |-- str: string (nullable = true)
 |-- timestamp: date (nullable = true)

+---+---+----------+
| id|str| timestamp|
+---+---+----------+
|  1|abd|2020-01-01|
|  2|def|2019-01-01|
+---+---+----------+

变化的根源是什么,我应该如何将加载的数据帧列时间戳的值和类型保持为Timestamp

【问题讨论】:

    标签: apache-spark apache-spark-sql


    【解决方案1】:

    spark.sql.legacy.timeParserPolicy 设置为LEGACY 应该可以解决问题。

    就更改的原因而言,它似乎是为了与其他 Apache 项目保持一致,例如 pandas、R 和 Apache Arrow,它们都使用 Proleptic Gregorian calendar。

    更多详情请见:https://databricks.com/blog/2020/07/22/a-comprehensive-look-at-dates-and-timestamps-in-apache-spark-3-0.html

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2016-11-14
      • 2023-04-07
      • 2023-04-09
      相关资源
      最近更新 更多