Hive分區表總結

分區表實際上就是對應一個HDFS文件系統上的獨立的文件夾,該文件夾下是該分區所有的數據文件。Hive中的分區就是分目錄,把一個大的數據集根據業務需要分割成小的數據集。在查詢時通過WHERE子句中的表達式選擇查詢所需要的指定的分區,這樣的查詢效率會提高很多。

當設置爲動態分區時,首先把Hive設置爲"nostrict"模式

hive>set hive.mapred.mode=nonstrict;

分區表基本操作

  1. 引入分區表(需要根據日期對日誌進行管理)
/user/hive/warehouse/log_partition/20170702/20170702.log
/user/hive/warehouse/log_partition/20170703/20170703.log
/user/hive/warehouse/log_partition/20170704/20170704.log

2.創建分區表語法

hive (default)> create table dept_partition(
deptno int, dname string, loc string
)
partitioned by (month string)
row format delimited fields terminated by '\t';

3.加載數據到分區表中

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201709');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201708');
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table default.dept_partition partition(month='201707’);

加載數據到分區表,如下圖所示:

HDFS上顯示分區表,如下圖所示: 

4.查詢分區表中數據 

單分區查詢

hive (default)> select * from dept_partition where month='201709';

多分區聯合查詢

hive (default)> select * from dept_partition where month='201709'
              union
              select * from dept_partition where month='201708'
              union
              select * from dept_partition where month='201707';

_u3.deptno      _u3.dname       _u3.loc _u3.month
10      ACCOUNTING      NEW YORK        201707
10      ACCOUNTING      NEW YORK        201708
10      ACCOUNTING      NEW YORK        201709
20      RESEARCH        DALLAS  201707
20      RESEARCH        DALLAS  201708
20      RESEARCH        DALLAS  201709
30      SALES   CHICAGO 201707
30      SALES   CHICAGO 201708
30      SALES   CHICAGO 201709
40      OPERATIONS      BOSTON  201707
40      OPERATIONS      BOSTON  201708
40      OPERATIONS      BOSTON  201709

5.增加分區

創建單個分區

hive (default)> alter table dept_partition add partition(month='201706') ;

同時創建多個分區

hive (default)> alter table dept_partition add partition(month='201705') partition(month='201704');

注意:創建多個分區用空格隔開

6.刪除分區

刪除單個分區

hive (default)> alter table dept_partition drop partition (month='201704');

同時刪除多個分區

hive (default)> alter table dept_partition drop partition (month='201705'), partition (month='201706');

注意:刪除多個分區用逗號隔開,與創建不同

7.查看分區表有多少分區

hive> show partitions dept_partition;

8.查看分區表結構

hive> desc formatted dept_partition;

# Partition Information          
# col_name              data_type               comment             
month                   string    

分區表注意事項

1.創建二級分區表

hive (default)> create table dept_partition2(
               deptno int, dname string, loc string
               )
               partitioned by (month string, day string)
               row format delimited fields terminated by '\t';

2.正常的加載數據

(1)加載數據到二級分區表中

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
 default.dept_partition2 partition(month='201709', day='13');

(2)查詢分區數據

hive (default)> select * from dept_partition2 where month='201709' and day='13';

3.把數據直接上傳到分區目錄上,讓分區表和數據產生關聯的三種方式

(1)方式一:上傳數據後修復

上傳數據

hive (default)> dfs -mkdir -p
 /user/hive/warehouse/dept_partition2/month=201709/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt  /user/hive/warehouse/dept_partition2/month=201709/day=12;

查詢數據(查詢不到剛上傳的數據)

hive (default)> select * from dept_partition2 where month='201709' and day='12';

執行修復命令

hive> msck repair table dept_partition2;

再次查詢數據

hive (default)> select * from dept_partition2 where month='201709' and day='12';

(2)方式二:上傳數據後添加分區

上傳數據

hive (default)> dfs -mkdir -p
 /user/hive/warehouse/dept_partition2/month=201709/day=11;
hive (default)> dfs -put /opt/module/datas/dept.txt  /user/hive/warehouse/dept_partition2/month=201709/day=11;

執行添加分區

hive (default)> alter table dept_partition2 add partition(month='201709',
 day='11');

查詢數據

hive (default)> select * from dept_partition2 where month='201709' and day='11';

方式三:創建文件夾後load數據到分區

創建目錄

hive (default)> dfs -mkdir -p
 /user/hive/warehouse/dept_partition2/month=201709/day=10;

上傳數據

hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table
 dept_partition2 partition(month='201709',day='10');

查詢數據

hive (default)> select * from dept_partition2 where month='201709' and day='10';

動態分區調整

關係型數據庫中,對分區表Insert數據時候,數據庫自動會根據分區字段的值,將數據插入到相應的分區中,Hive中也提供了類似的機制,即動態分區(Dynamic Partition),只不過,使用Hive的動態分區,需要進行相應的配置。

1.開啓動態分區參數設置

(1)開啓動態分區功能(默認true,開啓)

hive.exec.dynamic.partition=true

(2)設置爲非嚴格模式(動態分區的模式,默認strict,表示必須指定至少一個分區爲靜態分區,nonstrict模式表示允許所有的分區字段都可以使用動態分區。)

hive.exec.dynamic.partition.mode=nonstrict

(3)在所有執行MR的節點上,最大一共可以創建多少個動態分區。

hive.exec.max.dynamic.partitions=1000

(4)在每個執行MR的節點上,最大可以創建多少個動態分區。該參數需要根據實際的數據來設定。比如:源數據中包含了一年的數據,即day字段有365個值,那麼該參數就需要設置成大於365,如果使用默認值100,則會報錯。

hive.exec.max.dynamic.partitions.pernode=100

(5)整個MR Job中,最大可以創建多少個HDFS文件。

hive.exec.max.created.files=100000

(6)當有空分區生成時,是否拋出異常。一般不需要設置。

hive.error.on.empty.partition=false

下面一個例子來理解下動態分區

需求:將ori中的數據按照時間(如:20111230000008),插入到目標表ori_partitioned_target的相應分區中。

(1)創建分區表

create table ori_partitioned(id bigint, time bigint, uid string, keyword string,
 url_rank int, click_num int, click_url string) 
partitioned by (p_time bigint) 
row format delimited fields terminated by '\t';

(2)加載數據到分區表中

hive (default)> load data local inpath '/home/atguigu/ds1' into table
 ori_partitioned partition(p_time='20111230000010') ;
hive (default)> load data local inpath '/home/atguigu/ds2' into table ori_partitioned partition(p_time='20111230000011') ;

(3)創建目標分區表

create table ori_partitioned_target(id bigint, time bigint, uid string,
 keyword string, url_rank int, click_num int, click_url string) PARTITIONED BY (p_time STRING) row format delimited fields terminated by '\t';

(4)設置動態分區

set hive.exec.dynamic.partition = true;
set hive.exec.dynamic.partition.mode = nonstrict;
set hive.exec.max.dynamic.partitions = 1000;
set hive.exec.max.dynamic.partitions.pernode = 100;
set hive.exec.max.created.files = 100000;
set hive.error.on.empty.partition = false;

hive (default)> insert overwrite table ori_partitioned_target partition (p_time) 
select id, time, uid, keyword, url_rank, click_num, click_url, p_time from ori_partitioned;

(5)查看目標分區表的分區情況

hive (default)> show partitions ori_partitioned_target;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章