1. 程式人生 > 其它 >【hadoop生態之Hive】Hive的DML資料操縱語言【筆記+程式碼】

【hadoop生態之Hive】Hive的DML資料操縱語言【筆記+程式碼】

技術標籤:hadoop生態之Hivejavahivehdfshadoop大資料

五、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/itstar/hive; 載入HDFS上資料 hive (default)>load data inpath '/user/itstar/hive/student.txt' into table default.student;3)載入資料覆蓋表中已有的資料 上傳檔案到HDFS hive (default)> dfs -put /opt/module/datas/student.txt /user
/itstar/hive; 載入資料覆蓋表中已有的資料 hive (default)>load data inpath '/user/itstar/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='201809')  values(1,'wangwu');  

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

hive (default)> insert overwrite table student partition(month='201808')

       select id, name from student where month='201809';

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

hive (default)> from student

       insert overwrite table student partition(month='201807')

       select id, name where month='201809'

       insert overwrite table student partition(month='201806')

       select id, name where month='201809';

5.1.3 查詢語句中建立表並載入資料(As Select)

詳見4.5.1章建立表。

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

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

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匯出後,再將資料匯入。同在HDFS上是Copy級操作

  hive (default)> export table default.student to  '/user/hive/warehouse/export/student';  

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-Fe1fUfcm-1607930911177)(file:///C:/Users/18451/AppData/Local/Temp/msohtmlclip1/01/clip_image002.gif)]

  hive (default)> import table student2  partition(month='201809') 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;  

[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-SXqqrtaX-1607930911180)(file:///C:/Users/18451/AppData/Local/Temp/msohtmlclip1/01/clip_image004.gif)]

2)將查詢的結果格式化匯出到本地,資料之間"\t"間隔

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/itstar/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)

$ 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.2.5 Sqoop匯出

5.3 清除表中資料(Truncate)

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

hive (default)> truncate table student;