如何获得今天-"1天"日期在sparksql中? [英] How to get today -"1 day" date in sparksql?

查看:1086
本文介绍了如何获得今天-"1天"日期在sparksql中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

算术函数使您可以对包含日期的列执行算术运算.

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

例如,您可以计算两个日期之间的差,为日期添加天数,或从日期中减去天数.内置的日期算术函数包括datediffdate_add

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.

我们最需要的是

date_sub(时间戳记开始日期,整数天),用途:减去指定的天数 从TIMESTAMP值.第一个参数可以是一个字符串,它是 如果使用公认的格式,则会自动转换为TIMESTAMP,例如 TIMESTAMP数据类型中所述.返回类型:返回开始前>天天的日期

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

我们有

current_timestamp()用途:now()函数的别名.返回 类型:时间戳

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

这篇关于如何获得今天-"1天"日期在sparksql中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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