23 大資料 hive複雜資料型別

2021-09-25 05:55:37 字數 1960 閱讀 2159

1、array

一組有序字段,欄位的型別必須相同,array(1,2)

陣列是一組具有相同型別和名稱的變數的集合,這些變數稱為陣列的元素,每個陣列的元素都有乙個編號,編號從0開始

create table t1(id int,name string,likes array) row format delimited fields terminated by 『,』 collection items terminated by 『-』 --集合項用』-』 分割 lines terminated by 『\n』;

資料:1,張三,足球-讀書

2,李四,足球-電影-汽車

檢視第乙個愛好:select likes[0] from t1;

檢視第乙個愛好為足球的:select * from t1 where likes[0]=『足球』;

檢視使用者最喜歡哪個愛好,直接count是不可用的

hive提供了兩個函式:

explode將陣列拆分成行,lateral view來實現聚合 select hobby,count(1) c1 from t1 lateral view explode(likes) likes as hobby group by hobby order by c1 desc;

2、map

array

一組無序的鍵值對,鍵的型別必須是原子的,值可以是任何型別。同乙個對映的鍵的型別必須相同,值的型別必須相同 map(『a』,1,『b』,2)

map是一組鍵值對元組集合,使用陣列表示,可訪問元素

create table test_map(id int,name string,deductons map) row format delimited fields terminated by 『,』 collection items terminated by 『-』 map keys terminated by 『;』 lines terminated by 『\n』;

資料:1,里奧,英語:四級-日語:**

2,馬力斯,英語:六級-日語:一級

檢視英語為四級的:select * from test_map where deductons[『英語』]=『四級』;

查詢日語:select deductons[『日語』] from test_map;

select explode(deductons) as (note_name,note_level) from test_map;

select id,name,note_name,note_level from test_map lateral view outer explode(deductons) deductons as note_name,note_level;

3、struct

一組命名的字段,欄位的型別可以不同。一種記錄型別,封裝了乙個命名的字段集合

通過』點』符號訪問元素內容

針對一組資料中有不同的資料型別,公升級版map

strunct一旦宣告好結果,其位置就不可以改變

create table test_struct(id int ,address structcity:string,province:string,area:string,code:string) row format delimited fields terminated by 『,』 collection items terminated by 『-』 map keys terminated by 『:』 lines terminated by 『\n』;

資料:1,大連-遼寧-東北部-116000

2,青島-山東-東部-211000

select * from test_struct where address.city=『大連』;

4、union

值的資料型別可以是多個被定義的資料型別中的任意乙個,這個值通過乙個整數(零索引)來標記其為聯合型別中的那個資料型別

複雜資料型別允許任意層次的巢狀。複雜資料型別宣告必須用尖括號指明其中資料型別的字段型別

hive複雜資料型別 a29

一 map struct array 這3種的用法 1 array的使用 建立資料庫表,以array作為資料型別 create table person name string,work locations array row format delimited fields terminated b...

Hive複雜資料型別之array

create table tablename colname array 基本型別 說明 下標從0開始,越界不報錯,以null代替測試資料 zhangsan 78,89 92,96 lisi 67,75 83,94 王五 23 12create table ifnot exists arr1 nam...

複雜資料型別

1 在c語言中,除了之前學到的基本資料型別 整型,浮點型,字元型 外,還有指標型別和構造型別 結構型,聯合型,列舉型 2 結構體型別,用於把不同型別的資料組合成乙個集合體,宣告格式 struct 結構名 例如 includestruct students void main 結構體的特點是 表示更豐...