# Hive常用函数大全一览

## 关系运算

### 1、等值比较: =

```hive> select 1 from iteblog where 1=1;
1
```

### 2、不等值比较: <>

```hive> select 1 from iteblog where 1 <> 2;
1
```

### 3、小于比较: <

```hive> select 1 from iteblog where 1 < 2;
1
```

### 4、小于等于比较: <=

```hive> select 1 from iteblog where 1 < = 1;
1
```

### 5、大于比较: >

```hive> select 1 from iteblog where 2 > 1;
1
```

### 6、大于等于比较: >=

```hive> select 1 from iteblog where 1 >= 1;
1
```

```hive> select * from iteblog;
OK
2011111209 00:00:00     2011111209

hive> select a, b, a<b, a>b, a=b from iteblog;
2011111209 00:00:00     2011111209      false   true    false
```

### 7、空值判断: IS NULL

```hive> select 1 from iteblog where null is null;
1
```

### 8、非空判断: IS NOT NULL

```hive> select 1 from iteblog where 1 is not null;
1
```

### 9、LIKE比较: LIKE

```hive> select 1 from iteblog where 'football' like 'foot%';
1
hive> select 1 from iteblog where 'football' like 'foot____';
1
<strong>注意：否定比较时候用NOT A LIKE B</strong>
hive> select 1 from iteblog where NOT 'football' like 'fff%';
1
```

### 10、JAVA的LIKE操作: RLIKE

```hive> select 1 from iteblog where 'footbar’ rlike '^f.*r\$’;
1

hive>select 1 from iteblog where '123456' rlike '^\\d+\$';
1
hive> select 1 from iteblog where '123456aa' rlike '^\\d+\$';
```

### 11、REGEXP操作: REGEXP

```hive> select 1 from iteblog where 'footbar' REGEXP '^f.*r\$';
1
```

## 数学运算：

### 1、加法操作: +

```hive> select 1 + 9 from iteblog;
10
hive> create table iteblog as select 1 + 1.2 from iteblog;
hive> describe iteblog;
_c0     double
```

### 2、减法操作: -

```hive> select 10 – 5 from iteblog;
5
hive> create table iteblog as select 5.6 – 4 from iteblog;
hive> describe iteblog;
_c0     double
```

### 3、乘法操作: *

```hive> select 40 * 5 from iteblog;
200
```

### 4、除法操作: /

```hive> select 40 / 5 from iteblog;
8.0
```

```hive>select ceil(28.0/6.999999999999999999999) from iteblog limit 1;

hive>select ceil(28.0/6.99999999999999) from iteblog limit 1;

```

### 5、取余操作: %

```hive> select 41 % 5 from iteblog;
1
hive> select 8.4 % 4 from iteblog;
0.40000000000000036
<strong>注意</strong>：精度在hive中是个很大的问题，类似这样的操作最好通过round指定精度
hive> select round(8.4 % 4 , 2) from iteblog;
0.4
```

### 6、位与操作: &

```hive> select 4 & 8 from iteblog;
0
hive> select 6 & 4 from iteblog;
4
```

### 7、位或操作: |

```hive> select 4 | 8 from iteblog;
12
hive> select 6 | 8 from iteblog;
14
```

### 8、位异或操作: ^

```hive> select 4 ^ 8 from iteblog;
12
hive> select 6 ^ 4 from iteblog;
2
```

### 9．位取反操作: ~

```hive> select ~6 from iteblog;
-7
hive> select ~4 from iteblog;
-5
```

## 逻辑运算：

### 1、逻辑与操作: AND

```hive> select 1 from iteblog where 1=1 and 2=2;
1
```

### 2、逻辑或操作: OR

```hive> select 1 from iteblog where 1=2 or 2=2;
1
```

### 3、逻辑非操作: NOT

```hive> select 1 from iteblog where not 1=2;
1
```

## 数值计算

### 1、取整函数: round

```hive> select round(3.1415926) from iteblog;
3
hive> select round(3.5) from iteblog;
4
hive> create table iteblog as select round(9542.158) from iteblog;
hive> describe iteblog;
_c0     bigint
```

### 2、指定精度取整函数: round

```hive> select round(3.1415926,4) from iteblog;
3.1416
```

### 3、向下取整函数: floor

