HIVE 第八章 UDF
Posted 麦田里的守望者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HIVE 第八章 UDF相关的知识,希望对你有一定的参考价值。
公告:更新任在持续,更多精彩内容敬请关注!
dual表的概念来自oracle,数据库建立时即与数据字典一起初始化,该表只有一个varchar2类型名为dummy的字段,表数据只有一行“X”,用来查询一些系统信息,如select sysdate from dual; select user from dual;select seq.nextval from dual等。为了能在hive中测试一些时间、数学、聚合函数,可以仿照oracle创建dual表。
[root@hive ~]# echo X > dual.txt
hive>create dual (dummy String);
hive>load data local inpath '/root/dual.txt' overwrite into table dual;
Hive内部自定义函数UDF
等值比较: =,> ,<,>=,<=,<>,!=,is [not] null
hive> select 1 from dual where 1=1;
1
模糊搜索 like
hive> select 1 from dual where 'football' like '%foot%';
OK
1
hive> select 1 from dual where 'football' like '%foot____';
OK
1
注意:符”_”表示任意单个字符,而字符”%”表示任意数量的字符。
正则搜索RLIKE 或者 REGEXP
hive> select 1 from dual where 'football' rlike '^f.*l$';
OK
1
hive> select 1 from dual where 'footbar' regexp '^f.*r$';
OK
1
数学运算符 +, -,*,/ ,%
hive> select 10/3 from dual;
OK
3.3333333333333335
位操作 &,|,~,^
hive> select 1|2 from dual;
OK
3
Time taken: 0.086 seconds, Fetched: 1 row(s)
hive> select 1&2 from dual;
OK
0
Time taken: 0.1 seconds, Fetched: 1 row(s)
hive> select 1^2 from dual;
OK
3
Time taken: 0.081 seconds, Fetched: 1 row(s)
hive> select ~1 from dual;
OK
-2
布尔表达式 and ,or, not
hive> select 1 from dual where 1 =2 or 3=3;
OK
1
hive> select 1 from dual where 1 =2 and 3=3;
OK
hive> select 1 from dual where not 3=3;
OK
hive> select 1 from dual where not 3=2;
OK
1
复合类型构建操作
① map类型
hive> create table udftest as select map('100','tom','200','mary') as t from dual;
hive> desc udftest;
OK
t map<string,string>
hive> select t from udftest;
OK
{"100":"tom","200":"mary"}
hive> select t[100] from udftest;
OK
tom
hive> select t[200] from udftest;
OK
mary
② struct 类型
hive> drop table udftest;
hive>create table udftest as select struct('tom','mary','tim') as t from dual;
hive> desc udftest;
OK
t struct<col1:string,col2:string,col3:string>
hive> select t.col1,t.col2 ,t.col3 from udftest;
OK
tom mary tim
hive> select t from udftest;
OK
{"col1":"tom","col2":"mary","col3":"tim"}
③ array 类型
hive> drop table udftest;
hive> create table udftest as select array('jiangzz','wangwu','lisi') as t from dual;
hive> describe udftest;
OK
t array<string>
hive> select t[0],t[1],t[2] from udftest;
OK
jiangzz wangwu lisi
数值计算
round
hive> select round(3.1415926) from dual;
OK
3.0
hive> select round(3.1415926,3) from dual;
OK
3.142
floor、ceil|ceiling
hive> select floor(3.1415926) from dual;
OK
3
hive> select ceil(3.1415926) from dual;
OK
4
hive> select ceiling(3.1415926) from dual;
OK
4
rand
hive> select rand() from dual;
OK
0.8883078127190064
hive> select rand() from dual;
OK
0.7074664833607184
hive> select rand(100) from dual;
OK
0.7220096548596434
pow|power、sqrt
hive> select pow(10,2) from dual;
OK
100.0
hive> select power(10,2) from dual;
OK
100.0
hive> select sqrt(3) from dual;
OK
1.7320508075688772
bin
hive> select bin(2) from dual;
OK
10
Time taken: 0.116 seconds, Fetched: 1 row(s)
hive> select bin(8) from dual;
OK
1000
hex、unhex
hive> select hex(15) from dual;
OK
F
hive> select hex(16) from dual;
OK
10
hive> select hex('jiangzz') from dual;
OK
6A69616E677A7A
hive> select unhex('6A69616E677A7A') from dual;
OK
jiangzz
conv(BIGINT num, int from_base, int to_base)
hive> select conv(10,10,8) from dual;
OK
12
hive> select conv(10,2,10) from dual;
OK
2
abs
hive> select abs(-10) from dual;
OK
10
pmod
hive> select pmod(10,3) from dual;
OK
1
Time taken: 0.073 seconds, Fetched: 1 row(s)
hive> select pmod(10.5,3) from dual;
OK
1.5
Time taken: 0.064 seconds, Fetched: 1 row(s)
本期内容暂时更新到此,更多精彩内容请持续关注jiangzz_wy公众账号,如有任何疑问也可以添加小编jiangzz_wx微信。
以上是关于HIVE 第八章 UDF的主要内容,如果未能解决你的问题,请参考以下文章