alter table table1 drop partition(year=2019) 删除指定分区表
检索固定分区表,需加上分区字段
加载数据到分区表的两种方式
创建分区表"create external table tables(字段、...) partitioned by(字段)"
分区是为了缩小查询范围、减少查询时间、提升查询效率
内部表和外部表的区别
内部表:删除表,元数据被删掉,文件不存在
外部表:删除表,元数据被删掉,文件还存在
desc formatted table2 查看table2的相关信息
创建外部表语句“create external table tables()"
create table table1 ( id int, name string, interest array<string>, score map<string,string> ) partitioned by (year int) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':' stored as textfile; show partitions table1; --展示分区; alter table table1 add partition (year = 2019) location '/test' --添加分区
1、内部表:导入数据时,将数据移动到hive指定的目录文件中,删除表时,数据也会删除;
2、外部表:建表时添加关键字external,并指定位置,删除表时不会删除源数据。
create external table table2( id int,name string,interest array<string>, score map<string,string>) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':' location '/testtable'; desc formatted table2;
创建分区表
create external table table2( id int,name string,interest array<string>, score map<string,string>) partitioned by (year int) row format delimited fields terminated by ',' collection items terminated by '-' map keys terminated by ':' stored as textfile; load data local inpath 'root/testdata.txt' into table table3 partition(year=2018); show partitions tables; alter table table3 add partition(year=2019) location '/testtable'; show partitions table3; alter table table3 drop partition(year=2019);