```hive> select floor(3.1415926) from iteblog;
3
hive> select floor(25) from iteblog;
25
```

### 4、向上取整函数: ceil

```hive> select ceil(3.1415926) from iteblog;
4
hive> select ceil(46) from iteblog;
46
```

### 5、向上取整函数: ceiling

```hive> select ceiling(3.1415926) from iteblog;
4
hive> select ceiling(46) from iteblog;
46
```

### 6、取随机数函数: rand

```hive> select rand() from iteblog;
0.5577432776034763
hive> select rand() from iteblog;
0.6638336467363424
hive> select rand(100) from iteblog;
0.7220096548596434
hive> select rand(100) from iteblog;
0.7220096548596434
```

### 7、自然指数函数: exp

```hive> select exp(2) from iteblog;
7.38905609893065
<strong>自然对数函数</strong>: ln
<strong>语法</strong>: ln(double a)
<strong>返回值</strong>: double
<strong>说明</strong>: 返回a的自然对数
1
hive> select ln(7.38905609893065) from iteblog;
2.0
```

### 8、以10为底对数函数: log10

```hive> select log10(100) from iteblog;
2.0
```

### 9、以2为底对数函数: log2

```hive> select log2(8) from iteblog;
3.0
```

### 10、对数函数: log

```hive> select log(4,256) from iteblog;
4.0
```

### 11、幂运算函数: pow

```hive> select pow(2,4) from iteblog;
16.0
```

### 12、幂运算函数: power

```hive> select power(2,4) from iteblog;
16.0
```

### 13、开平方函数: sqrt

```hive> select sqrt(16) from iteblog;
4.0
```

### 14、二进制函数: bin

```hive> select bin(7) from iteblog;
111
```

### 15、十六进制函数: hex

```hive> select hex(17) from iteblog;
11
hive> select hex(‘abc’) from iteblog;
616263
```

### 16、反转十六进制函数: unhex

```hive> select unhex(‘616263’) from iteblog;
abc
hive> select unhex(‘11’) from iteblog;
-
hive> select unhex(616263) from iteblog;
abc
```

### 17、进制转换函数: conv

```hive> select conv(17,10,16) from iteblog;
11
hive> select conv(17,10,2) from iteblog;
10001
```

### 18、绝对值函数: abs

```hive> select abs(-3.9) from iteblog;
3.9
hive> select abs(10.9) from iteblog;
10.9
```

### 19、正取余函数: pmod

```hive> select pmod(9,4) from iteblog;
1
hive> select pmod(-9,4) from iteblog;
3
```

### 20、正弦函数: sin

```hive> select sin(0.8) from iteblog;
0.7173560908995228
```

### 21、反正弦函数: asin

```hive> select asin(0.7173560908995228) from iteblog;
0.8
```

### 22、余弦函数: cos

```hive> select cos(0.9) from iteblog;
0.6216099682706644
```

### 23、反余弦函数: acos

```hive> select acos(0.6216099682706644) from iteblog;
0.9
```

### 24、positive函数: positive

```hive> select positive(-10) from iteblog;
-10
hive> select positive(12) from iteblog;
12
```

### 25、negative函数: negative

```hive> select negative(-5) from iteblog;
5
hive> select negative(8) from iteblog;
-8
```

## 日期函数

### 1、UNIX时间戳转日期函数: from_unixtime

```hive> select from_unixtime(1323308943,'yyyyMMdd') from iteblog;
20111208
```

### 2、获取当前UNIX时间戳函数: unix_timestamp

```hive> select unix_timestamp() from iteblog;
1323309615
```

### 3、日期转UNIX时间戳函数: unix_timestamp

```hive> select unix_timestamp('2011-12-07 13:01:03') from iteblog;
1323234063
```

### 4、指定格式日期转UNIX时间戳函数: unix_timestamp

```hive> select unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from iteblog;
1323234063
```

### 5、日期时间转日期函数: to_date

```hive> select to_date('2011-12-08 10:03:01') from iteblog;
2011-12-08
```

### 6、日期转年函数: year

```hive> select year('2011-12-08 10:03:01') from iteblog;
2011
hive> select year('2012-12-08') from iteblog;
2012
```

### 7、日期转月函数: month

```hive> select month('2011-12-08 10:03:01') from iteblog;
12
hive> select month('2011-08-08') from iteblog;
8
```

### 8、日期转天函数: day

