简体   繁体   English

Spark 2.0如何在DF中将DF Date / timstamp列转换为另一种日期格式?

[英]Spark 2.0 How to convert DF Date/timstamp column to another date format in scala?

For my learning , i have been using below sample dataset . 为了我的学习,我一直在使用下面的样本数据集。

+-------------------+-----+-----+-----+-----+-------+
|             MyDate| Open| High|  Low|Close| Volume|
+-------------------+-----+-----+-----+-----+-------+
|2006-01-03 00:00:00|983.8|493.8|481.1|492.9|1537660|
|2006-01-04 00:00:00|979.6|491.0|483.5|483.8|1871020|
|2006-01-05 00:00:00|972.2|487.8|484.0|486.2|1143160|
|2006-01-06 00:00:00|977.8|489.0|482.0|486.2|1370250|
|2006-01-09 00:00:00|973.4|487.4|483.0|483.9|1680740|
+-------------------+-----+-----+-----+-----+-------+

I tried to change "MyDate" column values to different format like "YYYY-MON" and written like this.. 我试图将“MyDate”列值更改为不同的格式,如“YYYY-MON”,并像这样写。

citiDataDF.withColumn("New-Mydate",to_timestamp($"MyDate", "yyyy-MON")).show(5)

After executing the code, found that new column "New-Mydate". 执行代码后,发现新列“New-Mydate”。 but i couldn't see the desired output format. 但我看不到所需的输出格式。 can you please help 你能帮忙吗?

You need date_format instead to_timestamp : 你需要date_format而不是to_timestamp

val citiDataDF = List("2006-01-03 00:00:00").toDF("MyDate")
citiDataDF.withColumn("New-Mydate",date_format($"New-Mydate", "yyyy-MMM")).show(5)

Result: 结果:

+-------------------+----------+
|             MyDate|New-Mydate|
+-------------------+----------+
|2006-01-03 00:00:00|  2006-Jan|
+-------------------+----------+

Note: Three "M" mean the month as string, if you want a month as Int, you must use only two "M" 注意:三个“M”表示月份为字符串,如果您想要一个月作为Int,则必须仅使用两个“M”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM