1. 程式人生 > >hive複雜格式array,map,struct使用

hive複雜格式array,map,struct使用

 

-- 建立資料庫表,以array作為資料型別
drop table if exists person;
create table person(
     name string
    ,work_locations array<string>
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ' '
COLLECTION ITEMS TERMINATED BY ','
stored as textfile
;

-- 資料
biansutao beijing,shanghai,tianjin,hangzhou
linan changchu,chengdu,wuhan

-- 入庫資料 LOAD DATA LOCAL INPATH 'tmpa' OVERWRITE INTO TABLE person; -- 查詢 select * from person; select name from person; select work_locations[0] from person; select work_locations from person; select work_locations[3] from person; select work_locations[4] from person;

 

 

-- 建立資料庫表
create table score( name string ,score map<string,int> ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' COLLECTION ITEMS TERMINATED BY ',' MAP KEYS TERMINATED BY ':' stored as textfile ; -- 資料 biansutao 數學:80,語文:89,英語:95 jobs 語文:60,數學:80,英語:99 -- 入庫資料 LOAD DATA LOCAL INPATH 'tmpb' OVERWRITE INTO
TABLE score; -- 查詢 select * from score; select name from score; select t.score from score t; select t.score['語文'] from score t; select t.score['英語'] from score t;

 

-- 建立資料表
CREATE TABLE test(
     id int
    ,course struct<course:string,score:int>
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ' '
COLLECTION ITEMS TERMINATED BY ','
stored as textfile
;

-- 資料
1 english,80
2 math,89
3 chinese,95

-- 入庫
LOAD DATA LOCAL INPATH 'tmpc' OVERWRITE INTO TABLE test;

-- 查詢
select * from test;
select course from test;
select t.course.course from test t; 
select t.course.score from test t;

 

-- 建立資料表 (異常)
create table test1(
     id int
    ,a MAP<STRING,ARRAY<STRING>>
)
row format delimited fields terminated by ' '
collection items terminated by ','
MAP KEYS TERMINATED BY ':'
stored as textfile
;

-- 資料
1 english:80,90,70
2 math:89,78,86
3 chinese:99,100,82

-- 入庫
LOAD DATA LOCAL INPATH 'tmpd' OVERWRITE INTO TABLE test1;

-- 查詢

 

 

ref: https://blog.csdn.net/u010670689/article/details/72885944