如何在Spark中将unix时间戳转换为日期 [英] How to convert unix timestamp to date in Spark

查看:4087
本文介绍了如何在Spark中将unix时间戳转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含unix时间戳列的数据框(例如1435655706000),我想将其转换为格式为yyyy-MM-DD的数据,我尝试过nscala-time,但是它并没有工作。

  val time_col = sqlc.sql(从ts选择ts)。 (_(0).toString.toDateTime)
time_col.collect()。foreach(println)

我有错误:
java.lang.IllegalArgumentException:格式无效:1435655706000格式错误为6000

解决方案

  import org.joda.time。{DateTimeZone} 
import org.joda.time.format.DateTimeFormat

您需要导入以下库。

  val stri = new DateTime(timeInMillisec).toDateTime.toString(yyyy / MM / dd)

或调整到你的情况:

  val time_col = sqlContext.sql(从ts选择ts)
.map line => new D ateTime(line(0).toInt).toDateTime.toString(yyyy / MM / dd))

可能有另一种方式:

  import com.github.nscala_time.time.Imports._ 

val date =(new DateTime()+((threshold.toDouble)/ 1000).toInt.seconds
.toString(yyyy / MM / dd)

希望这有帮助:)


I have a data frame with a column of unix timestamp(eg.1435655706000), and I want to convert it to data with format 'yyyy-MM-DD', I've tried nscala-time but it doesn't work.

val time_col = sqlc.sql("select ts from mr").map(_(0).toString.toDateTime)
time_col.collect().foreach(println)

and I got error: java.lang.IllegalArgumentException: Invalid format: "1435655706000" is malformed at "6000"

解决方案

import org.joda.time.{DateTimeZone}
import org.joda.time.format.DateTimeFormat

You need to import the following libraries.

val stri = new DateTime(timeInMillisec).toDateTime.toString("yyyy/MM/dd")

Or adjusting to your case :

 val time_col = sqlContext.sql("select ts from mr")
                     .map(line => new DateTime(line(0).toInt).toDateTime.toString("yyyy/MM/dd"))

There could be another way :

  import com.github.nscala_time.time.Imports._

  val date = (new DateTime() + ((threshold.toDouble)/1000).toInt.seconds )
             .toString("yyyy/MM/dd")

Hope this helps :)

这篇关于如何在Spark中将unix时间戳转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