您好,登錄后才能下訂單哦!
本篇文章為大家展示了Hive中的復(fù)合數(shù)據(jù)結(jié)構(gòu)以及函數(shù)的用法說明是什么,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
目前 hive 支持的復(fù)合數(shù)據(jù)類型有以下幾種:
map
(key1, value1, key2, value2, ...) Creates a map with the given key/value pairs
struct
(val1, val2, val3, ...) Creates a struct with the given field values. Struct field names will be col1, col2, ...
named_struct
(name1, val1, name2, val2, ...) Creates a struct with the given field names and values. (as of Hive 0.8.0)
array
(val1, val2, ...) Creates an array with the given elements
create_union
(tag, val1, val2, ...) Creates a union type with the value that is being pointed to by the tag parameter
創(chuàng)建數(shù)據(jù)庫表,以array作為數(shù)據(jù)類型 create table person(name string,work_locations array<string>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ','; 數(shù)據(jù) biansutao beijing,shanghai,tianjin,hangzhou linan changchu,chengdu,wuhan 入庫數(shù)據(jù) LOAD DATA LOCAL INPATH '/home/hadoop/person.txt' OVERWRITE INTO TABLE person; 查詢 hive> select * from person; biansutao ["beijing","shanghai","tianjin","hangzhou"] linan ["changchu","chengdu","wuhan"] Time taken: 0.355 seconds hive> select name from person; linan biansutao Time taken: 12.397 seconds hive> select work_locations[0] from person; changchu beijing Time taken: 13.214 seconds hive> select work_locations from person; ["changchu","chengdu","wuhan"] ["beijing","shanghai","tianjin","hangzhou"] Time taken: 13.755 seconds hive> select work_locations[3] from person; NULL hangzhou Time taken: 12.722 seconds hive> select work_locations[4] from person; NULL NULL Time taken: 15.958 seconds
創(chuàng)建數(shù)據(jù)庫表 create table score(name string, score map<string,int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':'; 要入庫的數(shù)據(jù) biansutao '數(shù)學(xué)':80,'語文':89,'英語':95 jobs '語文':60,'數(shù)學(xué)':80,'英語':99 入庫數(shù)據(jù) LOAD DATA LOCAL INPATH '/home/hadoop/score.txt' OVERWRITE INTO TABLE score; 查詢 hive> select * from score; biansutao {"數(shù)學(xué)":80,"語文":89,"英語":95} jobs {"語文":60,"數(shù)學(xué)":80,"英語":99} Time taken: 0.665 seconds hive> select name from score; jobs biansutao Time taken: 19.778 seconds hive> select t.score from score t; {"語文":60,"數(shù)學(xué)":80,"英語":99} {"數(shù)學(xué)":80,"語文":89,"英語":95} Time taken: 19.353 seconds hive> select t.score['語文'] from score t; 60 89 Time taken: 13.054 seconds hive> select t.score['英語'] from score t; 99 95 Time taken: 13.769 seconds
創(chuàng)建數(shù)據(jù)表 CREATE TABLE test(id int,course struct<course:string,score:int>) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY ','; 數(shù)據(jù) 1 english,80 2 math,89 3 chinese,95 入庫 LOAD DATA LOCAL INPATH '/home/hadoop/test.txt' OVERWRITE INTO TABLE test; 查詢 hive> select * from test; OK 1 {"course":"english","score":80} 2 {"course":"math","score":89} 3 {"course":"chinese","score":95} Time taken: 0.275 seconds hive> select course from test; {"course":"english","score":80} {"course":"math","score":89} {"course":"chinese","score":95} Time taken: 44.968 seconds select t.course.course from test t; english math chinese Time taken: 15.827 seconds hive> select t.course.score from test t; 80 89 95 Time taken: 13.235 seconds
LOAD DATA LOCAL INPATH '/home/hadoop/test.txt' OVERWRITE INTO TABLE test; create table test1(id int,a MAP<STRING,ARRAY<STRING>>) row format delimited fields terminated by '\t' collection items terminated by ',' MAP KEYS TERMINATED BY ':'; 1 english:80,90,70 2 math:89,78,86 3 chinese:99,100,82 LOAD DATA LOCAL INPATH '/home/hadoop/test1.txt' OVERWRITE INTO TABLE test1;
常見的函數(shù)就不廢話了,和標(biāo)準(zhǔn)sql類似,下面我們要聊到的基本是HQL里面專有的函數(shù),
hive里面的函數(shù)大致分為如下幾種:Built-in、Misc.、UDF、UDTF、UDAF
我們就挑幾個(gè)標(biāo)準(zhǔn)SQL里沒有,但是在HIVE SQL在做統(tǒng)計(jì)分析常用到的來說吧。
這是內(nèi)置的對(duì)集合進(jìn)行操作的函數(shù),用法舉例:
create EXTERNAL table IF NOT EXISTS userInfo (id int,sex string, age int, name string, email string,sd string, ed string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' location '/hive/dw'; select * from userinfo where sex='male' and (id!=1 and id !=2 and id!=3 and id!=4 and id!=5) and age < 30; select * from (select * from userinfo where sex='male' and !array_contains(split('1,2,3,4,5',','),cast(id as string))) tb1 where tb1.age < 30;
其中建表所用的測(cè)試數(shù)據(jù)你可以用如下鏈接的腳本自動(dòng)生成:
http://my.oschina.net/leejun2005/blog/76631
測(cè)試數(shù)據(jù):
first {"store":{"fruit":[{"weight":8,"type":"apple"},{"weight":9,"type":"pear"}],"bicycle":{"price":19.951,"color":"red1"}},"email":"amy@only_for_json_udf_test.net","owner":"amy1"} third
first {"store":{"fruit":[{"weight":9,"type":"apple"},{"weight":91,"type":"pear"}],"bicycle":{"price":19.952,"color":"red2"}},"email":"amy@only_for_json_udf_test.net","owner":"amy2"} third
first {"store":{"fruit":[{"weight":10,"type":"apple"},{"weight":911,"type":"pear"}],"bicycle":{"price":19.953,"color":"red3"}},"email":"amy@only_for_json_udf_test.net","owner":"amy3"} third
create external table if not exists t_json(f1 string, f2 string, f3 string) row format delimited fields TERMINATED BY ' ' location '/test/json' select get_json_object(t_json.f2, '$.owner') from t_json; SELECT * from t_json where get_json_object(t_json.f2, '$.store.fruit[0].weight') = 9; SELECT get_json_object(t_json.f2, '$.non_exist_key') FROM t_json;
這里尤其要注意UDTF的問題,官方文檔有說明:
json_tuple
A new json_tuple() UDTF is introduced in hive 0.7. It takes a set of names (keys) and a JSON string, and returns a tuple of values using one function. This is much more efficient than calling GET_JSON_OBJECT to retrieve more than one key from a single JSON string. In any case where a single JSON string would be parsed more than once, your query will be more efficient if you parse it once, which is what JSON_TUPLE is for. As JSON_TUPLE is a UDTF, you will need to use the LATERAL VIEW syntax in order to achieve the same goal.
For example,
select a.timestamp, get_json_object(a.appevents, '$.eventid'), get_json_object(a.appenvets, '$.eventname') from log a;
should be changed to
select a.timestamp, b.* from log a lateral view json_tuple(a.appevent, 'eventid', 'eventname') b as f1, f2;
UDTF(User-Defined Table-Generating Functions) 用來解決 輸入一行輸出多行(On-to-many maping) 的需求。
通過Lateral view可以方便的將UDTF得到的行轉(zhuǎn)列的結(jié)果集合在一起提供服務(wù),因?yàn)橹苯釉赟ELECT使用UDTF會(huì)存在限制,即僅僅能包含單個(gè)字段,不光是多個(gè)UDTF,僅僅單個(gè)UDTF加上其他字段也是不可以,hive提示在UDTF中僅僅能有單一的表達(dá)式。如下:
hive> select my_test(“abcef:aa”) as qq,’abcd’ from sunwg01;
FAILED: Error in semantic analysis: Only a single expression in the SELECT clause is supported with UDTF’s
使用Lateral view可以實(shí)現(xiàn)上面的需求,Lateral view語法如下:
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (‘,’ columnAlias)*
fromClause: FROM baseTable (lateralView)*
hive> create table sunwg ( a array, b array )
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY ‘\t’
> COLLECTION ITEMS TERMINATED BY ‘,’;
OK
Time taken: 1.145 seconds
hive> load data local inpath ‘/home/hjl/sunwg/sunwg.txt’ overwrite into table sunwg;
Copying data from file:/home/hjl/sunwg/sunwg.txt
Loading data to table sunwg
OK
Time taken: 0.162 seconds
hive> select * from sunwg;
OK
[10,11] ["tom","mary"]
[20,21] ["kate","tim"]
Time taken: 0.069 seconds
hive>
> SELECT a, name
> FROM sunwg LATERAL VIEW explode(b) r1 AS name;
OK
[10,11] tom
[10,11] mary
[20,21] kate
[20,21] tim
Time taken: 8.497 seconds
hive> SELECT id, name
> FROM sunwg LATERAL VIEW explode(a) r1 AS id
> LATERAL VIEW explode(b) r2 AS name;
OK
10 tom
10 mary
11 tom
11 mary
20 kate
20 tim
21 kate
21 tim
Time taken: 9.687 seconds
測(cè)試數(shù)據(jù):
url1 http://facebook.com/path2/p.php?k1=v1&k2=v2#Ref1
url2 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-getjsonobject
url3 https://www.google.com.hk/#hl=zh-CN&newwindow=1&safe=strict&q=hive+translate+example&oq=hive+translate+example&gs_l=serp.3...10174.11861.6.12051.8.8.0.0.0.0.132.883.0j7.7.0...0.0...1c.1j4.8.serp.0B9C1T_n0Hs&bav=on.2,or.&bvm=bv.44770516,d.aGc&fp=e13e41a6b9dab3f6&biw=1241&bih=589
create external table if not exists t_url(f1 string, f2 string) row format delimited fields TERMINATED BY ' ' location '/test/url'; SELECT f1, b.* FROM t_url LATERAL VIEW parse_url_tuple(f2, 'HOST', 'PATH', 'QUERY', 'QUERY:k1') b as host, path, query, query_id;
結(jié)果:
url1 facebook.com /path2/p.php k1=v1&k2=v2 v1
url2 cwiki.apache.org /confluence/display/Hive/LanguageManual+UDF NULL NULL
url3 www.google.com.hk / NULL NULL
explode 是一個(gè) hive 內(nèi)置的表生成函數(shù):Built-in Table-Generating Functions (UDTF),主要是解決 1 to N 的問題,即它可以把一行輸入拆成多行,比如一個(gè) array 的每個(gè)元素拆成一行,作為一個(gè)虛表輸出。它有如下需要注意的地方:
Using the syntax "SELECT udtf(col) AS colAlias..." has a few limitations: No other expressions are allowed in SELECT SELECT pageid, explode(adid_list) AS myCol... is not supported UDTF's can't be nested SELECT explode(explode(adid_list)) AS myCol... is not supported GROUP BY / CLUSTER BY / DISTRIBUTE BY / SORT BY is not supported SELECT explode(adid_list) AS myCol ... GROUP BY myCol is not supported
從上面的原理與語法上可知,
select 列中不能 udtf 和其它非 udtf 列混用,
udtf 不能嵌套,
不支持 GROUP BY / CLUSTER BY / DISTRIBUTE BY / SORT BY
還有 select 中出現(xiàn)的 udtf 一定需要列別名,否則會(huì)報(bào)錯(cuò):
SELECT explode(myCol) AS myNewCol FROM myTable; SELECT explode(myMap) AS (myMapKey, myMapValue) FROM myMapTable; SELECT posexplode(myCol) AS pos, myNewCol FROM myTable;
lateral view 是Hive中提供給UDTF的conjunction,它可以解決UDTF不能添加額外的select列的問題。當(dāng)我們想對(duì)hive表中某一列進(jìn)行split之后,想對(duì)其轉(zhuǎn)換成1 to N的模式,即一行轉(zhuǎn)多列。hive不允許我們?cè)赨DTF函數(shù)之外,再添加其它select語句。
如下,我們想將登錄某個(gè)游戲的用戶id放在一個(gè)字段user_ids里,對(duì)每一行數(shù)據(jù)用UDTF后輸出多行。
select game_id, explode(split(user_ids,'\\[\\[\\[')) as user_id from login_game_log where dt='2014-05-15' ; FAILED: Error in semantic analysis: UDTF's are not supported outside the SELECT clause, nor nested in expressions。
提示語法分析錯(cuò)誤,UDTF不支持函數(shù)之外的select 語句,如果我們想支持怎么辦呢?接下來就是Lateral View 登場(chǎng)的時(shí)候了。
Lateral view 其實(shí)就是用來和像類似explode這種UDTF函數(shù)聯(lián)用的。lateral view 會(huì)將UDTF生成的結(jié)果放到一個(gè)虛擬表中,然后這個(gè)虛擬表(1 to N)會(huì)和輸入行即每個(gè)game_id進(jìn)行join 來達(dá)到連接UDTF外的select字段的目的(源表和拆分的虛表按行做行內(nèi) 1 join N 的直接連接),這也是為什么 LATERAL VIEW udtf(expression) 后面需要表別名和列別名的原因。
Lateral View Syntax
lateralView: LATERAL VIEW udtf(expression) tableAlias AS columnAlias (',' columnAlias)*
fromClause: FROM baseTable (lateralView)*
可以看出,可以在2個(gè)地方用Lateral view:
在udtf前面用
在from baseTable后面用
例如:
pageid adid_list
front_page [1, 2, 3]
contact_page [3, 4, 5]
SELECT pageid, adid FROM pageAds LATERAL VIEW explode(adid_list) adTable AS adid;
pageid adid
front_page 1
front_page 2
front_page 3
contact_page 3
contact_page 4
contact_page 5
From語句后可以跟多個(gè)Lateral View。
A FROM clause can have multiple LATERAL VIEW clauses. Subsequent LATERAL VIEWS can reference columns from any of the tables appearing to the left of the LATERAL VIEW.
給定數(shù)據(jù):
Array<int> col1 Array<string> col2
[1, 2] [a", "b", "c"]
[3, 4] [d", "e", "f"]
轉(zhuǎn)換目標(biāo):
想同時(shí)把第一列和第二列拆開,類似做笛卡爾乘積。
我們可以這樣寫:
SELECT myCol1, myCol2 FROM baseTable LATERAL VIEW explode(col1) myTable1 AS myCol1 LATERAL VIEW explode(col2) myTable2 AS myCol2;
還有一種情況,如果UDTF轉(zhuǎn)換的Array是空的怎么辦呢?
在Hive0.12里面會(huì)支持outer關(guān)鍵字,如果UDTF的結(jié)果是空,默認(rèn)會(huì)被忽略輸出。
如果加上outer關(guān)鍵字,則會(huì)像left outer join 一樣,還是會(huì)輸出select出的列,而UDTF的輸出結(jié)果是NULL。
Lateral View通常和UDTF一起出現(xiàn),為了解決UDTF不允許在select字段的問題。
Multiple Lateral View可以實(shí)現(xiàn)類似笛卡爾乘積。
Outer關(guān)鍵字可以把不輸出的UDTF的空結(jié)果,輸出成NULL,防止丟失數(shù)據(jù)。
上述內(nèi)容就是Hive中的復(fù)合數(shù)據(jù)結(jié)構(gòu)以及函數(shù)的用法說明是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。