任务描述
本关任务:根据相关知识内容实现 Hive 外部分区表的操作。
相关知识
为了完成本关任务,你需要掌握: 1.外部分区表的创建 2.数据加载 3.查询分区表
内外部分区表的区别
Hive 创建内部表时(默认创建内部表),会将数据移动到数据仓库指向的路径;创建外部表(需要加关键字EXTERNAL),仅记录数据所在的路径,不对数据的位置做任何改变。
在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。
创建外部分区表
通过EXTERNAL关键字指定为外部表,PARTITIONED BY子句指定为分区。
注意:分区字段不能是表中已经存在的数据,可以将分区字段看作表的伪列。
测试说明
平台会对你编写的代码进行测试:
创作不易,如果能解决您的问题,麻烦您点赞、收藏+关注,一键三连!!!
代码如下:
---创建mydb数据库
create database if not exists mydb;
---使用mydb数据库
use mydb;
---------- Begin ----------
---创建staff外部分区表
create external table if not exists staff(
id int,
name string,
age int
)
partitioned by (department string)
row format delimited fields terminated by ','
stored as textfile;
---将不同部门员工的数据加载到相应的部门分区中:/root/department/
load data local inpath '/root/department/operations.txt' into table staff partition(department='operations');
load data local inpath '/root/department/development.txt' into table staff partition(department='development');
load data local inpath '/root/department/sales.txt' into table staff partition(department='sales');
---查询staff表中sales和operations部门的员工数据
select * from staff where department='sales' or department='operations';
---查看staff表简要结构
desc staff;
---------- End ----------
---删除student表
drop table mydb.staff;