spark timestamp timezone in JDBC read/write

Viewed 1887

I am creating a parquet file from reading data from oracle.

Oracle is running in UTC. I confirmed using,

SELECT DBTIMEZONE FROM DUAL;

Output:

DBTIMEZONE|
----------|
+00:00    |

Reading from JDBC and writing to S3 as parquet:

df = spark.read.format('jdbc').options(url=url,
                                           dbtable=query,
                                           user=user,
                                           password=password,
                                           fetchsize=2000).load()

df.write.parquet(s3_loc, mode="overwrite")

Now, I checked value of spark.sql.session.timeZone

print(spark.conf.get("spark.sql.session.timeZone"))

Output:

UTC

Now, I am reading data from S3 location:

df1 = spark.read.parquet(s3_loc)
df1.show()

Output:

+-------------------+
|             col1  |
+-------------------+
|2012-11-11 05:00:00|
|2013-11-25 05:00:00|
|2013-11-11 05:00:00|
|2014-12-25 05:00:00|
+-------------------+

col1 is date in oracle and converted to timestamp in spark df.

Why 5 hours are added in the output? Database is running in UTC and spark.sql.session.timeZone is UTC.

Note:

  1. Both RDS and EMR are running in AWS US-EAST-1
  2. On all the spark nodes, I ran TZ=UTC
1 Answers

Timezone is reckognized by JDBC driver, which does not know about Spark's timezone setting, but relies on JVM's default timezone. Moreover, it ignores remote database session's timezone settings. You said you ran TZ=UTC - I'm not sure, but probably it didn't work. Check what TimeZone.getDefault tells you.

If, as I suspect, your JVM timezone is EDT (US-EAST-1 is Virginia), then 2012-11-11 00:00:00 read from Oracle by JDBC is interpreted to be in EDT. Displayed in Spark it's 2012-11-11 05:00:00 UTC and this is the result you got.

To fix it, override JVM default timezone when running spark-submit:

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