1. 程式人生 > >Hive的DML操作資料的匯入和匯出

Hive的DML操作資料的匯入和匯出

Hive的DML資料操作

資料的匯入

向表中裝載資料(load)::

語法

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)into table:表示載入到哪張表
(5)student:表示具體的表
(6)overwrite:表示覆蓋表中已有資料,否則表示追加
(7)partition:表示上傳到指定分割槽

例項:

建立一張表

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

載入本地檔案到hive

hive (hive)> load data local inpath '/opt/datas/student.txt' into table student;

載入HDFS上的資料(去掉local即可)

hive (hive)> dfs -put /opt/datas/student.txt /user
hive (hive)> load data inpath /user/student.txt into table student;

載入資料覆蓋表中已有的資料(overwrite關鍵詞)

load data inpath '/user/student.txt' overwrite into table student;

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

建立一張分割槽表

hive (hive)> create table student2(id string, name string) partitioned by (month string) row format delimited fields terminated by '\t';

插入資料

hive (hive)> insert into table student2 partition(month='201811') values('100','zyd');

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

hive (hive)> insert overwrite table student2 partition(month='201708') select id,name from student where month='201811';

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

hive (hive)> from student2
           > insert overwrite table student2 partition(month='201812')
           > select id, name 
           > insert overwrite table student2 partition(month='201901')
           > select id,name;

查詢語句中建立表並載入資料(as關鍵字)

hive (hive)> create table student3  as select id, name from student2;

建立表時通過Location指定載入資料路徑(指定在hdfs上的位置)

create table student6(
id int, name string
)
row format delimited fields terminated by '\t'
location '/user/student.txt';

資料的匯出

Insert匯出

1)將查詢的結果匯出到本地

insert overwrite local directory '/opt/datas/export' select * from student;

但是匯出的資料格式

001^Azhangshan
1002^Alishi
1003^Azhaoliu
1001^Azhangshan
1002^Alishi
1003^Azhaoliu
  1. 將查詢的結果格式化匯出到本地

    insert overwrite local directory ‘/opt/datas/export/student1’
    row format delimited fields terminated by ‘\t’ collection items terminated by ‘\n’
    select * from student;

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

insert overwrite directory '/user/student'
row format delimited fields terminated by '\t' collection items terminated by '\n'
select * from student;
Hadoop命令匯出到本地
hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0  /opt/module/datas/export/student3.txt;
Hive Shell 命令匯出到本地
[[email protected] export]# hive -e 'select * from hive.student' > student.txt
Export匯出到HDFS上
hive (hive)> export table student to '/user/student2';
資料匯入 import資料到指定的hive表中(先用export匯出後,再將資料匯入)
hive (hive)> import table student2 partition(month='201512')from '/user/student2';
FAILED: SemanticException [Error 10006]: Partition not found  - Specified partition not found in import directory

匯出的資料必須要有分割槽,匯入的表分割槽不能存在

清除表中資料(Truncate)
hive (hive)> truncate table student;
OK
Time taken: 0.331 seconds
hive (hive)> select * from student;
OK
student.id	student.name

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