095-BigData-23Hive分區及DML操作

上一篇:094-BigData-22Hive數據類型及操作

一、Hive分區

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

1、分區表基本操作

1)引入分區表(需要根據日期對日志進行管理)
/user/hive/warehouse/log_partition/20180702/20180702.log
/user/hive/warehouse/log_partition/20180703/20180703.log
/user/hive/warehouse/log_partition/20180704/20180704.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/mod/dept.txt' into table default.dept_partition partition(month='201907');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201908');
hive (default)> load data local inpath '/opt/mod/dept.txt' into table default.dept_partition partition(month='201909');
4)查詢分區表中數據
單分區查詢
hive (default)> select * from dept_partition where month='201907';
多分區聯合查詢
hive (default)> select * from dept_partition where month='201908'
union
select * from dept_partition where month='201907'
union
select * from dept_partition where month='201909';

_u3.deptno _u3.dname _u3.loc _u3.month
10 ACCOUNTING NEW YORK 201807
10 ACCOUNTING NEW YORK 201808
10 ACCOUNTING NEW YORK 201809
20 RESEARCH DALLAS 201807
20 RESEARCH DALLAS 201808
20 RESEARCH DALLAS 201809
30 SALES CHICAGO 201807
30 SALES CHICAGO 201808
30 SALES CHICAGO 201809
40 OPERATIONS BOSTON 201807
40 OPERATIONS BOSTON 201808
40 OPERATIONS BOSTON 201809
5)增加分區
創建單個分區
hive (default)> alter table dept_partition add partition(month='201806') ;
同時創建多個分區
hive (default)> alter table dept_partition add partition(month='201805') partition(month='201804');
注:增加多個分區之間用空格" "隔開,刪除多個分區用","隔開
6)刪除分區
刪除單個分區
hive (default)> alter table dept_partition drop partition (month='201804');
同時刪除多個分區
hive (default)> alter table dept_partition drop partition (month='201805'), partition (month='201806');
7)查看分區表有多少分區
hive>show partitions dept_partition;
8)查看分區表結構
hive>desc formatted dept_partition;

# Partition Information          
# col_name              data_type               comment             
month                   string    

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='201809', day='13');
(2)查詢分區數據
hive (default)> select * from dept_partition2 where month='201809' and day='13';
3)把數據直接上傳到分區目錄上,讓分區表和數據產生關聯的兩種方式
(1)方式一:上傳數據后修復
上傳數據
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=12;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=12;
查詢數據(查詢不到剛上傳的數據)
hive (default)> select * from dept_partition2 where month='201809' and day='12';
執行修復命令
hive>msck repair table dept_partition2;
再次查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='12';
(2)方式二:上傳數據后添加分區
上傳數據
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=11;
hive (default)> dfs -put /opt/module/datas/dept.txt /user/hive/warehouse/dept_partition2/month=201809/day=11;
執行添加分區
hive (default)> alter table dept_partition2 add partition(month='201809', day='11');
查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='11';
(3)方式三:上傳數據后load數據到分區
創建目錄
hive (default)> dfs -mkdir -p /user/hive/warehouse/dept_partition2/month=201809/day=10;
上傳數據
hive (default)> load data local inpath '/opt/module/datas/dept.txt' into table dept_partition2 partition(month='201809',day='10');
查詢數據
hive (default)> select * from dept_partition2 where month='201809' and day='10';

五、DML數據操作

續上一篇,多以是五~

5.1 數據導入

