Georg Heiler
Georg Heiler

Reputation: 17676

spark sql string to timestamp missing milliseconds

Why is:

import spark.implicits._
  val content = Seq(("2019", "09", "11","17","16","54","762000000")).toDF("year", "month", "day", "hour", "minute", "second", "nano")
  content.printSchema
  content.show
  content.withColumn("event_time_utc", to_timestamp(concat('year, 'month, 'day, 'hour, 'minute, 'second), "yyyyMMddHHmmss"))
    .withColumn("event_time_utc_millis", to_timestamp(concat('year, 'month, 'day, 'hour, 'minute, 'second, substring('nano, 0, 3)), "yyyyMMddHHmmssSSS"))
    .select('year, 'month, 'day, 'hour, 'minute, 'second, 'nano,substring('nano, 0, 3), 'event_time_utc, 'event_time_utc_millis)
    .show

missing the milliseconds?

+----+-----+---+----+------+------+---------+---------------------+-------------------+---------------------+
|year|month|day|hour|minute|second|     nano|substring(nano, 0, 3)|     event_time_utc|event_time_utc_millis|
+----+-----+---+----+------+------+---------+---------------------+-------------------+---------------------+
|2019|   09| 11|  17|    16|    54|762000000|                  762|2019-09-11 17:16:54|  2019-09-11 17:16:54|
+----+-----+---+----+------+------+---------+---------------------+-------------------+---------------------+

for a format string of: yyyyMMddHHmmssSSS which should include the milliseconds in SSS if I am not mistaken.

Upvotes: 2

Views: 8591

Answers (2)

SMaZ
SMaZ

Reputation: 2655

I have faced similar problem, Official Document says below line till spark <2.4:

Convert time string to a Unix timestamp (in seconds) with a specified format (see [http://docs.oracle.com/javase/tutorial/i18n/format/simpleDateFormat.html]) to Unix timestamp (in seconds), return null if fail.

Which means that it handles seconds only.

Spark>= 2.4 can handle SSS as well.

Solution: Below UDF will help to handle this scenario:

import java.text.SimpleDateFormat
import java.sql.Timestamp
import org.apache.spark.sql.functions._
import scala.util.{Try, Success, Failure}

val getTimestampWithMilis: ((String , String) => Option[Timestamp]) = (input, frmt) => input match {
  case "" => None
  case _ => {
    val format = new SimpleDateFormat(frmt)
    Try(new Timestamp(format.parse(input).getTime)) match {
      case Success(t) => Some(t)
      case Failure(_) => None
    }    
  }
}

val getTimestampWithMilisUDF = udf(getTimestampWithMilis)

For your Example:

val content = Seq(("2019", "09", "11","17","16","54","762000000")).toDF("year", "month", "day", "hour", "minute", "second", "nano")
val df = content.withColumn("event_time_utc", concat('year, 'month, 'day, 'hour, 'minute, 'second, substring('nano, 0, 3)))
df.show
+----+-----+---+----+------+------+---------+-----------------+
|year|month|day|hour|minute|second|     nano|   event_time_utc|
+----+-----+---+----+------+------+---------+-----------------+
|2019|   09| 11|  17|    16|    54|762000000|20190911171654762|
+----+-----+---+----+------+------+---------+-----------------+

df.withColumn("event_time_utc_millis", getTimestampWithMilisUDF($"event_time_utc", lit("yyyyMMddHHmmssSSS"))).show(1, false)
+----+-----+---+----+------+------+---------+-----------------+-----------------------+
|year|month|day|hour|minute|second|nano     |event_time_utc   |event_time_utc_millis  |
+----+-----+---+----+------+------+---------+-----------------+-----------------------+
|2019|09   |11 |17  |16    |54    |762000000|20190911171654762|2019-09-11 17:16:54.762|
+----+-----+---+----+------+------+---------+-----------------+-----------------------+

root
 |-- year: string (nullable = true)
 |-- month: string (nullable = true)
 |-- day: string (nullable = true)
 |-- hour: string (nullable = true)
 |-- minute: string (nullable = true)
 |-- second: string (nullable = true)
 |-- nano: string (nullable = true)
 |-- event_time_utc: string (nullable = true)
 |-- event_time_utc_millis: timestamp (nullable = true)

Upvotes: 7

oukAyou
oukAyou

Reputation: 11

Try to concat in this standard : "yyyy-MM-dd HH:mm:ss.ssss" (it ignores zeros, example : "762000000" as nano/milliseconds becomes "762")

youDataframe
.withColumn("dateTime_complete", 
concat_ws(" ", concat_ws("-", col("year"), col("month"), col("day")),
        concat_ws(":", col("hour"), col("minute"), concat_ws(".", col("second"), col("nano")))))
.withColumn("your_new_column", to_utc_timestamp(col("dateTime_complete"), "yyyy-MM-dd HH:mm:ss.sss"))

Upvotes: 0

Related Questions