Hive四種表類型

4種表類型

Hive的表就是在HDFS上新建了一個目錄,並制定了數據文件的分隔符,目錄下的文件便是數據來源,文件中的分隔符必須與建立表的分隔符一致工具

Hive有內部表、外部表、分區表、分桶表4種類型以知足不一樣的業務場景,簡化使用方式,提升生產速度。優化

內部表

說明:先使用命令建立表,而後在加載文件數據到表中編碼

命令:spa

create table table_name (column_name type,[column_name type]...) row format delimited fields terminated by '\t';orm

load data local inpath 'file_path' into table table_name;資源

使用場景:string

在新系統建設初期,對需求明確,先有表,系統上線,數據入表hash

其餘:it

刪除內部表時會將表的數據一併刪除io

drop table table_name;

能夠直接使用相關工具往表對應的HDFS文件目錄下上傳文件,Hive便可查詢出來

外部表

說明:數據文件已經存在,而後使用命令建立表並指定表的數據文件路徑

命令:

create external table (column_name type,[column_name type]...) table_name row format delimited fields terminated by '\t' location 'file_path';

使用場景:

系統已經上線,新需求來了,現有的表沒法支撐新需求,所以須要新建一張表

其餘:

刪除外部表時不會刪除它關聯的數據文件

drop table table_name;

分區表

說明:

根據業務編碼、日期、其餘類型等維度建立分區表,好比一個重慶市的9個區域各自一個分區,若是要查某一個區域的數據,只須要訪問一個分區的數據,而不須要從全量數據中進行篩選。

分區底層實現邏輯爲:

在一個表對應的目錄下,一個分區對應一個目錄

命令:

create table table_name (column_name type,[column_name type]...) partitioned by (column_name type) row format delimited ternimated by '\t';

select * from table_name where partition_column_name = value;

使用場景:

單表數據量巨大,並且查詢又常常限定某一個類別,那麼能夠將表按照該類別進行分區,以提升數據查詢效率,減小資源開銷

其餘:

直接在HDFS裏面建立分區目錄,HIVE是沒法識別的,由於MySQL中元數據不包含此分區

分桶表

說明:

將大表進行哈希散列抽樣存儲,方便作數據和代碼驗證。好比將表分紅10分,每次只拿其中的十分之一來使用,能夠快速的獲得結果

分桶底層實現邏輯:

在表對應的目錄下,將源文件拆分紅N個小文件

命令:

開啓分桶功能,強制多個 reduce 進行輸出:

set hive.enforce.bucketing=true;

準備主表:

create table teacher(id int,name string) row format delimited fields terminated by '|';

向主表加載數據:

load data local inpath '/root/work/teachers.txt' into table teacher;

建立帶桶的 table :

create table teacher_temp(id int,name string) clustered by (id) into 2 buckets row format delimited fields terminated by '|';

從主表向分桶表導入數據:

insert overwrite/into table teacher_temp select * from tacher; 分桶表其實就是將表中的數據按照hash分桶的方式分桶存放,而所謂的桶就是表文件夾下不一樣的文件

使用場景:

對於一個龐大的數據集咱們常常須要拿出來一小部分做爲樣例,而後在樣例上驗證咱們的查詢,優化咱們的程序,利用分桶表能夠實現數據的抽樣

相關文章
相關標籤/搜索