问题描述
我是 spark 新手,是否有任何内置函数可以显示从当前日期开始的下个月日期,例如今天是 27-12-2016,然后该函数将返回 27-01-2017.我使用了 date_add() 但没有添加月份的功能.我试过 date_add(date, 31) 但如果一个月有 30 天怎么办.
I am new in spark , is there any built in function which will show next month date from current date like today is 27-12-2016 then the function will return 27-01-2017. i have used date_add() but no function for adding month. I have tried date_add(date, 31)but what if the month has 30 days .
spark.sql("select date_add(current_date(),31)") .show()
谁能帮我解决这个问题.我需要为此编写自定义函数吗?因为我仍然没有找到任何内置代码提前致谢卡利安
could anyone help me about this problem. do i need to write custom function for that ? cause i didn't find any built in code stillThanks in advanceKalyan
推荐答案
这不是 pyspark
特定的.您可以使用 add_months
.它从 Spark 1.5 开始可用.例如:
This is not pyspark
specific. You can use add_months
. It's available since Spark 1.5. e.g :
spark.sql("select current_date(), add_months(current_date(),1)").show()
# +--------------+-----------------------------+
# |current_date()|add_months(current_date(), 1)|
# +--------------+-----------------------------+
# | 2016-12-27| 2017-01-27|
# +--------------+-----------------------------+
您也可以使用负整数来删除月份:
You can also use negative integers to remove months :
spark.sql("select current_date(), add_months(current_date(),-1) as last_month").show()
# +--------------+----------+
# |current_date()|last_month|
# +--------------+----------+
# | 2016-12-27|2016-11-27|
# +--------------+----------+
这篇关于是否有任何 pyspark 函数用于添加下个月,如 DATE_ADD(date, month(int type))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!