hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū),針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

改則網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,改則網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為改則上千提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的改則做網(wǎng)站的公司定做!

一、基本概念

  hive中分區(qū)表分為:范圍分區(qū)、列表分區(qū)、hash分區(qū)、混合分區(qū)等。

  分區(qū)列:分區(qū)列不是表中的一個實際的字段,而是一個或者多個偽列。翻譯一下是:“在表的數(shù)據(jù)文件中實際上并不保存分區(qū)列的信息與數(shù)據(jù)”,這個概念十分重要,要記住,后面是經(jīng)常用到。

1.1 創(chuàng)建數(shù)據(jù)表

  下面的語句創(chuàng)建了一個簡單的分區(qū)表:

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

create table partition_test(
  member_id string,
  name string
)
partitioned by (
  stat_date string,
  province string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

復(fù)制代碼

1.2 創(chuàng)建分區(qū)

  這個例子中創(chuàng)建了stat_date和province兩個字段作為分區(qū)列。如果要添加數(shù)據(jù),通常情況下我們需要先創(chuàng)建好分區(qū),然后才能使用該分區(qū),例如:

alter table partition_test add partition (stat_date='20141113',province='jilin');

  這樣就創(chuàng)建好了一個分區(qū)。這時我們會看到hive在HDFS存儲中創(chuàng)建了一個相應(yīng)的文件夾:

hive> dfs -ls /user/ticketdev/hive/warehouse/partition_test/stat_date=20141113;
Found 1 items
drwxr-xr-x   - ticketdev ticketdev          0 2014-11-13 17:50 /user/ticketdev/hive/warehouse/partition_test/stat_date=20141113/province=jilin
h

  每一個分區(qū)都會有一個獨立的文件夾,在這個例子中stat_date是主文件夾,province是子文件夾,如:

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive> alter table partition_test add partition (stat_date='20141113',province='beijing');     
OK
Time taken: 0.119 seconds
hive> dfs -ls /user/ticketdev/hive/warehouse/partition_test/stat_date=20141113/;              
Found 2 items
drwxr-xr-x   - ticketdev ticketdev          0 2014-11-13 18:06 /user/ticketdev/hive/warehouse/partition_test/stat_date=20141113/province=beijing
drwxr-xr-x   - ticketdev ticketdev          0 2014-11-13 17:50 /user/ticketdev/hive/warehouse/partition_test/stat_date=20141113/province=jilin

復(fù)制代碼

二、靜態(tài)分區(qū)

2.1 數(shù)據(jù)準備

  基本知識介紹到這里,下面開始插入數(shù)據(jù)。我使用一個輔助的非分區(qū)表partition_test_input準備向partition_test中插入數(shù)據(jù):

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive> desc partition_test_input;
OK
stat_date string
member_id string
name string
province string

hive> select * from partition_test_input;
OK20110526 1 liujiannan liaoning20110526 2 wangchaoqun hubei20110728 3 xuhongxing sichuan20110728 4 zhudaoyong henan20110728 5 zhouchengyu heilongjiang

復(fù)制代碼

2.2 添加數(shù)據(jù)

  然后我向partition_test的分區(qū)中插入數(shù)據(jù):

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive> insert overwrite table partition_test partition(stat_date='20110728',province='henan') select member_id,name from partition_test_input where stat_date='20141113' and province='beijing';
Total MapReduce jobs = 2...1 Rows loaded to partition_test
OK

復(fù)制代碼

  還可以同時向多個分區(qū)插入數(shù)據(jù):

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive>> from partition_test_input> insert overwrite table partition_test partition (stat_date='20110526',province='liaoning')> select member_id,name where stat_date='20110526' and province='liaoning'> insert overwrite table partition_test partition (stat_date='20110728',province='sichuan')> select member_id,name where stat_date='20110728' and province='sichuan'> insert overwrite table partition_test partition (stat_date='20110728',province='heilongjiang')> select member_id,name where stat_date='20110728' and province='heilongjiang';
Total MapReduce jobs = 4...3 Rows loaded to partition_test
OK

復(fù)制代碼

  特別要注意,在其他數(shù)據(jù)庫中,一般向分區(qū)表中插入數(shù)據(jù)時系統(tǒng)會校驗數(shù)據(jù)是否符合該分區(qū),如果不符合會報錯。而在hive中,向某個分區(qū)中插入什么樣的數(shù)據(jù)完全是由人來控制的,因為分區(qū)鍵是偽列,不實際存儲在文件中,如:

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive> insert overwrite table partition_test partition(stat_date='20110527',province='liaoning') select member_id,name from partition_test_input;
Total MapReduce jobs = 2...5 Rows loaded to partition_test
OK

hive> select * from partition_test where stat_date='20110527' and province='liaoning';
OK1 liujiannan 20110527 liaoning2 wangchaoqun 20110527 liaoning3 xuhongxing 20110527 liaoning4 zhudaoyong 20110527 liaoning5 zhouchengyu 20110527 liaoning

復(fù)制代碼

  可以看到在partition_test_input中的5條數(shù)據(jù)有著不同的stat_date和province,但是在插入到partition(stat_date='20110527',province='liaoning')這個分區(qū)后,5條數(shù)據(jù)的stat_date和province都變成相同的了,因為這兩列的數(shù)據(jù)是根據(jù)文件夾的名字讀取來的,而不是實際從數(shù)據(jù)文件中讀取來的:

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

$ hadoop fs -cat /user/hive/warehouse/partition_test/stat_date=20110527/province=liaoning/000000_01,liujiannan2,wangchaoqun3,xuhongxing4,zhudaoyong5,zhouchengyu

復(fù)制代碼

三、動態(tài)分區(qū)

  下面介紹一下動態(tài)分區(qū),因為按照上面的方法向分區(qū)表中插入數(shù)據(jù),如果源數(shù)據(jù)量很大,那么針對一個分區(qū)就要寫一個insert,非常麻煩。況且在之前的版本中,必須先手動創(chuàng)建好所有的分區(qū)后才能插入,這就更麻煩了,你必須先要知道源數(shù)據(jù)中都有什么樣的數(shù)據(jù)才能創(chuàng)建分區(qū)。

  使用動態(tài)分區(qū)可以很好的解決上述問題。動態(tài)分區(qū)可以根據(jù)查詢得到的數(shù)據(jù)自動匹配到相應(yīng)的分區(qū)中去。

  使用動態(tài)分區(qū)要先設(shè)置hive.exec.dynamic.partition參數(shù)值為true,默認值為false,即不允許使用:

hive> set hive.exec.dynamic.partition;
hive.exec.dynamic.partition=false
hive> set hive.exec.dynamic.partition=true;
hive> set hive.exec.dynamic.partition;
hive.exec.dynamic.partition=true

  動態(tài)分區(qū)的使用方法很簡單,假設(shè)我想向stat_date='20110728'這個分區(qū)下面插入數(shù)據(jù),至于province插入到哪個子分區(qū)下面讓數(shù)據(jù)庫自己來判斷,那可以這樣寫:

hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)

