简体   繁体   English

如何在 spark-scala 中将字符串列(仅包含时间而不包含日期的列)转换为 time_stamp?

[英]How to convert a string column (column which contains only time and not date ) to time_stamp in spark-scala?

I need to convert the column which contains only time as string to a time stamp type or any other time function which is available in spark.我需要将仅包含时间作为字符串的列转换为时间戳类型或 spark 中可用的任何其他时间函数。

Below is the test Data frame which having "Time_eg" as string column,下面是具有“Time_eg”作为字符串列的测试数据框,

Time_eg
12:49:09 AM
12:50:18 AM

Schema before it convert to the time,转换为时间之前的架构,

Time_eg: string (nullable = true) Time_eg: 字符串 (nullable = true)

//Converting to time stamp
val transType= test.withColumn("Time_eg", test("Time_eg").cast("timestamp"))

Schema After converting to timestamp, the schema is Schema 转换为时间戳后,schema为

Time_eg: timestamp (nullable = true) Time_eg:时间戳(可为空 = 真)

But the output of transType.show() gives null value for the "Time_eg" column.但是transType.show()的输出为“Time_eg”列提供了空值。

Please let me know how to convert the column which contains only time as a string to time stamp in spark scala?请让我知道如何将仅包含时间作为字符串的列转换为 spark scala 中的时间戳?

Much appreciate if anyone can help on this?如果有人可以帮助解决这个问题,非常感谢?

Thanks谢谢

You need to use a specific function to convert a string to a timestamp, and specify the format.您需要使用特定函数将字符串转换为时间戳,并指定格式。 Also, a timestamp in Spark represents a full date (with time of the day).此外,Spark 中的时间戳表示完整日期(包含一天中的时间)。 If you do not provide the date, it will be set to 1970, Jan 1st, the begining of unix timestamps.如果您不提供日期,它将设置为 1970 年 1 月 1 日,即 unix 时间戳的开始。

In your case, you can convert your strings as follows:在您的情况下,您可以按如下方式转换字符串:

Seq("12:49:09 AM", "09:00:00 PM")
    .toDF("Time_eg")
    .select(to_timestamp('Time_eg, "hh:mm:ss aa") as "ts")
    .show
+-------------------+
|                 ts|
+-------------------+
|1970-01-01 00:49:09|
|1970-01-01 21:00:00|
+-------------------+

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

相关问题 Scala Spark:将数据框中的双列转换为日期时间列 - Scala Spark : Convert Double Column to Date Time Column in dataframe 根据另一列中的时间戳过滤行Spark Scala - Filter rows based on a time stamp in another column Spark Scala 我如何将DataFrame列名称转换为Spark-Scala中的值 - How could i convert a DataFrame Column name into a value in Spark-Scala 在 Spark-Scala 中将单个 String 列拆分为多个列 - Split single String column to multiple columns in Spark-Scala 日期和时间列中的spark scala split timestamp列 - spark scala split timestamp column in date column and time column 仅将没有日期的时间戳保存到数据框列 - Saving only time stamp without date to data frame column 在火花数据框中,如何使用 scala 将字符串类型的日期列转换为日期类型的日期列 - In spark Data frame how to convert Date column of type string to Date column of type Date using scala Scala:如何将整数转换为时间戳 - Scala : how to convert integer to time stamp 给定小时,月和年,将其转换为Spark scala中的时间戳 - Given hours and month and year, convert it to a time stamp in Spark scala 验证输入火花数据帧中的time_stamp以生成正确的输出火花数据帧 - Validate time_stamp in input spark dataframe to generate correct output spark dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM