Hive-表数据的导出、导入(HDFS、本地)

表数据的导出、导入(HDFS、本地)

数据导入

1 向表中装载数据(Load)

1)语法

load data [local] 
inpath '数据的path' 
[overwrite] into table student [partition (partcol1=val1,)];

(1)load data:表示加载数据

(2)local:表示从本地加载数据到hive表;否则从HDFS加载数据到hive表

(加local是从本地复制过去,不加local是从hdfs上剪切过去)

(3)inpath:表示加载数据的路径

(4)overwrite:表示覆盖表中已有数据,否则表示追加

(overwrite会把之前的数据文件删除,在把新的数据文件传上去)

(5)into table:表示加载到哪张表

(6)student:表示具体的表

(7)partition:表示上传到指定分区

2)实操案例

(1)加载本地文件到hive

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

(2)加载HDFS文件到hive中

# 上传文件到HDFS
hadoop fs -put /opt/module/hive/datas/student.txt /user/qinjl;

# 加载HDFS上数据,导入完成后去hdfs查看文件是否还存在
load data inpath '/user/qinjl/student.txt' into table student;

(3)加载数据覆盖表中已有的数据

# 上传文件到HDFS
hdfs -put /opt/module/hive/datas/student.txt /user/qinjl;

# 加载数据覆盖表中已有的数据
load data inpath '/user/qinjl/student.txt' overwrite into table student;

2 通过查询语句向表中插入数据(Insert)

1)创建一张表

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

2)基本模式插入数据

insert into table student2 values(1,'wangwu'),(2,'zhaoliu');

3)根据查询结果插入数据( 插入select的表,的字段、类型要匹配,否则报错

insert overwrite table student2 
	select id, name from student where id < 1006;
  • insert into:以追加数据的方式插入到表或分区,原有数据不会删除

  • insert overwrite:会覆盖表中已存在的数据

  • 注意:insert不支持插入部分字段,并且后边跟select语句时,select之前不能加as,加了as会报错,一定要跟下面的as select区分开。

3 查询语句中创建表并加载数据(As Select)

根据查询结果创建表(查询的结果会添加到新创建的表中

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

4 创建表时通过Location指定加载数据路径

# 创建表,并指定在hdfs上的位置
create external table if not exists student5(
	id int, name string
)
row format delimited fields terminated by '\t'
location '/student';

5 Import数据到指定Hive表中

  • 注意:先用export导出后,再将数据导入。并且因为export导出的数据里面包含了元数据,因此import要导入的表不可以存在,否则报错
import table student2  from '/user/hive/warehouse/export/student';

数据导出

1 Insert导出

# 1)将查询的结果导出到本地(只能overwrite,不能into,否则会报错)
insert overwrite local directory '/opt/module/hive/datas/export/student' 
	select * from student;

# 2)将查询的结果格式化导出到本地(所有的insert语句都会跑MR)
insert overwrite local directory '/opt/module/hive/datas/export/student1' 
	ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
    select * from student;

# 3)将查询的结果导出到HDFS上(没有local)(是复制,原来的文件还在)
insert overwrite directory '/user/qinjl/student2'
	ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' 
	select * from student;

注意:insert 导出,导出的目录不用自己提前创建,hive会帮我们自动创建,但是由于是overwrite,所以导出路径一定要写具体,否则很可能会误删数据。

2 Hadoop命令导出到本地

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

3 Hive Shell 命令导出

  • 基本语法:(hive -f/-e 执行语句或者脚本 >> file)
[qinjl@hadoop102 hive]$ bin/hive -e 'select * from default.student;' >>
 /opt/module/hive/datas/export/student4.txt;

4 Export导出到HDFS上

  • 注意:export导出的数据,里面包含了具体数据和元数据
hive (default)> export table default.student to
 '/user/hive/warehouse/export/student';
  • export和import主要用于两个Hadoop平台集群之间Hive表迁移,不能直接导出的本地

猜你喜欢

转载自blog.csdn.net/qq_32727095/article/details/107752617