2017-02-14 22 views
0

我努力学习Spark和我使用如下的unix_timestamp函数读取数据帧与时间戳列:读一个完整的时间戳为数据帧

val columnName = "TIMESTAMPCOL" 
    val sequence = Seq(2016-01-20 12:05:06.999) 
    val dataframe = { 
    sequence.toDF(columnName) 
    } 
    val typeDataframe = dataframe.withColumn(columnName, org.apache.spark.sql.functions.unix_timestamp($"TIMESTAMPCOL")) 
    typeDataframe.show 

这将产生一个输出:

+------------+ 
|TIMESTAMPCOL| 
+------------+ 
| 1453320306| 
+------------+ 

我如何阅读它,以便我不会丢失ms,即.999部分?我尝试使用unix_timestamp(col: Col, s: String),其中s是SimpleDateFormat,例如“yyyy-MM-dd hh:mm:ss”,没有任何运气。

+0

'date_format'使用Java SimpleDateFormat的内部,所以你会得到充分的时间以毫秒秒为好。可能重复[of](http://stackoverflow.com/questions/41879125/handling-microseconds-in-spark-scala/41879869#41879869) –

+2

[Spark Scala中处理微秒](http:// stackoverflow。 COM /问题/ 41879125 /处理微秒功能于火花阶) –

回答

1

要保留毫秒,请使用"yyyy-MM-dd HH:mm:ss.SSS"格式。您可以使用如下所示的date_format

val typeDataframe = dataframe.withColumn(columnName, org.apache.spark.sql.functions.date_format($"TIMESTAMPCOL","yyyy-MM-dd HH:mm:ss.SSS")) 
typeDataframe.show 

这会给你

+-----------------------+ 
|TIMESTAMPCOL   | 
+-----------------------+ 
|2016-01-20 12:05:06:999| 
+-----------------------+ 
相关问题