如何获得今天在sparksql中的“1天”日期?

2024-10-06 12:59:51 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在sparksql中获得current_date - 1天,与mysql中的cur_date()-1相同。


Tags: datemysqlcurrentcursparksql
3条回答

您可以轻松地执行此任务,有许多与日期相关的方法,您可以在这里使用date_sub

关于Spark REPL的示例:

 scala> spark.sql("select date_sub(current_timestamp(), 1)").show
+----------------------------------------------+
|date_sub(CAST(current_timestamp() AS DATE), 1)|
+----------------------------------------------+
|                                    2016-12-12|
+----------------------------------------------+

你可以试试

date_add(current_date(), -1)

我也不知道spark,但我在谷歌上找到的。 您也可以使用这个link作为参考

算术函数允许您对包含日期的列执行算术运算。

例如,您可以计算两个日期之间的差异、向日期中添加天数或从日期中减去天数。内置的日期算术函数包括datediffdate_add^{}add_monthslast_daynext_day,和months_between

我们需要的是

date_sub(timestamp startdate, int days), Purpose: Subtracts a specified number of days from a TIMESTAMP value. The first argument can be a string, which is automatically cast to TIMESTAMP if it uses the recognized format, as described in TIMESTAMP Data Type. Return type: timestamp

我们有

current_timestamp() Purpose: Alias for the now() function. Return type: timestamp

你可以选择

date_sub(CAST(current_timestamp() as DATE), 1)

https://spark.apache.org/docs/1.6.2/api/java/org/apache/spark/sql/functions.html

相关问题 更多 >