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';

當添加好這兩個分區之後,這兩個目錄下的數據就可以在一張表中查看了,這個方法很適用於合併數據。


分享到:


相關文章: