String Pyspark DD-MMM-YYYY(字符串格式)到时间戳
您好,我是Pyspark的新手,我有一个字符串变量,其中包含日期DD-MMM-YYYY格式,我想将其转换为时间戳String Pyspark DD-MMM-YYYY(字符串格式)到时间戳,string,datetime,pyspark,String,Datetime,Pyspark,您好,我是Pyspark的新手,我有一个字符串变量,其中包含日期DD-MMM-YYYY格式,我想将其转换为时间戳 2019年5月24日-字符串格式到时间戳尝试使用到时间戳(首选)(或)从unixtime和unix\u时间戳功能: 示例: from pyspark.sql.functions import * from pyspark.sql.types import * df.selectExpr("to_timestamp(dt,'dd-MMM-yyyy') as tt").show() +-
2019年5月24日-字符串格式到时间戳尝试使用
到时间戳(首选)(或)从unixtime和unix\u时间戳功能:
示例:
from pyspark.sql.functions import *
from pyspark.sql.types import *
df.selectExpr("to_timestamp(dt,'dd-MMM-yyyy') as tt").show()
+-------------------+
| tt|
+-------------------+
|2019-05-24 00:00:00|
+-------------------+
df1.withColumn("ts",to_timestamp(col("dt"),'dd-MMM-yyyy')).show()
+-----------+-------------------+
| dt| ts|
+-----------+-------------------+
|24-MAY-2019|2019-05-24 00:00:00|
+-----------+-------------------+
#using from_unixtime and unix_timestamp
df1.withColumn("ts",from_unixtime(unix_timestamp(col("dt"),'dd-MMM-yyyy'),'yyyy-MM-dd HH:mm:ss.SSS').cast("timestamp")).show(10,False)
+-----------+-----------------------+
|dt |ts |
+-----------+-----------------------+
|24-MAY-2019|2019-05-24 00:00:00.000|
+-----------+-----------------------+
#using unix_timestamp and casting to timestamp
df1.withColumn("ts",unix_timestamp(col("dt"),'dd-MMM-yyyy').cast("timestamp")).show()
#+-----------+-------------------+
#| dt| ts|
#+-----------+-------------------+
#|24-MAY-2019|2019-05-24 00:00:00|
#+-----------+-------------------+