Hive常用函数介绍(一)

1、round

1
2
3
4
5
6
7
--四舍五入取整
>select round(3.5)
>4

--指定精度取整
>select round(3.1415926,4)
>3.1416

2、floor

1
2
3
--向下取整
>select floor(3.1415926)
>3

3、ceil/ceiling

1
2
3
--向上取整
>select ceil(3.1415926)
>4

4、rand

1
2
3
--返回0到1范围内的随机数
>select rand()
>0.32084268521140447

5、like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--比较运算符,A like B。如果字符串A或者字符串B为NULL,则返回NULL;
--如果字符串A符合表达式B 的正则语法,则为TRUE;否则为FALSE。
--B中字符”_”表示任意单个字符,而字符”%”表示任意数量的字符。
>select 'football' like 'foot%'
>true

>select 'football' like 'foot____'
>true

--not A like B 或者 A not like B
>select not 'football' like 'fff%'
>true

>select 'football' not like 'fff%'
>true

6、rlike/regexp

1
2
3
4
5
6
7
8
--比较运算符,A rlike B。如果字符串A或者字符串B为NULL,则返回NULL;
--如果字符串A符合表达式B 的正则语法,则为TRUE;否则为FALSE。
--not rlike用法与like一致
>select 'footbar' rlike '^f.*r$'
>true

>select '123456' rlike '^\d+$'
>true

7、if

1
2
3
4
5
6
7
--if(boolean testCondition, T valueTrue, T valueFalseOrNull)
--当条件testCondition为TRUE时,返回valueTrue;否则返回valueFalseOrNull
>select if(1=2,100,200)
>200

>select if(1=1,100,200)
>100

8、coalesce

1
2
3
--返回参数中的第一个非空值;如果所有值都为NULL,那么返回NULL
>select coalesce(null,'100','50')
>100

9、case…when…then…else…end

1
2
3
4
5
>select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end
>mary

>select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end
>mary

10、cast

1
2
3
--类型转换函数,不是所有类型都可以转换,无法转换时返回null
>select cast(3.1415926 as int)
>3