1.查看系统自带的函数
hive> show functions;
2.显示自带的函数的用法
hive> desc function upper;
3.详细显示自带的函数的用法
hive> desc function extended upper;
hive> select round(3.1415926) from tableName;
3
hive> select round(3.5) from tableName;
4
hive> create table tableName as select round(9542.158) from tableName;
hive> select round(3.1415926, 4) from tableName;
3.1416
hive> select floor(3.1415926) from tableName;
3
hive> select floor(25) from tableName;
25
hive> select ceil(3.1415926) from tableName;
4
hive> select ceil(46) from tableName;
46
hive> select ceiling(3.1415926) from tableName;
4
hive> select ceiling(46) from tableName;
46
hive> select rand() from tableName;
0.5577432776034763
hive> select rand() from tableName;
0.6638336467363424
hive> select rand(100) from tableName;
0.7220096548596434
hive> select rand(100) from tableName;
0.7220096548596434
hive> select from_unixtime(1323308943, ‘yyyyMMdd‘);
20111208
hive> select unix_timestamp() ;
1323309615
hive> select unix_timestamp(‘2021-4-30 13:01:03‘) ;
1323234063
hive> select unix_timestamp(‘20111207 13:01:03‘,‘yyyyMMdd HH:mm:ss‘);
1323234063
hive> select to_date(‘2011-12-08 10:03:01‘);
2011-12-08
hive> select year(‘2011-12-08 10:03:01‘) ;
2011
hive> select year(‘2021-12-08‘);
2012
hive> select month(‘2011-12-08 10:03:01‘) ;
12
hive> select month(‘2011-08-08‘);
8
hive> select day(‘2011-12-08 10:03:01‘) ;
8
hive> select day(‘2011-12-24‘);
24
hive> select hour(‘2011-12-08 10:03:01‘) ;
10
hive> select minute(‘2011-12-08 10:03:01‘) ;
3
-- second 返回秒
hive> select second(‘2011-12-08 10:03:01‘) ;
1
hive> select weekofyear(‘2021-04-08 10:03:01‘) ;
49
hive> select datediff(‘2012-12-08‘,‘2012-05-09‘) ;
213
hive> select date_add(‘2012-12-08‘,10) ;
2012-12-18
hive> select date_sub(‘2012-12-18‘,10) ;
2012-11-28
hive> select if(1=2,100,200);
200
hive> select if(1=1,100,200);
100
hive> select COALESCE(null,‘100‘,‘50‘) ;
100
hive> select case 100 when 50 then ‘tom‘ when 100 then ‘mary‘ else ‘tim‘ end ;
mary
hive> Select case 200 when 50 then ‘tom‘ when 100 then ‘mary‘ else ‘tim‘ end ;
tim
hive> select case when 1=2 then ‘tom‘ when 2=2 then ‘mary‘ else ‘tim‘ end ;
mary
hive> select case when 1=1 then ‘tom‘ when 2=2 then ‘mary‘ else ‘tim‘ end;
tom
hive> select length(‘abcedfg‘) ;
hive> select reverse(‘abcdefg‘) ;
gfdecba
hive> select concat(‘abc‘,‘def‘,‘gh‘);
abcdefgh
hive> select concat_ws(‘,‘,‘abc‘,‘def‘,‘gh‘) ;
abc,def,gh
hive> select substr(‘abcdeere‘,3) ;
cde
hive> select substring(‘abcde‘,3);
cde
hive> select substr(‘abcde‘,-2) ; (负数从后往前)
e
hive> select substr(‘abcde‘,3,2) ;
cd
hive> select substring(‘abcde‘,3,2) ;
cd
hive>select substring(‘abcde‘,-3,2) ;
cd
hive> select upper(‘abSEd‘);
ABSED
hive> select ucase(‘abSEd‘);
ABSED
hive> select lower(‘abSEd‘) ;
absed
hive> select lcase(‘abSEd‘);
absed
hive> select trim(‘ ab c ‘);
ab c
hive> select parse_url
(‘https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1‘, ‘HOST‘)
;
www.tableName.com
hive> select parse_url
(‘https://www.tableName.com/path1/p.php?k1=v1&k2=v2#Ref1‘, ‘QUERY‘, ‘k1‘)
;
v1
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‘) ;

hive> select repeat(‘abc‘, 5) ;
abcabcabcabcabc
hive> select split(‘abtcdtef‘,‘t‘);
["ab","cd","ef"]
hive> select count(*) from tableName;
20
hive> select count(distinct t) from tableName;
10
hive> select sum(t) from tableName;
100
hive> select sum(distinct t) from tableName;
70
hive> select avg(t) from tableName;
50
hive> select avg (distinct t) from tableName;
30
hive> select min(t) from tableName;
20
hive> select max(t) from tableName;
120
-- 建表
create table score_map(name string, score map<string, int>)
row format delimited fields terminated by ‘\t‘
collection items terminated by ‘,‘
map keys terminated by ‘:‘;
-- 创建数据内容如下并加载数据
cd /bigdata/logs/hivedatas/
vi score_map.txt
zhangsan sx:80,yw:89,zz:95
lisi sx:60,yw:80,zz:99
-- 加载数据到hive表当中去
load data local inpath ‘/bigdata/logs/hivedatas/score_map.txt‘ overwrite into table score_map;
-- map结构数据访问:
-- 获取所有的value:
select name,map_values(score) from score_map;
-- 获取所有的key:
select name,map_keys(score) from score_map;
-- 按照key来进行获取value值
select name,score["sx"] from score_map;
-- 查看map元素个数
select name,size(score) from score_map;
-- 构建一个map
select map(1, ‘zs‘, 2, ‘lisi‘);

