Parse dates with microseconds precision with dataframe in Spark

Viewed 881

I have a csv file:

Name;Date
A;2018-01-01 10:15:25.123456
B;2018-12-31 10:15:25.123456

I try to parse with Spark Dataframe:

val df = spark.read.format(source="csv")
    .option("header", true)
    .option("delimiter", ";")
    .option("inferSchema", true)
    .option("timestampFormat", "yyyy-MM-dd HH:mm:ss.SSSSSS")

But the resulting Dataframe is (wrongly) truncated at the millisecond:

scala> df.show(truncate=false)
+---+-----------------------+
|Nom|Date                   |
+---+-----------------------+
|A  |2018-01-01 10:17:28.456|
|B  |2018-12-31 10:17:28.456|
+---+-----------------------+


df.first()(1).asInstanceOf[Timestamp].getNanos()
res51: Int = 456000000

Bonus question: read with nanoseconds precision

1 Answers
Related