Hive基礎(十三):分割槽表和分桶表
分割槽表和分桶表
1 分割槽表
分割槽表實際上就是對應一個HDFS檔案系統上的獨立的資料夾,該資料夾下是該分割槽所有的資料檔案。Hive中的分割槽就是分目錄,把一個大的資料集根據業務需要分割成小的資料集。在查詢時通過WHERE子句中的表示式選擇查詢所需要的指定的分割槽,這樣的查詢效率會提高很多。
1.1 分割槽表基本操作
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');
注意:分割槽表載入資料時,必須指定分割槽
注意:分割槽表載入資料時,必須指定分割槽
圖載入資料到分割槽表
圖分割槽表
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.2 分割槽表注意事項
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';
(3)方式三:建立資料夾後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';
1.3 動態分割槽調整
關係型資料庫中,對分割槽表Insert資料時候,資料庫自動會根據分割槽欄位的值,將資料插入到相應的分割槽中,Hive中也提供了類似的機制,即動態分割槽(Dynamic Partition),只不過,使用Hive的動態分割槽,需要進行相應的配置。
1)開啟動態分割槽引數設定
(1)開啟動態分割槽功能(預設true,開啟)
hive.exec.dynamic.partition=true
(2)設定為非嚴格模式(動態分割槽的模式,預設strict,表示必須指定至少一個分割槽為靜態分割槽,nonstrict模式表示允許所有的分割槽欄位都可以使用動態分割槽。)
hive.exec.dynamic.partition.mode=nonstrict
(3)在所有執行MR的節點上,最大一共可以建立多少個動態分割槽。預設1000
hive.exec.max.dynamic.partitions=1000
(4)在每個執行MR的節點上,最大可以建立多少個動態分割槽。該引數需要根據實際的資料來設定。比如:源資料中包含了一年的資料,即day欄位有365個值,那麼該引數就需要設定成大於365,如果使用預設值100,則會報錯。
hive.exec.max.dynamic.partitions.pernode=100
(5)整個MR Job中,最大可以建立多少個HDFS檔案。預設100000
hive.exec.max.created.files=100000
(6)當有空分割槽生成時,是否丟擲異常。一般不需要設定。預設false
hive.error.on.empty.partition=false
2)案例實操
需求:將dept表中的資料按照地區(loc欄位),插入到目標表dept_partition的相應分割槽中。
(1)建立目標分割槽表
hive (default)> create table dept_partition(id int, name string) partitioned by (location int) row format delimited fields terminated by '\t';
(2)設定動態分割槽
set hive.exec.dynamic.partition.mode = nonstrict; hive (default)> insert into table dept_partition partition(location) select deptno, dname, loc from dept;
(3)檢視目標分割槽表的分割槽情況
hive (default)> show partitions dept_partition;
思考:目標分割槽表是如何匹配到分割槽欄位的?
2 分桶表
分割槽提供一個隔離資料和優化查詢的便利方式。不過,並非所有的資料集都可形成合理的分割槽。對於一張表或者分割槽,Hive 可以進一步組織成桶,也就是更為細粒度的資料範圍劃分。
分桶是將資料集分解成更容易管理的若干部分的另一個技術。
分割槽針對的是資料的儲存路徑;分桶針對的是資料檔案。
1)先建立分桶表,通過直接匯入資料檔案的方式
(1)資料準備
(2)建立分桶表
create table stu_buck(id int, name string) clustered by(id) into 4 buckets row format delimited fields terminated by '\t';
(3)查看錶結構
hive (default)> desc formatted stu_buck; Num Buckets: 4
(4)匯入資料到分桶表中
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table stu_buck;
(5)檢視建立的分桶表中是否分成4個桶
(6)查詢分桶的資料
select * from stu_buck; OK stu_buck.id stu_buck.name 1004 ss4 1008 ss8 1012 ss12 1016 ss16 1001 ss1 1005 ss5 1009 ss9 1013 ss13 1002 ss2 1006 ss6 1010 ss10 1014 ss14 1003 ss3 1007 ss7 1011 ss11 1015 ss15
分桶規則:
根據結果可知:Hive的分桶採用對分桶欄位的值進行雜湊,然後除以桶的個數求餘的方式決定該條記錄存放在哪個桶當中