简体   繁体   English

在火花 scala 中将 CDT 时间戳转换为 UTC 格式

[英]Converting CDT timestamp into UTC format in spark scala

My Dataframe, myDF is like bellow -我的 Dataframe,myDF 就像下面这样 -

DATE_TIME
Wed Sep  6 15:24:27 CDT 2017
Wed Sep  6 15:30:05 CDT 2017

Expected output in format:预期的 output 格式为:

2017-09-06 15:24:27
2017-09-06 15:30:05

Need to convert DATE_TIME timestamp to UTC.需要将 DATE_TIME 时间戳转换为 UTC。

Tried the below code in databricks notebook but it's not working.在 databricks 笔记本中尝试了以下代码,但它不起作用。

%scala

val df = Seq(("Wed Sep  6 15:24:27 CDT 2017")).toDF("times")
df.withColumn("times2",date_format(to_timestamp('times,"ddd MMM dd hh:mm:ss CDT yyyy"),"yyyy-MM-dd HH:mm:ss")).show(false)

times                        | times2    
Wed Sep  6 15:24:27 CDT 2017 | null

I think we need to remove wed from your string then use to_timestamp() function.我认为我们需要从您的字符串中删除wed然后使用to_timestamp() function。

Example:

df.show(false)
/*
+---------------------------+
|times                      |
+---------------------------+
|Wed Sep 6 15:24:27 CDT 2017|
+---------------------------+
*/

df.withColumn("times2",expr("""to_timestamp(substring(times,5,length(times)),"MMM d HH:mm:ss z yyyy")""")).
show(false)
/*
+---------------------------+-------------------+
|times                      |times2             |
+---------------------------+-------------------+
|Wed Sep 6 15:24:27 CDT 2017|2017-09-06 15:24:27|
+---------------------------+-------------------+
*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM