如何强制avro写入时间戳在UTC在火花Scala数据帧[重复]。

问题描述 投票:0回答:1

我需要将时间戳字段写入avro,并确保数据保存在UTC.目前avro将其转换为长(时间戳毫秒)在服务器的本地时区,这是造成的问题,如果服务器读取bk是一个不同的时区。我看了DataFrameWriter,它似乎提到了一个叫做timeZone的选项,但它似乎没有帮助。

**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
1个回答
2
投票

.option("timeZone","UTC") 选项不会将时间戳转换为UTC时区。

设置这个 spark.conf.set("spark.sql.session.timeZone", "UTC") 配置属性可将 UTC 设置为所有时间戳的默认时区。

默认值为 spark.sql.session.timeZone 属性是JVM系统的本地时区,如果没有设置。

如果上面的选项由于较低版本的spark无法使用,请尝试使用下面的选项。

--conf "spark.driver.extraJavaOptions=-Duser.timezone=UTC" --conf "spark.executor.extraJavaOptions=-Duser.timezone=UTC"

© www.soinside.com 2019 - 2024. All rights reserved.