我想使用SqlContext将月份添加到日期中 [英] I want to add month to a Date using SqlContext

查看:82
本文介绍了我想使用SqlContext将月份添加到日期中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

' 2013年2月1日'这是我的约会.如何获得2013年3月1日的结果?

'01-FEB-2013' This is my date. how can I get the result as 01-MAR-2013?

SELECT DATE_ADD('2011-01-01',INTERVAL 1 month);

mySql可以实现.我想要在scala中使用sqlContext的结果吗?

This is possible by mySql.I want the result using sqlContext in scala Is it possible?

推荐答案

您将要使用 org.apache.spark.sql.functions.add_months :

def add_months(startDate: Column, numMonths: Int): Column
"Returns the date that is numMonths after startDate."

以下是其用法示例:

scala> val df = sc.parallelize((0 to 6).map(i => 
       {now.setMonth(i); (i, new java.sql.Date(now.getTime))}).toSeq)
       .toDF("ID", "Dates")
df: org.apache.spark.sql.DataFrame = [ID: int, Dates: date]

scala> df.show
+---+----------+
| ID|     Dates|
+---+----------+
|  0|2016-01-21|
|  1|2016-02-21|
|  2|2016-03-21|
|  3|2016-04-21|
|  4|2016-05-21|
|  5|2016-06-21|
|  6|2016-07-21|
+---+----------+

scala> df.withColumn("New Dates", add_months(df("Dates"),1)).show
+---+----------+----------+
| ID|     Dates| New Dates|
+---+----------+----------+
|  0|2016-01-21|2016-02-21|
|  1|2016-02-21|2016-03-21|
|  2|2016-03-21|2016-04-21|
|  3|2016-04-21|2016-05-21|
|  4|2016-05-21|2016-06-21|
|  5|2016-06-21|2016-07-21|
|  6|2016-07-21|2016-08-21|
+---+----------+----------+

这篇关于我想使用SqlContext将月份添加到日期中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