玩转大数据大数据,机器学习,人工智能大数据 爬虫Python AI Sql

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

2020-04-15  本文已影响0人  四毛m
  • Hive的数学函数包含两种,一种是内嵌函数,一种是由java定义的自定义函数。
  • 对于不懂Java的数据分析师,掌握Hive的内嵌函数就显得尤为重要啦~

Hive内嵌函数都有哪些?

1.内置函数

四毛的图2.png

1.1.日期函数

select to_date('2017-07-03 11:30:00')
>>2017-07-03
select year('2017-07-03 11:30:00')
>> 2017
select weekofyear('2017-07-03 11:30:00')
>> 27
select datediff('2020-04-14 11:30:00','2017-07-03 11:30:00')
>> 1016
select date_add('2019-04-19 11:30:00',2)
>> 2019-04-21

1.2.转换函数

select cast(3 as float)
>> 3.0

1.3.数学函数

select round(4.98372,2)
>> 4.98
select ceil(4.98372)
>> 5
select floor(4.98372)
>> 4

1.4.条件函数

select coalesce(null,0,3,null,5);
>> 0
select case 1 when 1>3 then 1 else 3 end
>> 3

1.5.字符函数

select lower('Hello'),upper('Hello')
>>hello HELLO
select length('Hello')
>>5
select concat('Hello', ' World')
>>Hello World
select substr('Hello World',3,6)
>>llo Wo
select trim('   love   ')
>>love
select lpad('happy',10,'l'), rpad('happy', 10, 'l')
>>lllllhappy  happylllll

1.6.收集函数

select size(map(1,'love',2,'happy')
>> 2

2.聚合函数

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

select sex,count(id) from beauty where id is not null group by sex
>> 女 12
   男 13
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
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
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.表生成函数

select explode(map(1,'Tom',2,'Mary'))
>> 1 Tom
   2 Mary

文章说明

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

猜你喜欢

热点阅读