Hive应用:外部分区表

介绍

Hive可以创建外部分区表。创建表的时候,分区要在建表语句中体现。建完之后,你不会在表中看到数据,需要进行分区添加,使用alter语句进行添加。然后数据才会显示。

样例

有如下的目录结构。

![在这里插入图片描述](https://img-blog.csdn.net/20180926220933524?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2E5MzcyMTk0NjI=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

建表语句:

小编整理了一些大数据开发的系统性的学习资料需要的小伙伴可以加群:862879153,免费领取学习资料和大牛一起学习大数据。

create external table Tbl_Custom(CustomID int,AreaID int,Name string,Gender int) partitioned by(city string) row format delimited fields terminated by '\t' location 'hdfs://hadoop01:9000/data/tbl_custom';

创建表的时候,只创建到tbl_custom这一层目录,余下的一层目录使用分区表示,如果余下的有两层目录,那么可以使用两个分区,目录层级以此类推。将这个外部表创建好之后,使用查询语句,是看不到数据的,需要给这个表添加分区内容,才能看到具体的信息,如下:

alter table Tbl_Custom add partition(city='beijing') location 'hdfs://hadoop01:9000/data/tbl_custom/city=beijing';

alter table Tbl_Custom add partition(city='shanghai') location 'hdfs://hadoop01:9000/data/tbl_custom/city=shanghai';

当添加好这两个分区之后,这两个目录下的数据就可以在一张表中查看了,这个方法很适用于合并数据。


分享到:


相關文章: