HiveQL的DML操作(二) 資料匯出

2021-09-11 13:55:18 字數 3502 閱讀 5747

本篇整理從hive表中匯出資料的各種操作,包括將hive表匯出到本地、hdfs和mysql三種目標路徑。

資料操縱語言(data manipulation language, dml)。

目錄

1. insert overwrite匯出到本地/hdfs

(1)將表查詢結果匯出到本地檔案

(2)將表查詢結果格式化匯出到本地檔案

(3)將表查詢結構格式化匯出到hdfs中

2. hadoop命令匯出到本地 dfs -get hdfspath1 linuxpath2

3. shell命令匯出到本地 >

4. export匯出到hdfs export table ~ to 'path'

5. sqoop匯出到mysql

將hive表的查詢結果匯出到linux本地或hdfs中,與insert匯入資料的區別:

匯入資料:insert into table ~ partition (~ = '~')values (~,'~'),(~,'~')...;

insert overwrite table ~ partition (~ = '~')select ~,~ from ~ where ~ = '~';

load data (local) inpath 'path' into table name;

匯出資料:

insert overwrite (local)directory 'path' row format delimited fields terminated by '\t' select ~,~ from ~;

hive (hive_db1)> insert overwrite local directory '/opt/module/datas/export/student'

> select * from student;

[kevin@hadoop100 student]$ pwd

/opt/module/datas/export/student

[kevin@hadoop100 student]$ cat 000000_0

101kevin

102john

103daniel

104lee

hive (hive_db1)> insert overwrite local directory '/opt/module/datas/export/student1'

> row format delimited fields terminated by '\t'

> select * from student;

[kevin@hadoop100 student1]$ pwd

/opt/module/datas/export/student1

[kevin@hadoop100 student1]$ cat 000000_0

101 kevin

102 john

103 daniel

104 lee

hive (hive_db1)> insert overwrite directory '/user/kevin/student1'

> row format delimited fields terminated by '\t'

> select * from student;

將hive在hdfs上的表檔案,匯出到linux本地,也相當於從hive表匯出到本地txt檔案中

hive (hive_db1)> dfs -get /user/hive/warehouse/staff_hive/part-m-00000 /opt/module/datas/export/student2.txt;

[kevin@hadoop100 export]$ cat student2.txt

1 kevin male

2 john male

使用》重定向

[kevin@hadoop100 hive]$ bin/hive -e 'select * from hive_db1.student;' > /opt/module/datas/export/student3.txt;

[kevin@hadoop100 export]$ cat student3.txt

student.id student.name

101 kevin

102 john

103 daniel

104 lee

hive (hive_db1)> show partitions student2;

okpartition

month=1

month=10

month=11

month=2

hive (hive_db1)> export table student2 to '/user/hive/warehouse/export/student';

結果如下

將hive表資料匯出到mysql等關係型資料庫中

sqoop的import和export操作

1. 首先在mysql中建立表hive_staff

mysql> create table company.hive_staff(id int(4) primary key not null auto_increment, name varchar(255), *** varchar(255));

query ok, 0 rows affected (0.02 sec)

2. 然後export

bin/sqoop export \

--connect jdbc:mysql://hadoop100:3306/company \

--username root \

--password 000000 \

--table hive_staff \

--export-dir /user/hive/warehouse/staff_hive \

--input-fields-terminated-by "\t"

3. 結果查詢

mysql> select * from hive_staff;

+----+-------+------+

| id | name | *** |

+----+-------+------+

| 1 | kevin | male |

| 2 | john | male |

+----+-------+------+

DML資料操作

示例 hive本地檔案系統匯入資料到hdfs的hive表hive load data local inpath data st.txt into table st hive select from st hdfs檔案匯入到hdfs的hive表 hdfs有檔案 hive load data inpat...

Hive 5 HiveQL 資料操作

5.1 向管理表中裝載資料 hive 沒有行級別的資料插入更新和刪除操作,那麼往表中裝載資料的唯一途徑就是使用一種 大量 的資料裝載操作,或者通過其他方式僅僅將檔案寫入到正確的目錄下 load data local inpath califonia employees overwrite inot ...

DML 資料操作語言

本小白日常oracle學習總結,若有錯誤望海涵,並希望大神能指點迷津 開發中使用的部分 主要指資料庫的查詢與更新 例如 select,update,查詢該使用者下的所有表 select from tab查詢某乙個表的表結構 desc 表名 select子句中可以直接使用四則運算 select子句對應...