SYP'S Blog

Ten thousand hours to explore the power of data science

hive的UDF 函数(User-Defined-Function)

UDF函数可以直接应用于select语句，对查询结构做格式化处理后，再输出内容。

关系运算

等值比较: =

hive> select 1 from dual where 1=1;
1

不等值比较: <>

hive> select 1 from dual where 1 <> 2;
1

小于比较: <

hive> select 1 from dual where 1 < 2;
1

小于等于比较: <=

hive> select 1 from dual where 1 <= 1;
1

大于比较: >

hive> select 1 from dual where 2 > 1;
1

大于等于比较: >=

hive> select 1 from dual where 1 >= 1;
1

String的比较要注意(常用的时间比较可以先to_date之后再比较)

hive> select  * from udftest;
OK
2011111209 00:00:00     2011111209
hive> select a,b,a<b,a>b,a=b from udftest;
2011111209 00:00:00     2011111209      false   true    false

空值判断: IS NULL

hive> select 1 from dual where null is null;
1

非空判断: IS NOT NULL

hive> select 1 from dual where 1 is not null;
1

LIKE比较: LIKE

hive> select 1 from dual where 'football' like 'foot%';
1
hive> select 1 from dual where 'football' like 'foot____';
1

hive> select 1 from dual where 'footbar' rlike '^f.*r$'; 1 REGEXP操作: REGEXP 语法: A REGEXP B 操作类型: strings 描述: 功能与RLIKE相同 举例： hive> select 1 from dual where 'footbar' REGEXP '^f.*r$';
1

数学运算

加法操作: +

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

减法操作: -

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

乘法操作: *

hive> select 40 * 5 from dual;
200

除法操作: /

hive> select 40 / 5 from dual;
8.0

取余操作: %

hive> select 41 % 5 from dual;
1
hive> select 8.4 % 4 from dual;
0.40000000000000036

hive> select round(8.4 % 4 , 2) from dual;
0.4

位与操作: &

hive> select 4 & 8 from dual;
0
hive> select 6 & 4 from dual;
4

位或操作: |

hive> select 4 | 8 from dual;
12
hive> select 6 | 8 from dual;
14

位异或操作: ^

hive> select 4 ^ 8 from dual;
12
hive> select 6 ^ 4 from dual;
2

位取反操作: ~

hive> select ~6 from dual;
-7
hive> select ~4 from dual;
-5

逻辑运算

逻辑与操作: AND

hive> select 1 from dual where 1=1 and 2=2;
1

逻辑或操作: OR

hive> select 1 from dual where 1=2 or 2=2;
1

逻辑非操作: NOT

hive> select 1 from dual where not 1=2;
1

复合类型构建操作

Map类型构建: map

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

Struct类型构建: struct

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

array类型构建: array

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

复杂类型访问操作

map类型访问: M[key]

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

struct类型访问: S.x

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

array类型访问: A[n]

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

数值计算

取整函数: round

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

指定精度取整函数: round

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

向下取整函数: floor

hive> select floor(3.1415926) from dual;
3
hive> select floor(25) from dual;
25

向上取整函数: ceil

hive> select ceil(3.1415926) from dual;
4
hive> select ceil(46) from dual;
46

向上取整函数: ceiling

hive> select ceiling(3.1415926) from dual;
4
hive> select ceiling(46) from dual;
46

取随机数函数: rand

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

自然指数函数: exp

hive> select exp(2) from dual;
7.38905609893065

自然对数函数: ln

hive> select ln(7.38905609893065) from dual;
2.0

以10为底对数函数: log10

hive> select log10(100) from dual;
2.0

以2为底对数函数: log2

hive> select log2(8) from dual;
3.0

对数函数: log

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

幂运算函数: pow

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

幂运算函数: power

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

开平方函数: sqrt

hive> select sqrt(16) from dual;
4.0

二进制函数: bin

hive> select bin(7) from dual;
111

十六进制函数: hex

