Hive数据导入方法、数据导出方法总结
一、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.student | default表示默认数据库,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"