怎么理解hive分區(qū)partition

本篇文章給大家分享的是有關(guān)怎么理解hive分區(qū)partition,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)建站從2013年成立,先為桐柏等服務(wù)建站,桐柏等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為桐柏企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

一、背景

1、在Hive Select查詢中一般會掃描整個表內(nèi)容,會消耗很多時間做沒必要的工作。有時候只需要掃描表中關(guān)心的一部分?jǐn)?shù)據(jù),因此建表時引入了partition概念。

2、分區(qū)表指的是在創(chuàng)建表時指定的partition的分區(qū)空間。

3、如果需要創(chuàng)建有分區(qū)的表,需要在create表的時候調(diào)用可選參數(shù)partitioned by,詳見表創(chuàng)建的語法結(jié)構(gòu)。

二、技術(shù)細(xì)節(jié)

1、一個表可以擁有一個或者多個分區(qū),每個分區(qū)以文件夾的形式單獨(dú)存在表文件夾的目錄下。

2、表和列名不區(qū)分大小寫。

3、分區(qū)是以字段的形式在表結(jié)構(gòu)中存在,通過describe table命令可以查看到字段存在,但是該字段不存放實(shí)際的數(shù)據(jù)內(nèi)容,僅僅是分區(qū)的表示。

4、建表的語法(建分區(qū)可參見PARTITIONED BY參數(shù)):

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、分區(qū)建表分為2種,一種是單分區(qū),也就是說在表文件夾目錄下只有一級文件夾目錄。另外一種是多分區(qū),表文件夾下出現(xiàn)多文件夾嵌套模式。

a、單分區(qū)建表語句:create table day_table (id int, content string) partitioned by (dt string);單分區(qū)表,按天分區(qū),在表結(jié)構(gòu)中存在id,content,dt三列。

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

怎么理解hive分區(qū)partition

表文件夾目錄示意圖(多分區(qū)表):

6、添加分區(qū)表語法(表已創(chuàng)建,在此基礎(chǔ)上添加分區(qū)):

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 來向一個表中增加分區(qū)。當(dāng)分區(qū)名是字符串時加引號。例:

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、刪除分區(qū)語法:

ALTER TABLE table_name DROP partition_spec, partition_spec,...

用戶可以用 ALTER TABLE DROP PARTITION 來刪除分區(qū)。分區(qū)的元數(shù)據(jù)和數(shù)據(jù)將被一并刪除。例:

ALTER TABLE day_hour_table DROP PARTITION (dt='2008-08-08', hour='09');

8、數(shù)據(jù)加載進(jìn)分區(qū)表中語法:

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');

當(dāng)數(shù)據(jù)被加載至表中時,不會對數(shù)據(jù)進(jìn)行任何轉(zhuǎn)換。Load操作只是將數(shù)據(jù)復(fù)制至Hive表對應(yīng)的位置。數(shù)據(jù)加載時在表下自動創(chuàng)建一個目錄,文件存放在該分區(qū)下。

9、基于分區(qū)的查詢的語句:

SELECT day_table.* FROM day_table WHERE day_table.dt>= '2008-08-08';

10、查看分區(qū)語句:

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

三、總結(jié)

1、在 Hive 中,表中的一個 Partition 對應(yīng)于表下的一個目錄,所有的 Partition 的數(shù)據(jù)都存儲在最字集的目錄中。

2、總的說來partition就是輔助查詢,縮小查詢范圍,加快數(shù)據(jù)的檢索速度和對數(shù)據(jù)按照一定的規(guī)格和條件進(jìn)行管理。

——————————————————————————————————————

hive中關(guān)于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指定目錄結(jié)構(gòu)
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分區(qū)partition,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前文章:怎么理解hive分區(qū)partition
分享路徑:http://muchs.cn/article38/pihppp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站內(nèi)鏈、微信小程序網(wǎng)站建設(shè)、品牌網(wǎng)站建設(shè)動態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)