HIVE数据的导入与导出详解

本文详细讲解了HIVE数据的导入与导出,包括使用insert加载数据、通过location创建表、通过insert overwrite directory导出数据到本地或HDFS、查询结果重定向以及HIVE的import和export功能。在导入时要注意是否使用local和overwrite选项,导出时可选择格式化数据或直接保存查询结果。

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

1.导入

load data [local] inpath ''  [overwrite]  into database.table [partition(partcol=val)]

原始文件在linux本地 加上local 如果原始数据文件在hdfs 不用local

如果是覆盖数据加上overwrite 如果是追加 不要overwrite

如果是分区表加上partition,不是就不用了。

2.创建表通过insert 加载

create table db_hive.dept_copy like dept;
insert into table db_hive.dept_copy
select * from dept ;

3.创建表时通过location指定文件路径

 

2.导出

1.通过insert [overwrite] [local]  directory

导出数据到本地文件 会自动创建目录啦 注意这里的目录是一个文件夹

insert overwrite local directory '/opt/datas/hive_exp_dept' 
select * from db_hive.dept;

查看导出的数据

发现数据不太好看

格式化一下

insert overwrite local directory '/opt/datas/hive_exp_dept' 
row format delimited fields terminated by '\t'
collection items terminated by '\n'
select * from db_hive.dept;

再次查看

导出数据到hdfs 当然就是不要local啦

insert overwrite directory '/user/hive/warehouse/db_hive.db/dept_exp_hdfs' 
select * from db_hive.dept;

50070页面查看 

查看

将文件拿到本地

bin/hdfs dfs -get /user/hive/warehouse/db_hive.db/dept_exp_hdfs/0* /root/hive_datas/

 

2.重定向

将查询结果重定向到一个文件 这里是一个文件 上面有数据而不是文件夹

bin/hive -e "select * from db_hive.emp;" > /opt/datas/emp_exp_dept ;

3.hive的import和export

官方文档:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+ImportExport

import 导入数据到hive表中


IMPORT [[EXTERNAL] TABLE new_or_original_tablename [PARTITION (part_column="value"[, ...])]]
  FROM 'source_path'
  [LOCATION 'import_target_path']

export (注意这个target_path是hdfs的路径)


EXPORT TABLE tablename [PARTITION (part_column="value"[, ...])]
  TO 'export_target_path' [ FOR replication('eventid') ]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值