Hive Shell指令——数据导入方法、数据导出方法详解

一、Hive数据导入方法 ——六条

1. 使用 load data 导入

注意:此语句为剪切作用,将HDFS原数据剪切到hive表格指定存储路径。

load data [local] inpath '/opt/module/datas/student.txt' [overwrite] into table 
default.student [partition (partcol1=val1,…)];
字段说明
(1)load data表示加载数据
(2)local表示从本地加载数据到hive表;否则从HDFS加载数据到hive表
(3)inpath表示加载数据的路径
(4)overwrite表示覆盖表中已有数据,否则表示追加
(5)into table表示加载到哪张表
(6)default.studentdefault表示默认数据库,student表示具体的表
(7)partition表示上传到指定分区

2.使用 Insert into / overwrite 导入

insert into table  student partition(month='201709') 
values(1,'wangwu'),(2,’zhaoliu’);

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

 insert overwrite table student partition(month='201708') select * from student;

insert overwrite:会覆盖表或分区中已存在的数据

3. 使用 As select 导入

通过查询语句往表格中导入数据。

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

此方法与insert结合,常用于数据仓库ADS(数据应用层)、DWS(服务数据层)、DWD(明细数据层)数据导入。

insert overwrite table dws_user_retention_day partition(dt="2010-01-01")
select
    mid_id,
    user_id , 
from dws_uv_detail_day ud;

4. 使用Location导入

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

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

5. 使用Import导入

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

先用export导出,再用import导入

6. 使用Sqoop导入

--connect jdbc:mysql://127.0.0.1:3306/company \
--username root \
--password 000000 \
--table student \
--num-mappers 1 \
--hive-import \
--fields-terminated-by "\t" \
--hive-overwrite \
--hive-table student_hive

字段说明
–connect连接关系型数据库的URL
–username连接数据库的用户名
–password连接数据库的密码
–table指定关系数据库的表名
–m或–num-mappers启动N个map来并行导入数据,默认4个
–hive-import将数据从关系数据库中导入到hive表中
–fields-terminated-by分隔符
–hive-overwrite覆盖掉在hive表中已经存在的数据
–hive-table创建的hive表,默认使用MySQL的表名

二、Hive数据导出方法——五条

1. 使用 Insert overwrite 导出

1)将查询结果导出到HDFS。如添加 local,表示导出到本地目录

insert overwrite [local] directory '/user/lmzs/student' 
[ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'] 
select * from student;

2)将查询结果格式化导出到HDFS,ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’ ,表示以 ‘\t’ 作为列的分隔符写入到HDFS文件

insert overwrite [local] directory '/opt/module/datas/export/student' 
[ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'] 
select * from student;

2. 使用 Hadoop 命令导出到本地

在Hive客户端执行Hadoop指令

hive (default)> dfs -get /user/hive/warehouse/student/month=201709/000000_0
	/opt/module/datas/export/student.txt;

3. 使用 Hive Shell 命令导出

在控制台执行命令

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

4. 使用 Export 导出到HDFS上

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

(defahiveult)> export table default.student to
 '/user/hive/warehouse/export/student';

5. 使用 Sqoop 导出

使用sqoop将Hive表格数据导出到RDBMS(关系型数据库),代码以mysql为例。
注意:需提前在mysql中创建好表格。

$ bin/sqoop export \
--connect jdbc:mysql://hadoop102:3306/company \
--username root \
--password 000000 \
--table staff \
--num-mappers 1 \
--export-dir /user/hive/warehouse/staff_hive \
--input-fields-terminated-by "\t"
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值