hive中partition分割槽概念

2021-08-19 05:42:10 字數 2669 閱讀 4208

一、背景

1、在hive select查詢中一般會掃瞄整個表內容,會消耗很多時間做沒必要的工作。有時候只需要掃瞄表中關心的一部分資料,因此建表時引入了partition概念。

2、分割槽表指的是在建立表時指定的partition的分割槽空間。

3、如果需要建立有分割槽的表,需要在create表的時候呼叫可選引數partitioned by,詳見表建立的語法結構。

二、技術細節

1、乙個表可以擁有乙個或者多個分割槽,每個分割槽以資料夾的形式單獨存在表資料夾的目錄下。

2、表和列名不區分大小寫。

3、分割槽是以字段的形式在表結構中存在,通過describe table命令可以檢視到字段存在,但是該字段不存放實際的資料內容,僅僅是分割槽的表示。

4、建表的語法(建分割槽可參見partitioned by引數):

create [external] table [if not exists] table_name [(col_name data_type [comment col_comment], ...)] [comment table_comment] [partitioned by (col_name data_type [comment col_comment], ...)] [clustered by (col_name, col_name, ...) [sorted by (col_name [asc|desc], ...)] into num_buckets buckets] [row format row_format] [stored as file_format] [location hdfs_path]

5、分割槽建表分為2種,一種是單分割槽,也就是說在表資料夾目錄下只有一級資料夾目錄。另外一種是多分割槽,表資料夾下出現多資料夾巢狀模式。

a、單分割槽建表語句:create table day_table (id int, content string) partitioned by (dt string);單分割槽表,按天分割槽,在表結構中存在id,content,dt三列。

b、雙分割槽建表語句:create table day_hour_table (id int, content string) partitioned by (dt string, hour string);雙分割槽表,按天和小時分割槽,在表結構中新增加了dt和hour兩列。

表資料夾目錄示意圖(多分割槽表):

6、新增分割槽表語法(表已建立,在此基礎上新增分割槽):

alter table table_name add partition_spec [ location 'location1' ] partition_spec [ location 'location2' ] ... partition_spec: : partition (partition_col = partition_col_value, partition_col = partiton_col_value, ...)

使用者可以用 alter table add partition 來向乙個表中增加分割槽。當分割槽名是字串時加引號。例:

alter table day_table add partition (dt='2008-08-08', hour='08') location '/path/pv1.txt' partition (dt='2008-08-08', hour='09') location '/path/pv2.txt';

7、刪除分割槽語法:

alter table table_name drop partition_spec, partition_spec,...

使用者可以用 alter table drop partition 來刪除分割槽。分割槽的元資料和資料將被一併刪除。例:

alter table day_hour_table drop partition (dt='2008-08-08', hour='09');

8、資料載入進分割槽表中語法:

load data [local] inpath 'filepath' [overwrite] into table tablename [partition (partcol1=val1, partcol2=val2 ...)]

例:load data inpath '/user/pv.txt' into table day_hour_table partition(dt='2008-08- 08', hour='08'); load data local inpath '/user/hua/*' into table day_hour partition(dt='2010-07- 07');

當資料被載入至表中時,不會對資料進行任何轉換。load操作只是將資料複製至hive表對應的位置。資料載入時在表下自動建立乙個目錄,檔案存放在該分割槽下。

9、基於分割槽的查詢的語句:

select day_table.* from day_table where day_table.dt>= '2008-08-08';

10、檢視分割槽語句:

hive> show partitions day_hour_table; ok dt=2008-08-08/hour=08 dt=2008-08-08/hour=09 dt=2008-08-09/hour=09

三、總結

1、在 hive 中,表中的乙個 partition 對應於表下的乙個目錄,所有的 partition 的資料都儲存在最字集的目錄中。

2、總的說來partition就是輔助查詢,縮小查詢範圍,加快資料的檢索速度和對資料按照一定的規格和條件進行管理。

hive中partition如何使用

一 背景 1 在hive select查詢中一般會掃瞄整個表內容,會消耗很多時間做沒必要的工作。有時候只需要掃瞄表中關心的一部分資料,因此建表時引入了partition概念。2 分割槽表指的是在建立表時指定的partition的分割槽空間。3 如果需要建立有分割槽的表,需要在create表的時候呼叫...

Hive分割槽partition詳解

請看原文作者的部落格 我補充的是 外部表的分割槽 create external table t2 id int name string hobby array,add map partitioned by pt d string row format delimited fields termin...

Hive動態分割槽 Partition中跟函式

一 前段時間因為導表需求 從一張表中查詢出資料,按日期分割槽overwrite 到指定分割槽表中 在hive裡面研究了一下自動分割槽。步驟 1 建好所需分割槽表 2 設定分割槽引數?1 2 3 4 sethive.exec.dynamic.partition true 可通過這個語句檢視 sethi...