天天看點

【硬剛Hive】Hive基礎(六):Hive文法(2) DML(1) 資料操作(資料導入/資料導出)

歡迎關注部落格首頁:微信搜:import_bigdata,大資料領域硬核原創作者_王知無(import_bigdata)
【硬剛Hive】Hive基礎(六):Hive文法(2) DML(1) 資料操作(資料導入/資料導出)

本文是對《【硬剛大資料之學習路線篇】從零到大資料專家的學習指南(全面更新版)》的Hive部分補充。

1 資料導入

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/atguigu/hive;
           

加載 HDFS 上資料

hive (default)> load data inpath '/user/atguigu/hive/student.txt' into table 
default.student;           

(3)加載資料覆寫表中已有的資料

hive (default)> dfs -put /opt/module/datas/student.txt /user/atguigu/hive;           

加載資料覆寫表中已有的資料

hive (default)> load data inpath '/user/atguigu/hive/student.txt' overwrite into 
table default.student;           

1.2 通過查詢語句向表中插入資料(Insert)

1.建立一張表

hive (default)> create table student_par(id int, name string) row format 
delimited fields terminated by '\t';           

2.基本插入資料

hive (default)> insert into table student_par 
values(1,'wangwu'),(2,'zhaoliu');           

3.基本模式插入(根據單張表查詢結果)

hive (default)> insert overwrite table student_par
 select id, name from student where month='201709';           

insert into:以追加資料的方式插入到表或分區,原有資料不會删除

insert overwrite:會覆寫表或分區中已存在的資料

注意:insert 不支援插入部分字段

4.多表(多分區)插入模式(根據多張表查詢結果)

hive (default)> from student
 insert overwrite table student partition(month='201707')
 select id, name where month='201709'
 insert overwrite table student partition(month='201706')
 select id, name where month='201709';           

1.3 查詢語句中建立表并加載資料(As Select)

詳見 4.5.1 章建立表。

根據查詢結果建立表(查詢的結果會添加到新建立的表中)

create table if not exists student3
as select id, name from student;           

1.4 建立表時通過 Location 指定加載資料路徑

1.上傳資料到 hdfs 上

hive (default)> dfs -mkdir /student;
hive (default)> dfs -put /opt/module/datas/student.txt /student;           

2. 建立表,并指定在 hdfs 上的位置

hive (default)> create external table if not exists student5(
 id int, name string
 )
 row format delimited fields terminated by '\t'
 location '/student;           

3.查詢資料

hive (default)> select * from student5;           

1.5 Import 資料到指定 Hive 表中

注意:先用 export 導出後,再将資料導入。

hive (default)> import table student2 partition(month='201709') from
'/user/hive/warehouse/export/student';           

2 資料導出

2.1 Insert 導出

1.将查詢的結果導出到本地

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

2.将查詢的結果格式化導出到本地

hive(default)>insert overwrite local directory 
'/opt/module/datas/export/student1'
 ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' select * from 
student;           

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

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

2.2 Hadoop 指令導出到本地

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

2.3 Hive Shell 指令導出

基本文法:(hive -f/-e 執行語句或者腳本 > file)

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

2.4 Export 導出到 HDFS 上

export 和 import 主要用于兩個 Hadoop 平台叢集之間 Hive 表遷移。

2.5 Sqoop 導出

3 清除表中資料

注意:Truncate 隻能删除管理表,不能删除外部表中資料

hive (default)> truncate table student;           

繼續閱讀