1. 程式人生 > >Hive中的分割槽表

Hive中的分割槽表

目錄

總結:

分割槽結構圖和原理:

上手案例實操: 

總結:


總結:

①建立分割槽表的時候,指定非表字段的分割槽欄位,使用partitioned by ;②向分割槽表中插入資料的時候,在表名的後面要加上partition(分割槽欄位名=分割槽欄位值)③如果一個表是分割槽表,那麼該分割槽表在HDFS上是一個以表名為名的路徑,那麼同時,該路徑下還有分割槽的路徑,以分割槽欄位=分割槽欄位值的形式命名。④使用where來進行分割槽查詢。⑤alter add多個用空格,alter drop 多個用逗號。⑥:分割槽的目的是提高查詢效率

分割槽結構圖和原理:

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

分割槽表的目的是為了提高查詢效率。

上手案例實操: 

1,建立分割槽表:分割槽的欄位不能是表中的欄位
create table stu_par(id int,name string)
partitioned by (month string)  
row format delimited
fields terminated by '\t';

使用下面的命令檢視該表分割槽的欄位
0: jdbc:hive2://hadoop108:10000> desc formatted stu_par;

2,向分割槽表中匯入資料:
 load data local inpath '/opt/module/hive/stu.txt' into table stu_par partition(month = '12');

0: jdbc:hive2://hadoop108:10000> select * from stu_par;
OK
+-------------+---------------+----------------+--+
| stu_par.id  | stu_par.name  | stu_par.month  |
+-------------+---------------+----------------+--+
| 1001        | zhangfei      | 12             |
| 1002        | liubei        | 12             |
| 1003        | guanyu        | 12             |
| 1004        | zhaoyun       | 12             |
| 1005        | caocao        | 12             |
| 1006        | zhouyu        | 12             |
+-------------+---------------+----------------+--+
發現分割槽的欄位資訊也作為表的欄位資訊顯示出來,所以分割槽欄位得名虛欄位。

3,繼續執行:
load data local inpath '/opt/module/hive/stu.txt' into table stu_par partition(month = '11');

load data local inpath '/opt/module/hive/stu.txt' into table stu_par partition(month = '10');

4,檢視HDFS中的表資訊:
0: jdbc:hive2://hadoop108:10000> !sh hadoop fs -ls /user/hive/warehouse/db_hive.db/stu_par
Found 3 items
drwxr-xr-x   - isea supergroup      0 2018-12-01 04:34 /user/hive/warehouse/db_hive.db/stu_par/month=10
drwxr-xr-x   - isea supergroup      0 2018-12-01 04:34 /user/hive/warehouse/db_hive.db/stu_par/month=11
drwxr-xr-x   - isea supergroup      0 2018-12-01 04:30 /user/hive/warehouse/db_hive.db/stu_par/month=12

可以看出分割槽表會根據分割槽的欄位和分割槽欄位值的形式,組成  欄位=欄位值 的形式形成一個資料夾,在該
資料夾下儲存著分割槽欄位的資訊

5,查詢某個分割槽的欄位使用where:
0: jdbc:hive2://hadoop108:10000> select * from stu_par where month = 10;
OK
+-------------+---------------+----------------+--+
| stu_par.id  | stu_par.name  | stu_par.month  |
+-------------+---------------+----------------+--+
| 1001        | zhangfei      | 10             |
| 1002        | liubei        | 10             |
| 1003        | guanyu        | 10             |
| 1004        | zhaoyun       | 10             |
| 1005        | caocao        | 10             |
| 1006        | zhouyu        | 10             |
+-------------+---------------+----------------+--+

6,查詢表的分割槽情況:
0: jdbc:hive2://hadoop108:10000> show partitions stu_par;
OK
+------------+--+
| partition  |
+------------+--+
| month=10   |
| month=11   |
| month=12   |
+------------+--+

6,多分割槽查詢可以使用union,要使用MapReduce:
select * from stu_par where month = 10
union
select * from stu_par where month = 11;

+---------+-----------+------------+--+
| _u2.id  | _u2.name  | _u2.month  |
+---------+-----------+------------+--+
| 1001    | zhangfei  | 10         |
| 1001    | zhangfei  | 11         |
| 1002    | liubei    | 10         |
| 1002    | liubei    | 11         |
| 1003    | guanyu    | 10         |
| 1003    | guanyu    | 11         |
| 1004    | zhaoyun   | 10         |
| 1004    | zhaoyun   | 11         |
| 1005    | caocao    | 10         |
| 1005    | caocao    | 11         |
| 1006    | zhouyu    | 10         |
| 1006    | zhouyu    | 11         |
+---------+-----------+------------+--+

7,增加,和刪除多個分割槽:增加空格,刪除逗號:
增加分割槽,多個
alter table stu_par add partition(month = '09') partition(month = '08');
刪除分割槽,多個
alter table stu_par drop partition(month = '09'),partition(month = '08');

8,建立多級分割槽,在HDFS上,二級分割槽將會是一級分割槽的子資料夾:
> load data local inpath '/opt/module/hive/stu.txt' into table stu_par2 partition(month = '12', day = '01');

0: jdbc:hive2://hadoop108:10000> select * from stu_par2;
OK
+--------------+----------------+-----------------+---------------+--+
| stu_par2.id  | stu_par2.name  | stu_par2.month  | stu_par2.day  |
+--------------+----------------+-----------------+---------------+--+
| 1001         | zhangfei       | 12              | 01            |
| 1002         | liubei         | 12              | 01            |
| 1003         | guanyu         | 12              | 01            |
| 1004         | zhaoyun        | 12              | 01            |
| 1005         | caocao         | 12              | 01            |
| 1006         | zhouyu         | 12              | 01            |
+--------------+----------------+-----------------+---------------+--+

增加分割槽,獲取分割槽資訊:
> alter table stu_par2 add partition(month = '12',day = '02');
0: jdbc:hive2://hadoop108:10000> show partitions stu_par2;
OK
+------------------+--+
|    partition     |
+------------------+--+
| month=12/day=01  |
| month=12/day=02  |
+------------------+--+

alter table stu_par2 add partition(month = '12',day = '02');
就是在Metastore中的partition表中添加了一個欄位的資訊,如下圖:

如下圖:

總結:

①建立分割槽表的時候,指定非表字段的分割槽欄位,使用partitioned by ;②向分割槽表中插入資料的時候,在表名的後面要加上partition(分割槽欄位名=分割槽欄位值)③如果一個表是分割槽表,那麼該分割槽表在HDFS上是一個以表名為名的路徑,那麼同時,該路徑下還有分割槽的路徑,以分割槽欄位=分割槽欄位值的形式命名。④使用where來進行分割槽查詢。⑤alter add多個用空格,alter drop 多個用逗號;⑥分割槽的目的是為了提高查詢效率