将字符串转换为时间戳

时间:2020-08-04 09:17:33

标签: pyspark

我有一个字符串字段

+-------------------+
|col1               |
+-------------------+
|2020-08-02 13:01:15|
+-------------------+

我需要转换为下面使用的时间戳类型

SELECT to_timestamp(col1, 'yyyy-MM-dd hh:MM:ss') as time from table1;

但是上面的查询返回的是下面的结果

输出:

+----------------------------+
|time                        |
+----------------------------+
|2020-08-02T13:01:15.000+0000|
+----------------------------+

预期输出:

+-------------------+
|time               |
+-------------------+
|2020-08-02 13:01:15| 
+-------------------+

其中time列是时间戳类型

1 个答案:

答案 0 :(得分:0)

只需转换为时间戳-

df_b =df_b.withColumn("date", F.lit("2020-08-02 13:01:15").cast(T.TimestampType()))
df_b.show()

+----+----------+---+--------------+-------------------+
|col1|      col2| rn|case_condition|               date|
+----+----------+---+--------------+-------------------+
|   B|2020-08-01|  1|             1|2020-08-02 13:01:15|
|   B|2020-09-20|  2|             1|2020-08-02 13:01:15|
|   C|2020-05-10|  1|             1|2020-08-02 13:01:15|
|   A|2020-08-05|  1|             1|2020-08-02 13:01:15|
+----+----------+---+--------------+-------------------+

root
 |-- col1: string (nullable = true)
 |-- col2: string (nullable = true)
 |-- rn: integer (nullable = true)
 |-- case_condition: integer (nullable = true)
 |-- date: timestamp (nullable = true)