简体   繁体   English

将带有时区列的日期字符串转换为 spark scala 中的时间戳

[英]Convert the date string with timezone column to timestamp in spark scala

I need to convert the string (date with timezone) column to Timestamp.我需要将字符串(带时区的日期)列转换为时间戳。 The converted timestamp column should have the same value which the string field has.转换后的时间戳列应具有与字符串字段相同的值。

There is a string field it has the date and time offset, I tried to convert that into timestamp data type, it actually converted into UTC format but I want to have the same date and time with the offset as a timestamp data type.有一个字符串字段,它具有日期和时间偏移量,我尝试将其转换为时间戳数据类型,它实际上转换为 UTC 格式,但我希望将偏移量作为时间戳数据类型具有相同的日期和时间。

Seq("2019-02-05T18:59:11.0874121+05:30").toDF("date_str")
.select($"date_str")
.withColumn("date_timestamp",$"date_str".cast("timestamp"))
.show(false)

I expect the date_timestamp column should have the "2019-02-05T18:59:11.0874121+05:30" but it actually converted into UTC format "2019-02-05T13:29:11.087+0000" .我希望 date_timestamp 列应该有"2019-02-05T18:59:11.0874121+05:30"但它实际上转换为UTC格式"2019-02-05T13:29:11.087+0000"

I use a udf to convert Strings to Timestamps without any changes.我使用 udf 将字符串转换为时间戳而不做任何更改。

import java.text.SimpleDateFormat
import java.sql.Timestamp

val convertToTimestamp= (logTimestamp: String) => {
  try {
    // change the date format as needed
    val sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss','SSS")
    val theDate = sdf.parse(logTimestamp)
    new Timestamp(theDate.getTime)
  } catch {
    case _: Exception => null
  }
}

//register for sql
sqlContext.udf.register("convertToTimestamp", convertToTimestamp)
//register for scala 
def convertToTimestampUDF = udf(convertToTimestamp)

val newDfWithTimeStamp = oldDfWithString.select(convertToTimestampUDF($"date_timestamp ").alias("date_timestamp "))

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

相关问题 Scala - 如何在 Spark SQL 查询中将日期字符串转换为时间戳? - Scala - How to convert a Date String to a timestamp in a Spark SQL query? Spark 2.3 (Scala) - 将时间戳列从 UTC 转换为另一列中指定的时区 - Spark 2.3 (Scala) - Convert a timestamp column from UTC to timezone specified in another column Spark Scala 从日期创建时间戳列 - Spark Scala creating timestamp column from date 使用Scala将字符串转换为Spark的时间戳 - Convert string to timestamp for Spark using Scala 在火花数据框中,如何使用 scala 将字符串类型的日期列转换为日期类型的日期列 - In spark Data frame how to convert Date column of type string to Date column of type Date using scala 日期和时间列中的spark scala split timestamp列 - spark scala split timestamp column in date column and time column 在 Spark/Scala 中使用 date_format 将时间戳转换为星期几字符串 - Convert timestamp to day-of-week string with date_format in Spark/Scala Scala-将带有时区的ISO 8601格式的字符串转换为时间戳 - Scala - convert ISO 8601 formated string with timezone to timestamp 在Scala中将时间字符串转换为时间戳/日期时间 - Convert time string into timestamp/date time in scala Spark Scala - 字符串到时间戳 - Spark Scala - String to Timestamp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM