【发布时间】: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