Hive基礎六---DML數據操作語言

版權聲明:本文爲博主原創文章,轉載請註明出處。

交流QQ: 824203453

歡迎訪問博主個人主頁:http://www.oldsheep.cn 

 

DML數據操作

數據導入

向表中裝載數據(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/hive/warehouse;

加載HDFS上數據

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

       (3)加載數據覆蓋表中已有的數據

       上傳文件到HDFS

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

加載數據覆蓋表中已有的數據

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

通過查詢語句向表中插入數據(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='201709') values(1,'wangwu');

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

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

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

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';

查詢語句中創建表並加載數據(As Select)

詳見4.5.1章創建表。

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

create table if not exists student3

as select id, name from student;

創建表時通過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;

Import數據到指定Hive表中

注意:先用export導出後,再將數據導入。

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

數據導出

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;

Hadoop命令導出到本地

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

Hive Shell 命令導出

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

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

Export導出到HDFS

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

Sqoop導出

清除表中數據(Truncate)

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

hive (default)> truncate table student;

 

 

版權聲明:本文爲博主原創文章,轉載請註明出處。

交流QQ: 824203453

歡迎訪問博主個人主頁:http://www.oldsheep.cn 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章