Hive——DML数据操作(数据导入&数据导出)

1. 数据导入

1.1 向表中装载数据(Load)

语法

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

(1) load data:表示加载数据
(2) local:表示从本地加载数据到 hive 表;否则从 HDFS 加载数据到 hive 表
(3) inpath:表示加载数据的路径
(4) overwrite:表示覆盖表中已有数据, 否则表示追加
(5) into table:表示加载到哪张表(6) student:表示具体的表
(7) partition:表示上传到指定分区

实操案例:

创建一张表

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

加载数据方式一:加载本地文件到 hive

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

加载数据方式二:加载HDFS数据到hive

首先,将Linux系统的文件上传到HDFS根目录【可以自己选择路径】

hadoop fs -put /opt/module/data/student.txt /

之后,将上传到HDFS的文件上传到hive

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

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

使用格式:

insert into table new_table
select field1,field2,,, from old_table;

insert into :以追加数据的方式插入到表或分区,原有数据不会删除

insert overwritetable new_table
select field1,field2,,, from old_table;

insert overwrite :会覆盖表中已存在的数据
操作案例:

创建一张表:

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

基本模式插入(根据单张表查询结果)

insert overwrite table student_par                        	
select id, name from student where month='201709'; 

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

了解

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

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

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

使用Location指定路径时,数据不会存放在hive的默认路径

使用格式

create [external] table if not exists table_name(             		
field type, field type)                                                             	
row format delimited fields terminated by '\t'                	
location 'path';

准备工作:

(1)上传数据到 hdfs 上

hive (default)> dfs -mkdir /student;                                      
hive (default)> dfs -put /opt/module/datas/student.txt /student;         

(2)创建表,并指定在 hdfs 上的位置

create external table if not exists student5(             		
id int, name string)                                                             	
row format delimited fields terminated by '\t'                	
location '/student;  

(3) 查询数据

 select * from student5;  

2. 数据导出

2.1 Insert 导出

基本格式:

【直接使用案例体会语法】

insert overwrite [local] directory                          		
'path'                                    	
查询语句; 

将查询的结果导出到本地

insert overwrite local directory                          		
'/opt/module/hive/data/export/student'                                    	
select * from student; 

将查询的结果格式化导出到本地【在原来的基础上加一个导出格式限制】

insert overwrite local directory                            		'/opt/module/hive/data/export/student1'                                   
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'                            	
select * from student;      

将查询的结果导出到 HDFS 上(没有 local)

insert overwrite directory '/user/atguigu/student2'       	
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'                 		
select * from student;

2.2 Hadoop 命令导出到本地

【需要指明导出的具体文件路径】

dfs -get /user/hive/warehouse/student/student.txt         
/opt/module/data/export/student3.txt;

2.3 Hive Shell 命令导出

基本语法:(hive -f/-e 执行语句或者脚本 > file)

来到hive的根目录下操作:

cd /opt/module/hive-3.1.2
bin/hive -e 'select * from default.student;' >  		
/opt/module/hive/data/export/student4.txt

提示>表示追加,>>表示覆盖

2.4 Export 导出到 HDFS 上 & Import 数据到指定 Hive 表中

使用语法:

export table table_name
to 'path';
import table table_name
from 'path';

先用 export 导出后,再将数据导入。

导入

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

导出:

export table student                               
to '/user/hive/warehouse/export/student';          

使用场景
export 和 import 主要用于两个 Hadoop 平台集群之间 Hive 表迁移。

清除表中数据(Truncate):
Truncate 只能删除管理表,不能删除外部表中数据

truncate table table_name; 
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮皮皮皮皮皮卡乒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值