HiveDML数据操作之导入导出

文章详细介绍了如何使用Hive进行数据导入和导出,包括`LOADDATA`命令用于从本地或HDFS加载数据到Hive表,`INSERTOVERWRITE`语句用于导出数据到本地或HDFS,以及`EXPORTTABLE`命令将数据导出到HDFS上。还提到了不同场景下的具体参数使用,如`OVERWRITE`、`PARTITION`等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、数据导入

向表中加载数据Load
语法

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: 表示上传到指定的分区

案例实操

创建一张表

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

加载本地文件到hive

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

加载HDFS文件到Hive

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

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

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

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

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

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

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

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

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

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

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

二、数据导出

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

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

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

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

Hadoop命令导出到本地

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

Hive Shell 命令导出

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

export 导出到HDFS上

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