共計 3719 個字符,預計需要花費 10 分鐘才能閱讀完成。
本篇文章給大家分享的是有關怎么理解 hive 分區 partition,丸趣 TV 小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
一、背景
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 的操作:hive create table mp (a string) partitioned by (b string, c string);
OK
Time taken: 0.044 seconds
hive alter table mp add partition (b= 1 , c= 1
OK
Time taken: 0.079 seconds
hive alter table mp add partition (b= 1 , c= 2
OK
Time taken: 0.052 seconds
hive alter table mp add partition (b= 2 , c= 2
OK
Time taken: 0.056 seconds
hive show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=2/c=2
Time taken: 0.046 seconds
hive explain extended alter table mp drop partition (b= 1
OK
ABSTRACT SYNTAX TREE:
(TOK_ALTERTABLE_DROPPARTS mp (TOK_PARTSPEC (TOK_PARTVAL b 1)))
STAGE DEPENDENCIES:
Stage-0 is a root stage
STAGE PLANS:
Stage: Stage-0
Drop Table Operator:
Drop Table
table: mp
Time taken: 0.048 seconds
hive alter table mp drop partition (b= 1
FAILED: Error in metadata: table is partitioned but partition spec is not specified or tab: {b=1}
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask
hive show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=2/c=2
Time taken: 0.044 seconds
hive alter table mp add partition (b= 1 , c = 3) partition (b= 1 , c= 4
OK
Time taken: 0.168 seconds
hive show partitions mp ;
OK
b=1/c=1
b=1/c=2
b=1/c=3
b=1/c=4
b=2/c=2
b=2/c=3
Time taken: 0.066 seconds
hive insert overwrite table mp partition (b= 1 , c= 1) select cnt from tmp_et3 ;
hive alter table mp add columns (newcol string);
location 指定目錄結構
hive alter table alter2 add partition (insertdate= 2008-01-01) location 2008/01/01
hive alter table alter2 add partition (insertdate= 2008-01-02) location 2008/01/02
以上就是怎么理解 hive 分區 partition,丸趣 TV 小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注丸趣 TV 行業資訊頻道。