創建分區表
create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';
加載數據到分區表中
load data local inpath '/opt/module/datas/dept.txt' into table dept_partition partition(month='201709');
查詢分區表中數據
select * from dept_partition where month='201709';
多表聯合查詢
select * from dept_partition where month='201709'
union
select * from dept_partition where month='201708'
union
select * from dept_partition where month='201707';
增加分區
alter table dept_partition add partition(month='201706');
同時增加多個分區
alter table dept_partition add partition(month='201705') partition(month='201704');
刪除分區
alter table dept_partition drop partition(month='201704');
同時刪除多個分區
alter table dept_partition drop partition(month='201705'),partition (month='201706');
查看分區表有多少分區
show partitions dept_partition;
創建二級分區表
create table dept_partition2(
deptno int,dname string,loc string
)
partitioned by (month string,day string)
row format delimited fields terminated by '\t';
加載數據到二級分區
load data local inpath '/opt/module/data/text' into table dept_partition2 partition(month='201709',day='13');
查詢分區數據
select * from dept_partition2 where month='201709' and day='13';
分區表和數據管關聯
如果首先用hadoop fs -mkdri 創建分區文件夾,然后-put文件,不會查詢到,原因是因為mysql上沒有元數據信息,alter添加分區和load data會自動創建元數據
不一定是分區表,普通的表也同理
hadoop fs -mkdir /user/hive/warehouse/stu3
hadoop fs -put /opt/data/test /user/hive/warehouse/stu3
這樣查詢表不會查詢到數據,而且表也沒有,因為在Mysql上沒有元數據信息
只需要重新建立這張表就好
create table if not exists(
id int);
這就會將元數據與現在的文件夾與數據相關聯
1、上傳數據后修復
msck repair table dept_pattition2;
2、上傳數據后添加分區
# 相當於上面的建表操作,都是為了在Mysql上有元數據
alter table dept_pattition2 add partition(month='201709',day='11');
3、創建文件夾后load數據到分區
# load data會自動關聯元數據
load data local inpath '/opt/module/data/test' into table dept_partition2 partition(month='201709',day='10');