1. 建立分區表 create table
單分區表:其中分區字段是partdate,注意分區字段不能和表字段一樣,否則會報重復的錯
create table test_t2(words string,frequency string) partitioned by (partdate string) row format delimited fields terminated by '\1';
多分區表:id在前,address在后,注意多個分區字段時,是有先后順序的
create table test_table_hive(name string,school string) partitioned by (id int,address string) row format delimited fields terminated by '\1';
2. 新建分區:建立分區表之后,此時沒有數據,也沒有分區,需要建立分區
查看分區命令show partitions:
show partitions test_table_hive;
建立單分區 alter table:
alter table test_t2 add partition(partdate='20191030'); alter table test_t2 add partition(partdate=20191030); #也可,可能是內部轉換了格式 alter table test_t2 add IF NOT EXISTS partition (partdate='20191031'); #避免重復建立分區出錯 alter table test_t2 add IF NOT EXISTS partition (partdate='20191031') partition (partdate='20191101'); #一次建立多個分區
建立多字段分區:
alter table test_table_hive add partition(id=1,adress='chengdu'); #顯示分區 show partitions test_table_hive;

一次建立多個多字段分區,注意多字段時,必須填滿這多個分區字段,例如此時就不能只用 id 來分區:
alter table test_table_hive add partition(id=2,adress='beijing') partition(id=3,adress='shanghai'); show partitions test_table_hive;


3. 刪除分區 drop
alter table test_t2 drop partition(partdate='20191030');
附加:顯示當前數據庫
select current_database();
參考:
