在大数据世界里打小怪之Hive的常用函数

  • Hive的数学函数包含两种,一种是内嵌函数,一种是由java定义的自定义函数。
  • 对于不懂Java的数据分析师,掌握Hive的内嵌函数就显得尤为重要啦~

Hive内嵌函数都有哪些?

  • 内嵌函数包括内置函数、聚合函数和表生成函数。
    四毛的图1.png

1.内置函数

四毛的图2.png

1.1.日期函数

  • to_date:取出字符串中的日期部分,eg:
select to_date('2017-07-03 11:30:00')
>>2017-07-03
  • year/month/day:取出日期当中的年/月/日,eg:
select year('2017-07-03 11:30:00')
>> 2017
  • weekofyear:返回日期在一年中的第几个星期,eg:
select weekofyear('2017-07-03 11:30:00')
>> 27
  • datediff:两个日期相差的天数,eg:
select datediff('2020-04-14 11:30:00','2017-07-03 11:30:00')
>> 1016
  • date_add/date_sub:在日期之上加上/减去特定天数
select date_add('2019-04-19 11:30:00',2)
>> 2019-04-21

1.2.转换函数

  • cast ( as):数据类型转换,eg:
select cast(3 as float)
>> 3.0

1.3.数学函数

  • round:对数据进行四舍五入,eg:
select round(4.98372,2)
>> 4.98
  • ceil:对数据进行向上取整,eg:
select ceil(4.98372)
>> 5
  • floor:对数据进行向下取整,eg:
select floor(4.98372)
>> 4

1.4.条件函数

  • coalesce:从左往右找到第一个不为空的值并输出,eg:
select coalesce(null,0,3,null,5);
>> 0
  • case…when…then…else…end:按条件进行输出,eg:
select case 1 when 1>3 then 1 else 3 end
>> 3

1.5.字符函数

  • lower/upper:将字符全部小写/全部大写,eg:
select lower('Hello'),upper('Hello')
>>hello HELLO
  • length:计算字符串长度(不是字节),eg:
select length('Hello')
>>5
  • concat:字符串串接,eg:
select concat('Hello', ' World')
>>Hello World
  • substr:截取指定字符数的字符,eg:
select substr('Hello World',3,6)
>>llo Wo
  • trim:删除字符串左右末尾的空格,eg:
select trim('   love   ')
>>love
  • lpad/rpad:左填充/右填充,eg:
select lpad('happy',10,'l'), rpad('happy', 10, 'l')
>>lllllhappy  happylllll

1.6.收集函数

  • size:计算map集合的个数,eg:
select size(map(1,'love',2,'happy')
>> 2

2.聚合函数

聚合函数常用于配合group by语句使用,用于聚合运算。常见的聚合函数有:

  • count:用于计数,eg:
select sex,count(id) from beauty where id is not null group by sex
>> 女 12
   男 13
  • sum:用于求和,eg:
select order_id,sum(sales_price) from order_details where order_id in (1,2) group by order_id
>> 1 450.0
   2 1330.8
  • min/max:求最大值/最小值,eg:
select order_id,max(sales_price) from order_details where order_id in (1,2) group by order_id
>> 1 68.2
   2 460.2
  • avg:求平均值,eg:
select order_id,avg(sales_price) from order_details where order_id in (1,2) group by order_id
>> 1 30.5
   2 300.4

3.表生成函数

  • explode:把map或array的每个元素各生成一行,eg:
select explode(map(1,'Tom',2,'Mary'))
>> 1 Tom
   2 Mary

文章说明

  • 这是本人学习imooc的《走近大数据之Hive进阶》课程所做的笔记,需要视频学习的小伙伴,imooc指路:https://www.imooc.com/learn/399 PS:课程免费哒~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值