0%

HiveDML数据操作之导入导出

一、数据导入

向表中加载数据Load
语法

1
load data [local] inpath '/opt/module/datas/student.txt' [overwrite] | into table student [partition (partcol1=val1,...)];

load data:表示加载数据
local: 表示从本地数据中加载数据到Hive表中,否则是从HDFS中加载数据到Hive表
inpath: 表示加载数据的路径
overwrite: 表示覆盖表中的已有数据,否则表示追加
into table: 表示加载到哪张表
student: 表示表名
partition: 表示上传到指定的分区

案例实操

创建一张表

1
2
create table student(id string, name string)
row format fields terminated by '\t';

加载本地文件到hive

1
load data local inpath '/opt/module/datas/student.txt' into table default.student;

加载HDFS文件到Hive

1
load data inpath '/user/suiwo/hive/student.txt' into table default.student;

加载数据覆盖表中的已有的数据
上传文件到HDFS

1
dfs -put /opt/module/datas/student.txt /user/suiwo/hive;

加载数据覆盖表中已有的数据

1
load data inpath "/user/suiwo/hive/student.txt" overwrite into table default.student;

通过查询语句向表中插入数据

1
2
insert overwrite table student partition(month='201708')
select id, name from student where month='201709';

查询语句中创建表并加载数据

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

import数据到指定的Hive表中(此处导入的数据格式应该是和导出的数据格式相同)

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

二、数据导出

将查询的结果导出到本地(将local去掉便是将数据导出到HDFS)

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

将查询的结果格式化导出到本地

1
2
insert overwrite local directory '/opt/module/datas/export/student1' row format delimited dields terminated by '\t'
select * from student;

Hadoop命令导出到本地

1
2
dfs -get /user/hive/warehouse/student/month=2017/00000_0
/opt/module/datas/export/studemt3.txt

Hive Shell 命令导出

1
bin/hive -e 'select * from default.student;' > /opt/module/datas/export/student3.txt;

export 导出到HDFS上

1
export table default.student to '/user/hive/warehouse/export/student'