【发布时间】:2020-05-23 18:46:57
【问题描述】:
我需要将 Timestamp 字段写入 avro 并确保数据以 UTC 格式保存。当前 avro 将其转换为服务器本地时区中的 long (timestamp millis ),这会导致问题,就好像服务器读取 bk 是不同的时区一样。我查看了 DataFrameWriter,它似乎提到了一个名为 timeZone 的选项,但它似乎没有帮助。有没有办法强制 Avro 考虑在特定时区收到的所有时间戳字段?
**CODE SNIPPET**
--write to spark avro
val data = Seq(Row("1",java.sql.Timestamp.valueOf("2020-05-11 15:17:57.188")))
val schemaOrig = List( StructField("rowkey",StringType,true)
,StructField("txn_ts",TimestampType,true))
val sourceDf = spark.createDataFrame(spark.sparkContext.parallelize(data),StructType(schemaOrig))
sourceDf.write.option("timeZone","UTC").avro("/test4")
--now try to read back from avro
spark.read.avro("/test4").show(false)
avroDf.show(false)
original value in soure 2020-05-11 15:17:57.188
in avro 1589224677188
read bk from avro wt out format
+-------------+-------------+
|rowkey |txn_ts |
+-------------+-------------+
|1 |1589224677188|
+-------------+-------------+
This is mapping fine but issue is if the local time of the server writing is EST and the one reading back is GMT it would give problem .
println(new java.sql.Timestamp(1589224677188L))
2020-05-11 7:17:57.188 -- time in GMT
【问题讨论】:
标签: apache-spark apache-spark-sql avro spark-avro