1. 程式人生 > >hive學習(三) hive的分割槽

hive學習(三) hive的分割槽

1.Hive 分割槽partition

必須在表定義時指定對應的partition欄位 a、單分割槽建表語句:
create table day_table (id int, content string) partitioned by (dt string);
單分割槽表,按天分割槽,在表結構中存在id,content,dt三列。 以dt為資料夾區分 b、 雙分割槽建表語句:
create table day_hour_table (id int, content string) partitioned by (dt string, hour string);
雙分割槽表,按天和小時分割槽,在表結構中新增加了dt和hour兩列。 先以dt為資料夾,再以hour子資料夾區分  

2.建立2個表psn2 psn3

create table psn2 (
id int,
name string,
hobby array<string>,
address map<string,string>
)
partitioned by (age int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED 
BY '-' MAP KEYS TERMINATED BY ':' LINES TERMINATED BY '\n';

 

3.插入資料

load data local inpath '/root/data' into table psn2 partition (age=10);

4.查詢psn2

hive> select * from psn2;
OK
1    小明1    ["lol","book","movie"]    {"beijing":"changping","shanghai":"pudong"}    10
2 小明2 ["lol","book","movie"] {"beijing":"changping","shanghai":"pudong"} 10 3 小明3 ["lol","book","movie"] {"beijing":"changping","shanghai":"pudong"} 10 4 小明4 ["lol","book","movie"] {"beijing":"changping","shanghai":"pudong"} 10 5 小明5 ["lol","movie"] {"beijing":"changping","shanghai":"pudong"} 10 6 小明6 ["lol","book","movie"] {"beijing":"changping","shanghai":"pudong"} 10 7 小明7 ["lol","book"] {"beijing":"changping","shanghai":"pudong"} 10 8 小明8 ["lol","book"] {"beijing":"changping","shanghai":"pudong"} 10 9 小明9 ["lol","book","movie"] {"beijing":"changping","shanghai":"pudong"} 10 Time taken: 7.93 seconds, Fetched: 9 row(s)
理論上分割槽可以無限分,但是實際需要根據需求來分割槽。 如:歷史資料按天分割槽  

5.錯誤例項psn3

create table psn3 (
id int,
name string,
age int,
hobby array<string>,
address map<string,string>
)
partitioned by (age int)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '-'
MAP KEYS TERMINATED BY ':'
LINES TERMINATED BY '\n';
報錯提示:FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns 原因:分割槽欄位不能再表的列中  

6.同時建立兩個分割槽

create table psn3 (
id int,
name string,
hobby array<string>,
address map<string,string>
)
partitioned by (age int,sex string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '-'
MAP KEYS TERMINATED BY ':