大数据技术架构(组件)11——Hive:日期函数

1.4.5、日期函数

1.4.5.1、from_unixtime

select from_unixtime(1638602968),from_unixtime(1638602968,'yyyy-MM-dd HH:mm:SS'),from_unixtime(1638602968,'yyyy-MM-dd');

1.4.5.2、unix_timestamp

select unix_timestamp();

1.4.5.3、to_date

select to_date('2021-12-04 2021-12-04 15:29:28'),to_date('2021-12-04 15:29:28');

1.4.5.4、year

select year('2021-12-04 15:29:28');

1.4.5.5、quarter-->Hive1.3.0

1.4.5.6、month

select month('2021-12-04 15:29:28');

1.4.5.7、day

select day('2021-12-04 15:29:28');

1.4.5.8、hour

select hour('2021-12-04 15:29:28');

1.4.5.9、minute

select minute('2021-12-04 15:29:28');

1.4.5.10、second

select second('2021-12-04 15:29:28');

1.4.5.11、weekofyear

select weekofyear('2021-12-04 15:29:28');

1.4.5.12、extract-->Hive2.2.0

1.4.5.13、datediff

select datediff('2009-03-01', '2009-02-27') ;

1.4.5.14、date_add

select date_add('2021-12-04 15:29:28',1);

1.4.5.15、date_sub

select date_add('2021-12-04 15:29:28',-1),date_sub('2021-12-04 15:29:28',1),date_sub('2021-12-04 15:29:28',-1);

1.4.5.16、from_utc_timestamp-->Hive0.8.0

select  from_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') ,from_utc_timestamp(2592000.0,'PST');

1.4.5.17、to_utc_timestamp-->Hive0.8.0

select to_utc_timestamp(2592000.0,'PST'), to_utc_timestamp(2592000000,'PST'),to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST') ;

1.4.5.18、current_date -->Hive1.2.0

select current_date();

1.4.5.19、current_timestamp -->Hive1.2.0

select current_timestamp();

1.4.5.20、add_months-->Hive1.1.0

select add_months('2009-08-31', 1),add_months('2017-12-31 14:15:16',3) ;

1.4.5.21、last_day --> Hive1.1.0

select last_day('2021-12-04');

1.4.5.22、next_day --> Hive1.2.0

--获取下一个周二

select next_day('2021-12-04', 'TU');

1.4.5.23、trunc --> Hive1.2.0

--返回截断为格式指定单位的日期(从 Hive 1.2.0 开始)。支持的格式:MONTH/MON/MM、YEAR/YYYY/YY。

select trunc('2021-12-04', 'MM');

1.4.5.24、months_between-->Hive1.2.0

1.4.5.25、date_format --> Hive1.2.0

支持版本+

返回值类型

函数名称

功能描述

string

from_unixtime(bigint unixtime[, string format])

将 unix epoch (1970-01-01 00:00:00 UTC) 的秒数转换为表示当前系统时区中该时刻时间戳的字符串,格式为“1970-01-01 00:00: 00”。

bigint

unix_timestamp()

以秒为单位获取当前的 Unix 时间戳。这个函数不是确定性的,它的值在查询执行的范围内不是固定的,因此阻止了查询的正确优化 - 自 2.0 以来,它已被弃用,以支持 CURRENT_TIMESTAMP 常量

bigint

unix_timestamp(string date)

将格式为 yyyy-MM-dd HH:mm:ss 的时间字符串转换为 Unix 时间戳(以秒为单位),使用默认时区和默认语言环境,如果失败则返回 0: unix_timestamp('2009-03-20 11:30:01 ') = 1237573801

bigint

unix_timestamp(string date, string pattern)

将具有给定模式的时间字符串转换为 Unix 时间戳(以秒为单位),如果失败则返回 0:unix_timestamp('2009-03-20', 'yyyy-MM-dd') = 1237532400

pre 2.1.0: string

2.1.0 on: date

to_date(string timestamp)

返回时间戳字符串的日期部分(Hive 2.1.0 之前):to_date("1970-01-01 00:00:00") = "1970-01-01"。从 Hive 2.1.0 开始,返回一个日期对象。

在 Hive 2.1.0 (HIVE-13248) 之前,返回类型是 String,因为在创建方法时不存在 Date 类型。

int

year(string date)

返回日期或时间戳字符串的年份部分:year("1970-01-01 00:00:00") = 1970, year("1970-01-01") = 1970。

Hive1.3.0

int

quarter(date/timestamp/string)

返回 1 到 4 范围内的日期、时间戳或字符串的一年中的季度(从 Hive 1.3.0 开始))。示例:季度('2015-04-08')= 2

int

month(string date)

返回日期或时间戳字符串的月份部分:month("1970-11-01 00:00:00") = 11, month("1970-11-01") = 11

int

day(string date) dayofmonth(date)

返回日期或时间戳字符串的日期部分:day("1970-11-01 00:00:00") = 1, day("1970-11-01") = 1

int

hour(string date)

返回时间戳的小时数:hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12

int

minute(string date)

返回时间戳的分钟。

int

second(string date)

返回时间戳的第二个

int

weekofyear(string date)

返回时间戳字符串的周数:weekofyear("1970-11-01 00:00:00") = 44, weekofyear("1970-11-01") = 44

Hive2.2.0

int

extract(field FROM source)

从源(从 Hive 2.2.0 开始)检索字段,例如天数或小时数。来源必须是日期、时间戳、间隔或可以转换为日期或时间戳的字符串。支持的字段包括:天、星期几、小时、分钟、月、季度、秒、周和年

示例:

select extract(month from "2016-10-20") results in 10.

select extract(hour from "2016-10-20 05:06:07") results in 5.

