【Hive-基础】表数据的导出、导入(HDFS、本地)

1)数据导入

1.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;

1.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 区分开。

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

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

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

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

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

1.5.Import 数据到指定 Hive 表中

注意:先用export导出后,再将数据导入。并且因为export导出的数据里面包含了元数据因此import要导入的表不可以存在,否则报错。

import table student2  from '/user/hive/warehouse/export/student';

2)数据导出

2.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.2.Hadoop 命令导出到本地

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

2.3.Hive Shell 命令导出

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

2.4.Export 导出到 HDFS 上

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

2.5.Sqoop 导出

详情请参考:【Sqoop-命令】Sqoop 相关了解及命令

相关推荐

  1. Hive-基础数据导出导入HDFS本地

    2024-01-20 11:14:02       30 阅读
  2. Hive数据导出四种方法

    2024-01-20 11:14:02       33 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-20 11:14:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-20 11:14:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-20 11:14:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-20 11:14:02       20 阅读

热门阅读

  1. Chrome Devtools 调试指南

    2024-01-20 11:14:02       34 阅读
  2. ORACLE 11G 内置库详解

    2024-01-20 11:14:02       34 阅读
  3. LSTM 模型“实现”整数相加运算

    2024-01-20 11:14:02       31 阅读
  4. Unity中的协程

    2024-01-20 11:14:02       34 阅读
  5. 什么是CSS Sprite,以及如何在页面或网站中使用它

    2024-01-20 11:14:02       28 阅读
  6. vue解决部署文件缓存方式

    2024-01-20 11:14:02       32 阅读