歡迎訪問我的GitHub
https://github.com/zq2599/blog_demos
內容:所有原創文章分類匯總及配套源碼,涉及Java、Docker、Kubernetes、DevOPS等;
《hive學習筆記》系列導航
本篇概覽
本文是《hive學習筆記》系列的第四篇,要學習的是hive的分區表,簡單來說hive的分區就是創建層級目錄的一種方式,處於同一分區的記錄其實就是數據在同一個子目錄下,分區一共有兩種:靜態和動態,接下來逐一嘗試;
靜態分區(單字段分區)
先嘗試用單個字段分區,t9表有三個字段:名稱city、年齡age、城市city,以城市作為分區字段:
- 建表:
create table t9 (name string, age int)
partitioned by (city string)
row format delimited
fields terminated by ',';
- 查看:
hive> desc t9;
OK
name string
age int
city string
# Partition Information
# col_name data_type comment
city string
Time taken: 0.159 seconds, Fetched: 8 row(s)
- 創建名為009.txt的文本文件,內容如下,可見每行只有name和age兩個字段,用來分區的city字段不在這里設置,而是在執行導入命令的時候設置,稍后就會見到:
tom,11
jerry,12
- 導入數據的命令如下,可見導入命令中制定了city字段,也就是說一次導入的所有數據,city字段值都是同一個:
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t9
partition(city='shenzhen');
- 再執行一次導入操作,命令如下,city的值從前面的shenzhen改為guangzhou:
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t9
partition(city='guangzhou');
- 查詢數據,可見一共四條數據,city共有兩個值:
hive> select * from t9;
OK
t9.name t9.age t9.city
tom 11 guangzhou
jerry 12 guangzhou
tom 11 shenzhen
jerry 12 shenzhen
Time taken: 0.104 seconds, Fetched: 4 row(s)
- 前面曾提到分區實際上是不同的子目錄,來看一下是不是如此,如下圖,紅框是t9的文件目錄,下面有兩個子目錄city=guangzhou和city=shenzhen:
- 查看子目錄里面文件的內容,可見每條記錄只有name和age兩個字段:
[hadoop@node0 bin]$ ./hadoop fs -ls /user/hive/warehouse/t9/city=guangzhou
Found 1 items
-rwxr-xr-x 3 hadoop supergroup 16 2020-10-31 16:47 /user/hive/warehouse/t9/city=guangzhou/009.txt
[hadoop@node0 bin]$ ./hadoop fs -cat /user/hive/warehouse/t9/city=guangzhou/009.txt
tom,11
jerry,12
[hadoop@node0 bin]$
以上就是以單個字段做靜態分區的實踐,接下來嘗試多字段分區;
靜態分區(多字段分區)
- 新建名為t10的表,有兩個分區字段:province和city,建表語句:
create table t10 (name string, age int)
partitioned by (province string, city string)
row format delimited
fields terminated by ',';
- 上述建表語句中,分區字段province寫在了city前面,這就意味着第一級子目錄是province值,每個province子目錄,再按照city值建立二級子目錄,圖示如下:
3. 第一次導入,province='shanxi', city='xian':
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t10
partition(province='shanxi', city='xian');
- 第二次導入,province='shanxi', city='xian':
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t10
partition(province='shanxi', city='hanzhong');
- 第三次導入,province='guangdong', city='guangzhou':
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t10
partition(province='guangdong', city='guangzhou');
- 第四次導入,province='guangdong', city='shenzhen':
load data
local inpath '/home/hadoop/temp/202010/25/009.txt'
into table t10
partition(province='guangdong', city='shenzhen');
- 全部數據如下:
hive> select * from t10;
OK
t10.name t10.age t10.province t10.city
tom 11 guangdong guangzhou
jerry 12 guangdong guangzhou
tom 11 guangdong shenzhen
jerry 12 guangdong shenzhen
tom 11 shanxi hanzhong
jerry 12 shanxi hanzhong
tom 11 shanxi xian
jerry 12 shanxi xian
Time taken: 0.129 seconds, Fetched: 8 row(s)
- 查看hdfs文件夾,如下圖,一級目錄是province字段的值:
- 打開一個一級目錄,如下圖,可見二級目錄是city的值:
10. 查看數據:
[hadoop@node0 bin]$ ./hadoop fs -cat /user/hive/warehouse/t10/province=shanxi/city=hanzhong/009.txt
tom,11
jerry,12
- 以上就是靜態分區的基本操作,可見靜態分區有個不便之處:新增數據的時候要針對每一個分區單獨使用load命令去操作,這時候使用動態分區來解決這個麻煩;
動態分區
- 動態分區的特點就是不用指定分區目錄,由hive自己選擇;
- 執行以下命令開啟動態分區功能:
set hive.exec.dynamic.partition=true
- 名為hive.exec.dynamic.partition.mode的屬性,默認值是strict,意思是不允許分區列全部是動態的,這里改為nostrict以取消此禁制,允許全部分區都是動態分區:
set hive.exec.dynamic.partition.mode=nostrict;
- 建一個外部表,名為t11,只有四個字段:
create external table t11 (name string, age int, province string, city string)
row format delimited
fields terminated by ','
location '/data/external_t11';
- 創建名為011.txt的文件,內容如下:
tom,11,guangdong,guangzhou
jerry,12,guangdong,shenzhen
tony,13,shanxi,xian
john,14,shanxi,hanzhong
- 將011.txt中的四條記錄載入表t11:
load data
local inpath '/home/hadoop/temp/202010/25/011.txt'
into table t11;
- 接下來要,先創建動態分區表t12,再把t11表的數據添加到t12中;
- t12的建表語句如下,按照province+city分區:
create table t12 (name string, age int)
partitioned by (province string, city string)
row format delimited
fields terminated by ',';
- 執行以下操作,即可將t11的所有數據寫入動態分區表t12,注意,要用overwrite:
insert overwrite table t12
partition(province, city)
select name, age, province, city from t11;
- 通過hdfs查看文件夾,可見一級和二級子目錄都符合預期:
11. 最后檢查二級子目錄下的數據文件,可以看到該分區下的記錄:
[hadoop@node0 bin]$ ./hadoop fs -cat /user/hive/warehouse/t12/province=guangdong/city=guangzhou/000000_0
tom,11
至此,分區表的學習就完成了,希望能給您一些參考;
你不孤單,欣宸原創一路相伴
歡迎關注公眾號:程序員欣宸
微信搜索「程序員欣宸」,我是欣宸,期待與您一同暢游Java世界...
https://github.com/zq2599/blog_demos