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

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

问题描述

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

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)

我得到了错误:java.lang.IllegalArgumentException:格式无效:1435655706000"在6000"处格式错误

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

推荐答案

这里使用的是 Scala DataFrame 函数:from_unixtimeto_date

Here it is using Scala DataFrame functions: from_unixtime and to_date

// NOTE: divide by 1000 required if milliseconds
// e.g. 1446846655609 -> 2015-11-06 21:50:55 -> 2015-11-06 
mr.select(to_date(from_unixtime($"ts" / 1000))) 

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

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