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

Hive -分割槽表

1.建立一個分割槽表

hive (default)> create table order_partition(orderNumber string,event_time string)PARTITIONED BY(event_month string) row format delimited fields terminated by '\t';

2.把TXT文字上傳至分割槽表中

hive (default)> load data local inpath '/home/hadoop/data/order.txt' into table order_partition PARTITION (event_month='2014-05');
(如果上面把TXT載入到表中的時候出現錯誤日誌說什麼too long等等,就登入到mysql)
mysql> use ruoze_d5;
mysql> alter table PARTITIONS convert to character set latin1;
mysql> alter table PARTITION_KEYS convert to character set latin1;

3.分割槽表的建立及內容載入進去之後,檢視hdfs上的分割槽表

[[email protected] data]$ hdfs dfs -ls /user/hive/warehouse/order_partition
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 14:51 /user/hive/warehouse/order_partition/event_month=2014-05
[[email protected] data]$ hdfs dfs -ls /user/hive/warehouse/order_partition/event_month=2014-05
-rwxr-xr-x   1 hadoop supergroup        208 2018-11-09 14:51 /user/hive/warehouse/order_partition/event_month=2014-05/order.txt
(注:以後見到event_month=2014-05這種類似的資料夾考慮到其就是分割槽表)

4.在3.中的分割槽表order_partition手動建立一個類似分割槽表的資料夾,與3.對比

[[email protected] data]$ hdfs dfs -mkdir -p /user/hive/warehouse/order_partition/event_month=2014-06
手工在這裡面建立一個類似分割槽表的資料夾)
[[email protected] data]$ hdfs dfs -put order.txt /user/hive/warehouse/order_partition/event_month=2014-06
(再手動把order.txt檔案傳入建立的類似分割槽表的資料夾中)
hive (default)> select * from order_partition where event_month='2014-05';(分割槽表查詢的時候要把分割槽條件帶上,不然還是會在order_partition下面全域性搜尋)
10703007267488 2014-05-01 06:01:12.334+01 2014-05
10101043505096 2014-05-01 07:28:12.342+01 2014-05
10103043509747 2014-05-01 07:50:12.33+01 2014-05
10103043501575 2014-05-01 09:27:12.33+01 2014-05
10104043514061 2014-05-01 09:03:12.324+01 2014-05
hive (default)> select * from order_partition where  event_month='2014-06';(則顯示為空,並沒有資料。因為元資料並沒有,當我們正常建立分割槽表的時候,用load載入文件的時候,它會自動重新整理分割槽,而我們手動建立的分割槽表裡沒有元資料資訊。)
hive (default)> msck repair table order_partition;(重新整理order_partition分割槽表的分割槽)
Partitions not in metastore: order_partition:event_month=2014-06
Repair: Added partition to metastore order_partition:event_month=2014-06

hive (default)> select * from order_partition where event_month='2014-06';
10703007267488 2014-05-01 06:01:12.334+01 2014-06
10101043505096 2014-05-01 07:28:12.342+01 2014-06
10101043505096 2014-05-01 07:28:12.342+01 2014-06
10103043501575 2014-05-01 09:27:12.33+01 2014-06
10104043514061 2014-05-01 09:03:12.324+01 2014-06
(注:msck repair table order_partition 此命令不能用,它會刷所有的分割槽,效能很低!生產上杜絕使用此方法。用另一種方法來解決,如下:)
[
[email protected]
data]$ hdfs dfs -mkdir -p /user/hive/warehouse/order_partition/event_month=2014-07 [[email protected] data]$ hdfs dfs -put order.txt /user/hive/warehouse/order_partition/event_month=2014-07 hive (default)> alter table order_partition add partition(event_month='2014-07');(生產上一般使用這種方法進行新增分割槽裡面的元資料) hive (default)> select * from order_partition where event_month='2014-07'; 10703007267488 2014-05-01 06:01:12.334+01 2014-07 10101043505096 2014-05-01 07:28:12.342+01 2014-07 10101043505096 2014-05-01 07:28:12.342+01 2014-07 10103043501575 2014-05-01 09:27:12.33+01 2014-07 10104043514061 2014-05-01 09:03:12.324+01 2014-07 hive (default)> show partitions order_partition;(檢視order_partition表下面有哪些分割槽) event_month=2014-05 event_month=2014-06 event_month=2014-07