hive> insert overwrite table partition_test partition(stat_date='20110728',province)> select member_id,name,province from partition_test_input where stat_date='20110728';
Total MapReduce jobs = 2...3 Rows loaded to partition_test
OK

復(fù)制代碼

  stat_date叫做靜態(tài)分區(qū)列,province叫做動態(tài)分區(qū)列。select子句中需要把動態(tài)分區(qū)列按照分區(qū)的順序?qū)懗鰜恚o態(tài)分區(qū)列不用寫出來。這樣stat_date='20110728'的所有數(shù)據(jù),會根據(jù)province的不同分別插入到/user/hive/warehouse/partition_test/stat_date=20110728/下面的不同的子文件夾下,如果源數(shù)據(jù)對應(yīng)的province子分區(qū)不存在,則會自動創(chuàng)建,非常方便,而且避免了人工控制插入數(shù)據(jù)與分區(qū)的映射關(guān)系存在的潛在風(fēng)險。

  注意,動態(tài)分區(qū)不允許主分區(qū)采用動態(tài)列而副分區(qū)采用靜態(tài)列,這樣將導(dǎo)致所有的主分區(qū)都要創(chuàng)建副分區(qū)靜態(tài)列所定義的分區(qū):

hive> insert overwrite table partition_test partition(stat_date,province='liaoning')> select member_id,name,province from partition_test_input where province='liaoning';
FAILED: Error in semantic analysis: Line 1:48 Dynamic partition cannot be the parent of a static partition 'liaoning'

  動態(tài)分區(qū)可以允許所有的分區(qū)列都是動態(tài)分區(qū)列,但是要首先設(shè)置一個參數(shù)hive.exec.dynamic.partition.mode :

hive> set hive.exec.dynamic.partition.mode;
hive.exec.dynamic.partition.mode=strict

  它的默認值是strick,即不允許分區(qū)列全部是動態(tài)的,這是為了防止用戶有可能原意是只在子分區(qū)內(nèi)進行動態(tài)建分區(qū),但是由于疏忽忘記為主分區(qū)列指定值了,這將導(dǎo)致一個dml語句在短時間內(nèi)創(chuàng)建大量的新的分區(qū)(對應(yīng)大量新的文件夾),對系統(tǒng)性能帶來影響。所以我們要設(shè)置:

hive> set hive.exec.dynamic.partition.mode=nostrick;

  再介紹3個參數(shù):

  • hive.exec.max.dynamic.partitions.pernode (缺省值100):每一個mapreduce job允許創(chuàng)建的分區(qū)的最大數(shù)量,如果超過了這個數(shù)量就會報錯

  • hive.exec.max.dynamic.partitions (缺省值1000):一個dml語句允許創(chuàng)建的所有分區(qū)的最大數(shù)量

  • hive.exec.max.created.files (缺省值100000):所有的mapreduce job允許創(chuàng)建的文件的最大數(shù)量

關(guān)于hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

新聞標題:hive中怎么實現(xiàn)動態(tài)分區(qū)和靜態(tài)分區(qū)
分享鏈接:http://bm7419.com/article30/igchpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃外貿(mào)建站、面包屑導(dǎo)航、響應(yīng)式網(wǎng)站、微信小程序、軟件開發(fā)

廣告

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

微信小程序開發(fā)