-- 创建struct表
hive> create table movie_score(name string, info struct<number:int,score:float>)
row format delimited fields terminated by "\t"
collection items terminated by ":";
-- 加载数据
cd /bigdata/logs/hivedatas/
vi struct.txt
-- 电影ABC,有1254人评价过,打分为7.4分
ABC 1254:7.4
DEF 256:4.9
XYZ 456:5.4
-- 加载数据
load data local inpath ‘/bigdata/logs/hivedatas/struct.txt‘ overwrite into table movie_score;
-- hive当中查询数据
hive> select * from movie_score;
hive> select name, info.number, info.score from movie_score;
OK
1254 7.4
256 4.9
456 5.4
-- 构建一个struct
select struct(1, ‘anzhulababy‘, ‘moon‘, 1.68);

hive> create table person(name string, work_locations array<string>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t‘
COLLECTION ITEMS TERMINATED BY ‘,‘;
-- 加载数据到person表当中去
cd /bigdata/logs/hivedatas/
vim person.txt
-- 数据内容格式如下
biansutao beijing,shanghai,tianjin,hangzhou
linan changchun,chengdu,wuhan
-- 加载数据
hive > load data local inpath ‘/bigdata/logs/hivedatas/person.txt‘ overwrite into table person;
-- 查询所有数据数据
hive > select * from person;
-- 按照下标索引进行查询
hive > select work_locations[0] from person;
-- 查询所有集合数据
hive > select work_locations from person;
-- 查询元素个数
hive > select size(work_locations) from person;
-- 构建array
select array(1, 2, 1);
select array(1, ‘a‘, 1.0);
select array(1, 2, 1.0);
hive> select size(map(1, ‘zs‘, 2, ‘anzhulababy‘));
2
hive> select size(t) from arr_table2;
4
hive> select cast(‘1‘ as bigint) ;
1
CONCAT(string A/col, string B/col…):返回输入字符串连接后的结果,支持任意个输入字符串;
CONCAT_WS(separator, str1, str2,...):它是一个特殊形式的 CONCAT()。
COLLECT_SET(col):函数只接受基本数据类型,它的主要作用是将某字段的值进行去重汇总,产生array类型字段。
| name | constellation | blood_type |
|---|---|---|
| 孙悟空 | 白羊座 | A |
| 老王 | 射手座 | A |
| 宋宋 | 白羊座 | B |
| 猪八戒 | 白羊座 | A |
| 按住啦baby | 射手座 | A |
射手座,A 老王|按住啦baby
白羊座,A 孙悟空|猪八戒
白羊座,B 宋宋
cd /bigdata/logs/hivedatas
vim constellation.txt
孙悟空 白羊座 A
老王 射手座 A
宋宋 白羊座 B
猪八戒 白羊座 A
凤姐 射手座 A
hive (hive_explode)> create table person_info(name string, constellation string, blood_type string) row format delimited fields terminated by "\t";
hive (hive_explode)> load data local inpath ‘/bigdata/logs/hivedatas/constellation.txt‘ into table person_info;
hive (hive_explode)> select t1.base, concat_ws(‘|‘, collect_set(t1.name)) name
from
(select name, concat(constellation, "," , blood_type) base from person_info) t1
group by t1.base;
EXPLODE(col):将hive一列中复杂的array或者map结构拆分成多行。
LATERAL VIEW
cd /bigdata/logs/hivedatas
vim movie.txt
《疑犯追踪》 悬疑,动作,科幻,剧情
《Lie to me》 悬疑,警匪,动作,心理,剧情
《战狼2》 战争,动作,灾难
《疑犯追踪》 悬疑
《疑犯追踪》 动作
《疑犯追踪》 科幻
《疑犯追踪》 剧情
《Lie to me》 悬疑
《Lie to me》 警匪
《Lie to me》 动作
《Lie to me》 心理
《Lie to me》 剧情
《战狼2》 战争
《战狼2》 动作
《战狼2》 灾难
hive (hive_explode)> create table movie_info(movie string, category array<string>)
row format delimited fields terminated by "\t"
collection items terminated by ",";
load data local inpath "/bigdata/logs/hivedatas/movie.txt" into table movie_info;
hive (hive_explode)> select movie, category_name from movie_info
lateral view explode(category) table_tmp as category_name;
原文:https://www.cnblogs.com/tenic/p/14762526.html