5.建立一個多級分割槽表

hive (default)> create table order_mulit_partition(orderNumber string,event_time string)PARTITIONED BY(event_month string, step string)row format delimited fields terminated by '\t';
hive (default)> desc formatted order_mulit_partition;(檢視分割槽表的詳細資訊)
hive (default)> load data local inpath '/home/hadoop/data/order.txt' into table order_mulit_partition PARTITION (event_month='2014-05',step='1'); (載入資料進去)
hive (default)> select *from order_mulit_partition where event_month='2014-05';
10703007267488 2014-05-01 06:01:12.334+01 2014-05 1
10101043505096 2014-05-01 07:28:12.342+01 2014-05 1
10103043509747 2014-05-01 07:50:12.33+01 2014-05 1
10103043501575 2014-05-01 09:27:12.33+01 2014-05 1
10104043514061 2014-05-01 09:03:12.324+01 2014-05 1
[[email protected] data]$ hdfs dfs -ls /user/hive/warehouse/order_mulit_partition/event_month=2014-05
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 16:09 /user/hive/warehouse/order_mulit_partition/event_month=2014-05/step=1 
(此時order_mulit_partition是兩個分割槽。hdfs上面一個分割槽對應一個目錄)

小結:上面的單級分割槽/多級分割槽 ==> 統稱為靜態分割槽。(靜態分割槽指在指定分割槽段時候,一定要把寫全了,就是event_month step都要寫出來)

hive (default)> show create table ruoze_emp;(檢視當時建立ruoze_emp表的語句)
CREATE TABLE `ruoze_emp_partition`(`empno` int, `ename` string, `job` string,`mgr` int, `hiredate` string, `sal` double, `comm` double) partitioned by(`deptno` int) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
(**注**:分割槽的欄位不能出現在表的結構的欄位中。當用deptno作為分割槽欄位時候就把表結構中的deptno這一項去掉了)

6.靜態分割槽與動態分割槽
問題:請按照ruoze_emp表中的部門編號deptno欄位對該表進行分割槽,寫到分割槽表裡)
方法一:按照每個部門編號10、20、30分別寫到分割槽表裡面

hive (default)> insert into table ruoze_emp_partition PARTITION(deptno=10) select empno,ename,job,mgr,hiredate,sal,comm from ruoze_emp where deptno=10;

假設:有1000個deptno (那麼此時再按照方法一去單個新增不現實,這就是靜態分割槽的弊端)
方法二:採用動態分割槽的方法

hive (default)> insert overwrite table ruoze_emp_partition PARTITION(deptno)
select empno,ename,job,mgr,hiredate,sal,comm,deptno from ruoze_emp;
FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict
(報錯了,因為預設都是嚴格的靜態模式,按照控制檯給的提示,執行set hive.exec.dynamic.partition.mode=nonstrict)
hive (default)> set hive.exec.dynamic.partition.mode=nonstrict;(如果想要全域性使用的話就到hive site裡面去配置)

和方法一靜態分割槽的對比如下:注:1、在指定分割槽段PARTITION(deptno)這裡不給deptno賦具體值 2、把分割槽欄位deptno加在select語句的最後 3、最後就不用where指定ruoze_emp中的具體deptno部分編號)

hive (default)> select *from ruoze_emp_partition;
[[email protected] data]$ hdfs dfs -ls /user/hive/warehouse/ruoze_emp_partition
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 17:08 /user/hive/warehouse/ruoze_emp_partition/deptno=10
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 17:07 /user/hive/warehouse/ruoze_emp_partition/deptno=20
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 17:07 /user/hive/warehouse/ruoze_emp_partition/deptno=30
drwxr-xr-x   - hadoop supergroup          0 2018-11-09 17:07 /user/hive/warehouse/ruoze_emp_partition/deptno=__HIVE_DEFAULT_PARTITION__