hive中複雜的資料型別array與map

2021-09-27 01:42:02 字數 2398 閱讀 1779

//建立表

create table if not exists arr2(

name string,

score array)

row format delimited fields terminated by '\t' //字段之間的分隔符

collection items terminated by ',' //array之間的分隔符

;//資料

zhangsan 78,89,92,96

lisi 67,75,83,94

//載入資料

load data local inpath '/root/test/arraydata' into arr2;

//查詢資料

select name,score[1] from arr2 where size(score) > 3;

陣列的某個值查詢直接欄位名[index]

//將陣列的資料拆分開並插入到arr_temp表

create table arr_temp

asselect name,cj from arr2 lateral view explode(score) score as cj;

拆分後的結果為:

//統計某個學生的總成績

select name,sum(cj) as totalscore

from arr2 lateral view explode(score) score as cj group by name;

//將拆分的資料合併並寫到arr_temp2表

create table arr_temp2

asselect name,collect_set(cj) from arr_temp group by name;

合併後的結果為:

//建立表

create table if not exists map2(

name string,

score map)

row format delimited fields terminated by ' '

collection items terminated by ','

map keys terminated by ':'

;//資料

zhangsan chinese:90,math:87,english:63,nature:76

lisi chinese:60,math:30,english:78,nature:0

wangwu chinese:89,math:25,english:81,nature:9

load data local inpath '/root/test/mapdata' into map2;

//查詢

查詢數學大於35分的學生的英語和自然成績:

select

m.name,

m.score['english'] ,

m.score['nature']

from map2 m

where m.score['math'] > 35

;

map的某個值查詢使用欄位名[key]

//拆分資料

//拆分資料並插入到map2_temp表中

create table map2_temp

asselect name,m_class,m_score from map2

lateral view explode(score) score as m_class,m_score;

結果為:

//合併拆分的資料

select name,collect_set(concat_ws(":",m_class,cast(m_score as string)))

from map2_temp group by name;

hive中對複雜資料型別的支援

hive提供了復合資料型別 struct struct內部的資料可以通過dot 來訪問,例如,表中一列c的型別為struct,可以通過c.a來訪問域a map key value對 訪問指定域可以通過 指定網域名稱稱 進行,例如,乙個map m包含了乙個group gid的kv對,gid的值可以通過...

hive學習 hive中的資料型別

1 基本型別 資料型別 所佔位元組 開始支援版本 tinyint 1byte,128 127 smallint 2byte,32,768 32,767 int4byte,2,147,483,648 2,147,483,647 bigint 8byte,9,223,372,036,854,775,80...

hive複雜資料型別 a29

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