select extract(dayofweek from "2016-10-20 05:06:07") results in 5.

select extract(month from interval '1-3' year to month) results in 3.

select extract(minute from interval '3 12:20:30' day to second) results in 20.

int

datediff(string enddate, string startdate)

返回从 startdate 到 enddate 的天数:datediff('2009-03-01', '2009-02-27') = 2。

pre 2.1.0: string

2.1.0 on: date

date_add(date/timestamp/string startdate, tinyint/smallint/int days)

将天数添加到开始日期:date_add('2008-12-31', 1) = '2009-01-01'。在 Hive 2.1.0 (HIVE-13248) 之前,返回类型是 String,因为在创建方法时不存在 Date 类型

pre 2.1.0: string

2.1.0 on: date

date_sub(date/timestamp/string startdate, tinyint/smallint/int days)

减去开始日期的天数:date_sub('2008-12-31', 1) = '2008-12-30'。在 Hive 2.1.0 (HIVE-13248) 之前,返回类型是 String,因为在创建方法时不存在 Date 类型

Hive0.8.0

timestamp

from_utc_timestamp({any primitive type} ts, string timezone)

将 UTC 中的时间戳转换为给定的时区(从 Hive 0.8.0 开始)。timestamp 是原始类型,包括timestamp/date、tinyint/smallint/int/bigint、float/double 和decimal。小数值被视为秒。整数值被视为毫秒。例如 from_utc_timestamp(2592000.0,'PST'), from_utc_timestamp(2592000000,'PST') 和 from_utc_timestamp(timestamp '1970-01-30 16:00:00','PST')1930-1930-1930-时间戳08:00:00

Hive0.8.0

timestamp

to_utc_timestamp({any primitive type} ts, string timezone)

将给定时区中的时间戳 转换为 UTC(从 Hive 0.8.0 开始)。timestamp 是原始类型,包括timestamp/date、tinyint/smallint/int/bigint、float/double 和decimal。小数值被视为秒。整数值被视为毫秒。例如 to_utc_timestamp(2592000.0,'PST'), to_utc_timestamp(2592000000,'PST') 和 to_utc_timestamp(timestamp '1970-01-30 16:00:00','PST')1930-1930-1930-timestamp都返回时间戳00:00:00

Hive1.2.0

date

current_date

返回查询开始时的当前日期(从Hive 1.2.0开始)。在同一个查询中对current_date的所有调用都会返回相同的值

Hive1.2.0

timestamp

current_timestamp

返回查询评估开始时的当前时间戳(从Hive 1.2.0开始)。在同一个查询中调用current_timestamp,都会返回相同的值

Hive1.1.0

string

add_months(string start_date, int num_months, output_date_format)

返回 start_date 之后 num_months 的日期(从 Hive 1.1.0 开始)。 start_date 是一个字符串、日期或时间戳。 num_months 是一个整数。如果 start_date 是该月的最后一天,或者如果结果月份的天数少于 start_date 的日期部分,则结果是结果月份的最后一天。否则,结果与 start_date 具有相同的日期部分。默认输出格式为“yyyy-MM-dd”。在 Hive 4.0.0 之前,忽略日期的时间部分。

从 Hive 4.0.0 开始,add_months 支持可选参数 output_date_format,它接受一个表示输出的有效日期格式的 String。这允许在输出中保留时间格式

For example :

add_months('2009-08-31', 1) 返回'2009-09-30'.add_months('2017-12-31 14:15:16', 2, 'YYYY-MM-dd HH:mm:ss') 返回'2018-02-28 14:15:16'.

Hive1.1.0

string

last_day(string date)

返回日期所属月份的最后一天(从 Hive 1.1.0 开始)。日期是格式为“yyyy-MM-dd HH:mm:ss”或“yyyy-MM-dd”的字符串。日期的时间部分被忽略。

Hive1.2.0

string

next_day(string start_date, string day_of_week)

返回晚于 start_date 并命名为 day_of_week 的第一个日期(从 Hive 1.2.0)。 start_date 是一个字符串/日期/时间戳。 day_of_week 是星期几的 2 个字母、3 个字母或全名(例如 Mo、tue、FRIDAY)。 start_date 的时间部分被忽略。示例:next_day('2015-01-14', 'TU') = 2015-01-20

Hive1.2.0

string

trunc(string date, string format)

返回截断为格式指定单位的日期(从 Hive 1.2.0 开始)。支持的格式:MONTH/MON/MM、YEAR/YYYY/YY。示例: trunc('2015-03-17', 'MM') = 2015-03-01

Hive1.2.0

double

months_between(date1, date2)

返回日期 date1 和 date2 之间的月数(从 Hive 1.2.0)。如果 date1 晚于 date2,则结果为正。如果 date1 早于 date2,则结果为负数。如果 date1 和 date2 是该月的同一天或都是该月的最后几天,则结果始终为整数。否则,UDF 会根据有 31 天的月份计算结果的小数部分,并考虑时间分量 date1 和 date2 的差异。 date1 和 date2 类型可以是日期、时间戳或字符串,格式为“yyyy-MM-dd”或“yyyy-MM-dd HH:mm:ss”。结果四舍五入到小数点后 8 位。示例:months_between('1997-02-28 10:30:00', '1996-10-30') = 3.94959677

Hive1.2.0

string

date_format(date/timestamp/string ts, string fmt)

将日期/时间戳/字符串转换为日期格式 fmt 指定格式的字符串值(从 Hive 1.2.0 开始)。第二个参数 fmt 应该是常量. 示例: date_format('2015-04-08', 'y') = '2015'.

date_format can be used to implement other UDFs, e.g.:

●dayname(date) is date_format(date, 'EEEE')

●dayofyear(date) is date_format(date, 'D')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mylife512

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值