hive> select hex(17) from dual;
11
hive> select hex('abc') from dual;
616263

反转十六进制函数: unhex

hive> select unhex('616263') from dual;
abc
hive> select unhex('11') from dual;
-
hive> select unhex(616263) from dual;
abc

进制转换函数: conv

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

绝对值函数: abs

hive> select abs(-3.9) from dual;
3.9
hive> select abs(10.9) from dual;
10.9

正取余函数: pmod

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

正弦函数: sin

hive> select sin(0.8) from dual;
0.7173560908995228

反正弦函数: asin

hive> select asin(0.7173560908995228) from dual;
0.8

余弦函数: cos

hive> select cos(0.9) from dual;
0.6216099682706644

反余弦函数: acos

hive> select acos(0.6216099682706644) from dual;
0.9

positive函数: positive

hive> select positive(-10) from dual;
-10
hive> select positive(12) from dual;
12

negative函数: negative

hive> select negative(-5) from dual;
5
hive> select negative(8) from dual;
-8

复杂类型长度统计函数

Map类型长度函数: size(Map

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

array类型长度函数: size(Array)

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

类型转换函数

类型转换函数: cast

hive> select cast(1 as bigint) from dual;
1

日期函数

UNIX时间戳转日期函数: from_unixtime

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

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

hive> select unix_timestamp() from dual;
1323309615

日期转UNIX时间戳函数: unix_timestamp

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

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

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

日期时间转日期函数: to_date

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

日期转年函数: year

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

日期转月函数: month

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

日期转天函数: day

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

日期转小时函数: hour

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

日期转分钟函数: minute

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

日期转秒函数: second

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

日期转周函数: weekofyear

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

日期比较函数: datediff

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

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

日期减少函数: date_sub

hive> select date_sub('2012-12-08',10) from dual;
2012-11-2

条件函数

If函数: if

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

非空查找函数: COALESCE

hive> select COALESCE(null,'100','50') from dual;
100

条件判断函数1：CASE

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

条件判断函数2：CASE

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

字符串函数

字符串长度函数：length

hive> select length('abcedfg') from dual;
7

字符串反转函数：reverse

hive> select reverse('abcedfg') from dual;
gfdecba

字符串连接函数：concat

hive> select concat('abc','def','gh') from dual;
abcdefgh

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

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

字符串截取函数1：substr, substring

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

字符串截取函数2：substr, substring

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

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

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

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

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

去空格函数：trim

hive> select trim(' abc ') from dual;
abc

左边去空格函数：ltrim

hive> select ltrim(' abc ') from dual;
abc

右边去空格函数：rtrim

hive> select rtrim(' abc ') from dual;
abc

正则表达式替换函数：regexp_replace

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

正则表达式解析函数：regexp_extract

hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 1) from dual;
the
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 2) from dual;
bar
hive> select regexp_extract('foothebar', 'foo(.*?)(bar)', 0) from dual;
foothebar

URL解析函数：parse_url

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

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 dual;
amy

空格字符串函数：space

hive> select space(10) from dual;
hive> select length(space(10)) from dual;
10

重复字符串函数：repeat

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

首字符ascii函数：ascii

hive> select ascii('abcde') from dual;
97

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

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

分割字符串函数: split

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

集合查找函数: find_in_set

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

集合统计函数

个数统计函数: count

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

总和统计函数: sum

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

平均值统计函数: avg

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

最小值统计函数: min

hive> select min(t) from udftest;
20

最大值统计函数: max

hive> select max(t) from udftest;
120

中位数函数: percentile

select percentile(score,<0.2,0.4>) from udftest； 取0.2，0.4位置的数据

直方图: histogram_numeric

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

聚合函数：collect_set

hive> select collect_set(col) from dual;


聚合函数2:collect_list

hive> select collect_list(col) from dual;


参考链接

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

05-18 1092

11-10 303

11-11 1055

01-19 350

03-08 959

12-01 196

04-26 2097

01-18 371

10-10 221

04-25 3642