5.1.1 向表中裝載數據(Load)
1)語法
hive>load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table student [partition (partcol1=val1,…)];
(1)load data:表示加載數據
(2)local:表示從本地加載數據到hive表;否則從HDFS加載數據到hive表
(3)inpath:表示加載數據的路徑
(4)overwrite:表示覆蓋表中已有數據,否則表示追加
(5)into table:表示加載到哪張表
(6)student:表示具體的表名
(7)partition:表示上傳到指定分區
2)實操案例
(0)創建一張表
hive (default)> create table student(id string, name string) row format delimited fields terminated by '\t';
(1)加載本地文件到hive
hive (default)> load data local inpath '/opt/module/datas/student.txt' into table default.student;
(2)加載HDFS文件到hive中
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加載HDFS上數據
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' into table default.student;
(3)加載數據覆蓋表中已有的數據
上傳文件到HDFS
hive (default)> dfs -put /opt/module/datas/student.txt /user/AncientMing/hive;
加載數據覆蓋表中已有的數據
hive (default)>load data inpath '/user/AncientMing/hive/student.txt' overwrite into table default.student;
注:load hdfs的數據相當于mv文件到另一個目錄中,原目錄文件消失

5.1.2 通過查詢語句向表中插入數據(Insert)
1)創建一張分區表
hive (default)> create table student(id int, name string) partitioned by (month string) row format delimited fields terminated by '\t';
2)基本插入數據

hive (default)> insert into table  student partition(month='201907') values(1,'wangwu');

3)基本模式插入(根據單張表查詢結果)
hive (default)> insert overwrite table student partition(month='201908')
select id, name from student where month='201907';
4)多插入模式(根據多張表查詢結果)
hive (default)> from student
insert overwrite table student partition(month='201909')
select id, name where month='201907'
insert overwrite table student partition(month='201910')
select id, name where month='201908';

5.1.3 查詢語句中創建表并加載數據(As Select)
詳見4.5.1章創建表。
根據查詢結果創建表(查詢的結果會添加到新創建的表中)

create table if not exists student3
as select id, name from student;
這種方式不能創建外部表。
 external
CREATE-TABLE-AS-SELECT cannot create external table

5.1.4 創建表時通過Location指定加載數據路徑
1)創建表,并指定在hdfs上的位置
hive (default)> create table if not exists student5(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/hive/warehouse/student5';
2)上傳數據到hdfs上
hive (default)> dfs -put /opt/module/datas/student.txt /user/hive/warehouse/student5;
3)查詢數據
hive (default)> select * from student5;
5.1.5 Import數據到指定Hive表中
注意:先用export導出后(導出的數據目錄里面附帶有元數據),再import數據導入。同在HDFS上是Copy級操作

hive (default)> export table default.student to '/user/hive/warehouse/export/student';
hive (default)>create table student5(
           >                id int, name string
           >                )
           >                partitioned by (month string)
           >                row format delimited fields terminated by '\t';
hive (default)> import table student5 from '/user/hive/warehouse/export/student';
5.2 數據導出

5.2.1 Insert導出
1)將查詢的結果導出到本地,數據之間無間隔

hive (default)> insert overwrite local directory '/opt/module/datas/export/student'
            select * from student;

2)將查詢的結果格式化導出到本地,數據之間"\t"間隔

hive (default)> insert overwrite local directory '/root/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'             select * from student;

3)將查詢的結果導出到HDFS上(沒有local)

hive (default)> insert overwrite directory '/user/AncientMing/student2'
             ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
             select * from student;

注:雖然同是HDFS,但不是copy操作

5.2.2 Hadoop命令導出到本地

hive (default)> dfs -get /user/hive/warehouse/student/month=201809/000000_0  /opt/module/datas/export/student3.txt;

5.2.3 Hive Shell 命令導出
基本語法:(hive -f/-e 執行語句或者腳本 > file(自己創建))

[AncientMing@bigdata111hive]$ bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student4.txt;

5.2.4 Export導出到HDFS上

hive (default)> export table default.student to '/user/hive/warehouse/export/student';
5.3 清除表中數據(Truncate)

注意:Truncate只能刪除管理表,不能刪除外部表中數據

hive (default)> truncate table student;

下一篇:096-BigData-24Hive查詢排序分桶

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,818評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,185評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,656評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,647評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,446評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,951評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,041評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,189評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,718評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,602評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,800評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,316評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,045評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,419評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,671評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,420評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,755評論 2 371

推薦閱讀更多精彩內容