```hive> select day('2011-12-08 10:03:01') from iteblog;
8
hive> select day('2011-12-24') from iteblog;
24
```

### 9、日期转小时函数: hour

```hive> select hour('2011-12-08 10:03:01') from iteblog;
10
```

### 10、日期转分钟函数: minute

```hive> select minute('2011-12-08 10:03:01') from iteblog;
3
```

### 11、日期转秒函数: second

```hive> select second('2011-12-08 10:03:01') from iteblog;
1
```

### 12、日期转周函数: weekofyear

```hive> select weekofyear('2011-12-08 10:03:01') from iteblog;
49
```

### 13、日期比较函数: datediff

```hive> select datediff('2012-12-08','2012-05-09') from iteblog;
213
```

```hive> select date_add('2012-12-08',10) from iteblog;
2012-12-18
```

### 15、日期减少函数: date_sub

```hive> select date_sub('2012-12-08',10) from iteblog;
2012-11-28
```

## 条件函数

### 1、If函数: if

```hive> select if(1=2,100,200) from iteblog;
200
hive> select if(1=1,100,200) from iteblog;
100
```

### 2、非空查找函数: COALESCE

```hive> select COALESCE(null,'100','50′) from iteblog;
100
```

### 3、条件判断函数：CASE

```hive> Select case 100 when 50 then 'tom' when 100 then 'mary' else 'tim' end from iteblog;
mary
hive> Select case 200 when 50 then 'tom' when 100 then 'mary' else 'tim' end from iteblog;
tim
```

### 4、条件判断函数：CASE

```hive> select case when 1=2 then 'tom' when 2=2 then 'mary' else 'tim' end from iteblog;
mary
hive> select case when 1=1 then 'tom' when 2=2 then 'mary' else 'tim' end from iteblog;
tom
```

## 字符串函数

### 1、字符串长度函数：length

```hive> select length('abcedfg') from iteblog;
7
```

### 2、字符串反转函数：reverse

```hive> select reverse(abcedfg’) from iteblog;
gfdecba
```

### 3、字符串连接函数：concat

```hive> select concat(‘abc’,'def’,'gh’) from iteblog;
abcdefgh
```

### 4、带分隔符字符串连接函数：concat_ws

```hive> select concat_ws(',','abc','def','gh') from iteblog;
abc,def,gh
```

### 5、字符串截取函数：substr,substring

```hive> select substr('abcde',3) from iteblog;
cde
hive> select substring('abcde',3) from iteblog;
cde
hive>  select substr('abcde',-1) from iteblog;  （和ORACLE相同）
e
```

### 6、字符串截取函数：substr,substring

```hive> select substr('abcde',3,2) from iteblog;
cd
hive> select substring('abcde',3,2) from iteblog;
cd
hive>select substring('abcde',-2,2) from iteblog;
de
```

### 7、字符串转大写函数：upper,ucase

```hive> select upper('abSEd') from iteblog;
ABSED
hive> select ucase('abSEd') from iteblog;
ABSED
```

### 8、字符串转小写函数：lower,lcase

```hive> select lower('abSEd') from iteblog;
absed
hive> select lcase('abSEd') from iteblog;
absed
```

### 9、去空格函数：trim

```hive> select trim(' abc ') from iteblog;
abc
```

### 10、左边去空格函数：ltrim

```hive> select ltrim(' abc ') from iteblog;
abc
```

### 11、右边去空格函数：rtrim

```hive> select rtrim(' abc ') from iteblog;
abc
```

### 12、正则表达式替换函数：regexp_replace

```hive> select regexp_replace('foobar', 'oo|ar', '') from iteblog;
fb
```

### 13、正则表达式解析函数：regexp_extract

```hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from iteblog;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from iteblog;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from iteblog;
foothebar
strong>注意，在有些情况下要使用转义字符，下面的等号要用双竖线转义，这是java正则表达式的规则。
select data_field,
regexp_extract(data_field,'.*?bgStart\\=([^&]+)',1) as aaa,
where pt = '2012-03-26' limit 2;
```

### 14、URL解析函数：parse_url

```hive> select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'HOST') from iteblog;
hive> select parse_url('https://www.iteblog.com/path1/p.php?k1=v1&k2=v2#Ref1', 'QUERY', 'k1') from iteblog;
v1
```

