前面文章介紹了Hive中是支持分區(qū)的锈拨。
關(guān)系型數(shù)據(jù)庫(如Oracle)中十拣,對分區(qū)表Insert數(shù)據(jù)時(shí)候叙身,數(shù)據(jù)庫自動(dòng)會(huì)根據(jù)分區(qū)字段的值渔扎,將數(shù)據(jù)插入到相應(yīng)的分區(qū)中,Hive中也提供了類似的機(jī)制信轿,即動(dòng)態(tài)分區(qū)(Dynamic Partition)晃痴,只不過,使用Hive的動(dòng)態(tài)分區(qū)财忽,需要進(jìn)行相應(yīng)的配置倘核。
先看一個(gè)應(yīng)用場景,源表t_lxw1234的數(shù)據(jù)如下:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- SELECT day,url FROM t_lxw1234;
- 2015-05-10 url1
- 2015-05-10 url2
- 2015-06-14 url1
- 2015-06-14 url2
- 2015-06-15 url1
- 2015-06-15 url2
- ……
</pre>
目標(biāo)表為:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
- CREATE TABLE t_lxw1234_partitioned (
- url STRING
- ) PARTITIONED BY (month STRING,day STRING)
- stored AS textfile;
</pre>
需求:將t_lxw1234中的數(shù)據(jù)按照時(shí)間(day)即彪,插入到目標(biāo)表t_lxw1234_partitioned的相應(yīng)分區(qū)中紧唱。
如果按照之前介紹的往指定一個(gè)分區(qū)中Insert數(shù)據(jù),那么這個(gè)需求很不容易實(shí)現(xiàn)隶校。
這時(shí)候就需要使用動(dòng)態(tài)分區(qū)來實(shí)現(xiàn)漏益,使用動(dòng)態(tài)分區(qū)需要注意設(shè)定以下參數(shù):
- hive.exec.dynamic.partition
默認(rèn)值:false
是否開啟動(dòng)態(tài)分區(qū)功能,默認(rèn)false關(guān)閉深胳。
使用動(dòng)態(tài)分區(qū)時(shí)候遭庶,該參數(shù)必須設(shè)置成true;
- hive.exec.dynamic.partition.mode
默認(rèn)值:strict
動(dòng)態(tài)分區(qū)的模式,默認(rèn)strict稠屠,表示必須指定至少一個(gè)分區(qū)為靜態(tài)分區(qū),nonstrict模式表示允許所有的分區(qū)字段都可以使用動(dòng)態(tài)分區(qū)翎苫。
一般需要設(shè)置為nonstrict
- hive.exec.max.dynamic.partitions.pernode
默認(rèn)值:100
在每個(gè)執(zhí)行MR的節(jié)點(diǎn)上权埠,最大可以創(chuàng)建多少個(gè)動(dòng)態(tài)分區(qū)。
該參數(shù)需要根據(jù)實(shí)際的數(shù)據(jù)來設(shè)定煎谍。
比如:源數(shù)據(jù)中包含了一年的數(shù)據(jù)攘蔽,即day字段有365個(gè)值,那么該參數(shù)就需要設(shè)置成大于365呐粘,如果使用默認(rèn)值100满俗,則會(huì)報(bào)錯(cuò)转捕。
- hive.exec.max.dynamic.partitions
默認(rèn)值:1000
在所有執(zhí)行MR的節(jié)點(diǎn)上,最大一共可以創(chuàng)建多少個(gè)動(dòng)態(tài)分區(qū)唆垃。
同上參數(shù)解釋五芝。
- hive.exec.max.created.files
默認(rèn)值:100000
整個(gè)MR Job中,最大可以創(chuàng)建多少個(gè)HDFS文件辕万。
一般默認(rèn)值足夠了枢步,除非你的數(shù)據(jù)量非常大,需要?jiǎng)?chuàng)建的文件數(shù)大于100000渐尿,可根據(jù)實(shí)際情況加以調(diào)整醉途。
- hive.error.on.empty.partition
默認(rèn)值:false
當(dāng)有空分區(qū)生成時(shí),是否拋出異常砖茸。
一般不需要設(shè)置隘擎。
那么,上面的需求可以使用如下的語句來完成:
<pre class="prettyprint linenums" style="padding: 8px; color: rgb(68, 68, 68); border-radius: 2px; font-family: Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; display: block; margin: 0px 0px 20px; font-size: 14px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(248, 248, 248); border: 1px solid rgb(238, 238, 238); overflow: hidden; box-shadow: rgb(238, 238, 238) 40px 0px 0px inset, rgb(51, 183, 150) 42px 0px 0px inset; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 30px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
SET hive.exec.dynamic.partition=true;
SET hive.exec.dynamic.partition.mode=nonstrict;
SET hive.exec.max.dynamic.partitions.pernode = 1000;
SET hive.exec.max.dynamic.partitions=1000;
INSERT overwrite TABLE t_lxw1234_partitioned PARTITION (month,day)
SELECT url,substr(day,1,7) AS month,day
FROM t_lxw1234;
</pre>
注意:在PARTITION (month,day)中指定分區(qū)字段名即可凉夯;
在SELECT子句的最后兩個(gè)字段货葬,必須對應(yīng)前面PARTITION (month,day)中指定的分區(qū)字段,包括順序恍涂。
執(zhí)行結(jié)果如下:
Loading data to table liuxiaowen.t_lxw1234_partitioned partition (month=null, day=null)
Loading partition {month=2015-05, day=2015-05-10}
Loading partition {month=2015-06, day=2015-06-14}
Loading partition {month=2015-06, day=2015-06-15}
Partition liuxiaowen.t_lxw1234_partitioned{month=2015-05, day=2015-05-10} stats: [numFiles=1, numRows=2, totalSize=10, rawDataSize=8]
Partition liuxiaowen.t_lxw1234_partitioned{month=2015-06, day=2015-06-14} stats: [numFiles=1, numRows=2, totalSize=10, rawDataSize=8]
Partition liuxiaowen.t_lxw1234_partitioned{month=2015-06, day=2015-06-15} stats: [numFiles=1, numRows=2, totalSize=10, rawDataSize=8]
使用show partitions t_lxw1234_partitioned;查看目標(biāo)表有哪些分區(qū):
hive> show partitions t_lxw1234_partitioned;
OK
month=2015-05/day=2015-05-10
month=2015-06/day=2015-06-14
month=2015-06/day=2015-06-15