1. 程式人生 > >Hive應用:外部分割槽表

Hive應用:外部分割槽表

介紹
Hive可以建立外部分割槽表。建立表的時候,分割槽要在建表語句中體現。建完之後,你不會在表中看到資料,需要進行分割槽新增,使用alter語句進行新增。然後資料才會顯示。
微信公眾號:大資料小世界
樣例
有如下的目錄結構。
在這裡插入圖片描述
建表語句:

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’;
當新增好這兩個分割槽之後,這兩個目錄下的資料就可以在一張表中查看了,這個方法很適用於合併資料。在這裡插入圖片描述