hive中常用的函数

一.日期函数

1.unix_timestamp(获取时间戳:秒)

获取当前时间戳

hive> select unix_timestamp();
OK
1605712071

获取指定日期时间戳

hive> select unix_timestamp('2020-01-01 00:00:00');
OK
1577836800

获取指定格式的时间戳

hive> select unix_timestamp('20200101 00:00:00','yyyyMMdd HH:mm:ss');
OK
1577836800

2.from_unixtime(将时间戳转为日期格式,秒)

格式:from_unixtime(bigint unixtime[, string format])

hive> select from_unixtime(1577836800,'yyyy-MM-dd HH:mm:ss');
OK
2020-01-01 00:00:00

3.current_date(当前日期)

hive> select current_date();
OK
2020-11-18

4.current_timestamp(当前的日期加时间)

hive> select current_timestamp();
OK
2020-11-18 23:21:15.19

5.to_date(抽取日期部分)

hive> select to_date('2020-01-01 12:12:12');
OK
2020-01-01

6.year、month、 day 、hour、 minute、 second(获取年月日时分秒)

hive> select year('2020-01-01 12:12:12');
OK
2020

hive> select month('2020-01-01 12:12:12');
OK
1

hive> select day('2020-01-01 12:12:12');
OK
1

hive> select hour('2020-01-01 12:13:14');
OK
12

hive> select minute('2020-01-01 12:13:14');
OK
13

hive> select second('2020-01-01 12:13:14');
OK
14

7.weekofyear(当前时间是一年中的第几周)

当前周

hive> select weekofyear(current_date());
OK
47

传入日期

hive> select weekofyear('2020-01-01');
OK
1

8.dayofmonth(当前时间是一个月中的第几天)

hive> select dayofmonth('2020-02-02');
OK
2

9.months_between(两个日期间的月份)

用于计算date1和date2之间有几个月。    如果date1在日历中比date2晚,那么MONTHS_BETWEEN()就返回一个正数。

如果date1在日历中比date2早,那么months_between()就返回一个负数。
如果date1和date2日期一样,那么months_between()就返回一个0。

hive> select months_between('2021-01-01','2020-01-01');
OK
12.0

hive> select months_between('2020-01-01','2020-11-11');
OK
-10.32258065

hive> select months_between('2021-11-01','2020-11-11');
OK
11.67741935

10.add_months(日期加减月)

hive> select add_months('2020-01-01',12);
OK
2021-01-01

hive> select add_months('2020-01-01',-12);
OK
2019-01-01

 

11.datediff(两个日期相差的天数)

hive> select datediff('2020-01-01','2020-02-01');
OK
-31

hive> select datediff('2020-03-01','2020-02-01');
OK
29

12.date_add、date_sub(日期加天数,日期减天数)

hive> select date_add('2020-01-02',4);
OK
2020-01-06

hive> select date_sub('2020-01-02',4);
OK
2019-12-29

13.last_day(日期的当月的最后一天)

hive> select last_day('2020-06-01 12:12:12');
OK
2020-06-30

14.date_format()(格式化日期)

hive> select date_format('2020-01-01','yyyy/MM/dd HH:mm:ss');
OK
2020/01/01 00:00:00

 

二.常用取整函数

1.round(四舍五入)

hive> select round(12.123,2);
OK
12.12

hive> select round(12.126,2);
OK
12.13

2.ceil(向上取整)

hive> select ceil(12.36);
OK
13

hive> select ceil(12.56);
OK
13

3.floor(向下取整)

hive> select floor(12.12);
OK
12

hive> select floor(12.62);
OK
12

三.字符串转换函数

1.STR_TO_MAP

STR_TO_MAP(VARCHAR text, VARCHAR listDelimiter, VARCHAR keyValueDelimiter)

使用listDelimiter将text分隔成K-V对,然后使用keyValueDelimiter分隔每个K-V对,组装成MAP返回。默认listDelimiter为( ,),keyValueDelimiter为(:

str_to_map('1001=2020-06-14,1002=2020-06-14',  ','  ,  '=')
输出
{"1001":"2020-06-14","1002":"2020-06-14"}

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值