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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41114875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:09:36  来源:igfitidea点击:

How to get today -"1 day" date in sparksql?

javapythonscalaapache-sparkapache-spark-sql

提问by Vishan Rana

How to get current_date - 1day in sparksql, same as cur_date()-1in mysql.

如何current_date - 1在 sparksql 中获取日期,与cur_date()-1在 mysql 中相同。

采纳答案by Ram Ghadiyaram

The arithmetic functions allow you to perform arithmetic operation on columns containing dates.

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

For example, you can calculate the difference between two dates, add days to a date, or subtract days from a date. The built-in date arithmetic functions include datediff, date_add, date_sub, add_months, last_day, next_day, and months_between.

例如,您可以计算两个日期之间的差值、向日期添加天数或从日期中减去天数。内置的日期计算功能包括datediffdate_adddate_subadd_monthslast_daynext_day,和months_between

Out of above what we need is

除了上面我们需要的是

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: Returns the date that is > days days before start

date_sub(timestamp startdate, int days), 目的:从 TIMESTAMP 值中减去指定的天数。第一个参数可以是一个字符串,如果它使用可识别的格式,它会自动转换为 TIMESTAMP,如 TIMESTAMP 数据类型中所述。返回类型:返回开始前 > 天的日期

and we have

我们有

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

current_timestamp() 目的: now() 函数的别名。返回类型:时间戳

you can do select

你可以选择

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

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

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

回答by Shiv4nsh

You can easily perform this task , there are many methods related to the date and what you can use here is date_sub

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

Example on Spark-REPL:

Spark-REPL 示例:

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

回答by TAYFUN CANAKCI

You can try

你可以试试

date_add(current_date(), -1)

I don't know spark either but I found it on google. You can also use this linkfor reference

我也不知道火花,但我在谷歌上找到了它。您也可以使用此链接作为参考

回答by Kai

Yes, the date_sub()function is the right for the question, anyway, there's an error in the selected answer:

是的,该date_sub()函数适合该问题,无论如何,所选答案中存在错误:

Return type: timestamp

返回类型:时间戳

The return type should be dateinstead, date_sub() function will trim any hh:mm:sspart of the timestamp, and returns only a date.

返回类型应该是date, date_sub() 函数将修剪hh:mm:ss时间戳的任何部分,并且只返回一个date.