[英]Spark dataFrame convert columns Datatype from String to Date
I have the following data, with schema 我有以下数据,有架构
scala> df2.printSchema()
root
|-- RowID: integer (nullable = true)
|-- Order Date: string (nullable = true)
scala> df2.show(5)
+-----+----------+
|RowID|Order Date|
+-----+----------+
| 1| 4/10/15|
| 49| 4/10/15|
| 50| 4/10/15|
| 80| 4/10/15|
| 85| 4/10/15|
+-----+----------+
I want to convert the "Order Date" String column to Date data type, and trying the following with no luck, can anyone please suggest a better way to do this? 我想将“订购日期”字符串列转换为日期数据类型,然后尝试以下操作,但没有运气,有人可以建议一种更好的方法吗?
scala> df2.select(df2.col("RowID"), df2.col("Order Date"), date_format(df2.col("Order Date"), "M/dd/yy")).show(5)
+-----+----------+-------------------------------+
|RowID|Order Date|date_format(Order Date,M/dd/yy)|
+-----+----------+-------------------------------+
| 1| 4/10/15| null|
| 49| 4/10/15| null|
| 50| 4/10/15| null|
| 80| 4/10/15| null|
| 85| 4/10/15| null|
+-----+----------+-------------------------------+
Managed to convert to the unix epoch timestamp, I think from here it is straightforward 设法转换为unix epoch时间戳,我认为从这里开始很简单
scala> df.select(df.col("RowID"), df.col("Order Date"), unix_timestamp(df.col("Order Date"), "M/d/yy")).show(5)
+-----+----------+--------------------------------+
|RowID|Order Date|unixtimestamp(Order Date,M/d/yy)|
+-----+----------+--------------------------------+
| 1| 4/10/15| 1428604200|
| 49| 4/10/15| 1428604200|
| 50| 4/10/15| 1428604200|
| 80| 4/10/15| 1428604200|
| 85| 4/10/15| 1428604200|
+-----+----------+--------------------------------+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.