### 15、json解析函数：get_json_object

```hive> select  get_json_object('{"store":
>   {"fruit":\[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],
>    "bicycle":{"price":19.95,"color":"red"}
>   },
>  "email":"amy@only_for_json_udf_test.net",
>  "owner":"amy"
> }
> ','\$.owner') from iteblog;
amy
```

### 16、空格字符串函数：space

```hive> select space(10) from iteblog;
hive> select length(space(10)) from iteblog;
10
```

### 17、重复字符串函数：repeat

```hive> select repeat('abc',5) from iteblog;
abcabcabcabcabc
```

### 18、首字符ascii函数：ascii

```hive> select ascii('abcde') from iteblog;
97
```

```hive> select lpad('abc',10,'td') from iteblog;
tdtdtdtabc

```

```hive> select rpad('abc',10,'td') from iteblog;
abctdtdtdt
```

### 21、分割字符串函数: split

```hive> select split('abtcdtef','t') from iteblog;
["ab","cd","ef"]
```

### 22、集合查找函数: find_in_set

```hive> select find_in_set('ab','ef,ab,de') from iteblog;
2
hive> select find_in_set('at','ef,ab,de') from iteblog;
0
```

## 集合统计函数

### 1、个数统计函数: count

```hive> select count(*) from iteblog;
20
hive> select count(distinct t) from iteblog;
10
```

### 2、总和统计函数: sum

```hive> select sum(t) from iteblog;
100
hive> select sum(distinct t) from iteblog;
70
```

### 3、平均值统计函数: avg

```hive> select avg(t) from iteblog;
50
hive> select avg (distinct t) from iteblog;
30
```

### 4、最小值统计函数: min

```hive> select min(t) from iteblog;
20
```

### 5、最大值统计函数: max

```hive> select max(t) from iteblog;
120
```

### 11、中位数函数: percentile

```select percentile(score,&lt;0.2,0.4>) from iteblog； 取0.2，0.4位置的数据
```

### 14、直方图: histogram_numeric

```hive> select histogram_numeric(100,5) from iteblog;
[{"x":100.0,"y":1.0}]
```

## 复合类型构建操作

### 1、Map类型构建: map

```hive> Create table iteblog as select map('100','tom','200','mary') as t from iteblog;
hive> describe iteblog;
t       map<string ,string>
hive> select t from iteblog;
{"100":"tom","200":"mary"}
```

### 2、Struct类型构建: struct

```hive> create table iteblog as select struct('tom','mary','tim') as t from iteblog;
hive> describe iteblog;
t       struct<col1:string ,col2:string,col3:string>
hive> select t from iteblog;
{"col1":"tom","col2":"mary","col3":"tim"}
```

### 3、array类型构建: array

```hive> create table iteblog as select array("tom","mary","tim") as t from iteblog;
hive> describe iteblog;
t       array<string>
hive> select t from iteblog;
["tom","mary","tim"]
```

## 复杂类型访问操作

### 1、array类型访问: A[n]

```hive> create table iteblog as select array("tom","mary","tim") as t from iteblog;
hive> select t[0],t[1],t[2] from iteblog;
tom     mary    tim
```

### 2、map类型访问: M[key]

```hive> Create table iteblog as select map('100','tom','200','mary') as t from iteblog;
hive> select t['200'],t['100'] from iteblog;
mary    tom
```

### 3、struct类型访问: S.x

```hive> create table iteblog as select struct('tom','mary','tim') as t from iteblog;
hive> describe iteblog;
t       struct<col1:string ,col2:string,col3:string>
hive> select t.col1,t.col3 from iteblog;
tom     tim
```

## 复杂类型长度统计函数

### 1.Map类型长度函数: size(Map<k .V>)

```hive> select size(map('100','tom','101','mary')) from iteblog;
2
```

### 2.array类型长度函数: size(Array<T>)

```hive> select size(array('100','101','102','103')) from iteblog;
4
```

### 3.类型转换函数

```hive> select cast(1 as bigint) from iteblog;
1
```

(3)个小伙伴在吐槽
1. 要是有目录就完美了

李静2021-03-23 14:50 回复
• 文章的右上角不是有个 文章目录 吗，点开来就是目录。

w3970907702021-03-26 22:37 回复
2. 够全

梦游攀山2018-04-03 21:02 回复