hive分割槽表的建立+外部表
阿新 • • 發佈:2019-01-03
hive表的建立:
1.外部表+分割槽表
create externaltable hive_2018(
id string,
akc194 string
)
partitioned by(cysj string)
row format delimited fields terminated by '|' lines terminated by '\n' stored as textfile;
load data local inpath '/root/partition.txt' into table hive_2018 partition(cysj='2017');
備註:1分割槽欄位不能和表已有欄位重複
2.建立外部分割槽表,不能直接使用location,需要單獨在使用load指令。
---外部表
create external table fz_external_table(id int,name string,age int,tel string)
row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile
location '/user/hive/external/fz_external_table';
分割槽表和內部表的區別:
1、在匯入資料到外部表,資料並沒有移動到自己的資料倉庫目錄下(如果指定了location的話),也就是說外部表中的資料並不是由它自己來管理的!而內部表則不一樣;
2、在刪除內部表的時候,Hive將會把屬於表的元資料和資料全部刪掉;而刪除外部表的時候,Hive僅僅刪除外部表的元資料,資料是不會刪除的!
分割槽表的備註:
分割槽表建立後,直接將檔案放入分割槽表的檔案目錄,分割槽表是不會自動有資料的,還需要對新增分割槽資訊後分區表才可以讀取到資料。在hive中使用指令:msck repair table xxx;(修復分割槽表)或者
alter table xxx add partition(month=“201801”)//新增分割槽資訊