1. 程式人生 > >hive內建函式和自定義函式的使用

hive內建函式和自定義函式的使用

1.hive函式的分類

內建函式和自定義函式

1.1、內建函式



1、查詢有哪些內建函式: 
show functions;
2、查詢某個內建函式怎麼使用
desc function extended concat;


1.2、自定義函式



分三大類:
1、UDF : user define function : 單行普通函式 (一進一出 : 進一行, 出一行)
substring(string,0,3)


2、UDAF :user define aggregate function : 使用者自定義聚集函式(多進一出:多換輸入,單行輸出)
max, count, min, avg, sum.....  group(id)


3、UDTF : user define table_tranform function : 表格生成函式 : (一進多出)
explode(array)

explode(map)

2.內建函式get_json_object的使用案例

需要藉助一張臨時表 主要用來處理json資料的 json資料格式如下 {"movie":"1193","rate":"5","timeStamp":"978300760","uid":"1"}
說明:key _value 形式的  movie =1193  rate=5  timeStamp=978300760  UID=1    資料的準備:rating.json  有63M的檔案 // 建立一張表儲存json檔案
create table json_table (jsonline string);
// 載入資料
load data local inpath "/home/hadoop/rating.json" into table json_table; select * from json_table limit 5;

// 通過內建函式get_json_object解析json資料
select get_json_object(jsonline, '$.movie') as movie from json_table limit 5;




建立一張hive表有這四個欄位:
create table movierate(movie int, rate int, ts bigint, uid int) row format delimited fields terminated by "\t";




insert into table movierate 
select get_json_object(jsonline, '$.movie') as movie,
get_json_object(jsonline, '$.rate') as rate,
get_json_object(jsonline, '$.timeStamp') as ts,
get_json_object(jsonline, '$.uid') as uid 
from json_table;
select * from  movierate  limit 5;

3.自定義函式UDTF中的explode的使用案例

explode使用場景: huangboa,1:b,2:c,3
xuzhenga,4:b,5:c,6
wangbaoqianga,7:b,8:c,9


huangboa 1
huangbob 2
huangboc 3
xuzhegna 4
xuzhengb 5
. . . create table mapmap(name string, info map<string, int>)  row format delimited fields terminated by "\t" collection items terminated by ":" map keys terminated by ",";
load data local inpath "/home/hadoop/mapmap.txt" into table mapmap;
select * from mapmap;

select m.name, tf.key, tf.value from mapmap m lateral view explode(m.info) tf as key,value;