sqoop导入导出hive/oracle

版本:

sqoop1最新版用户指导:http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.6-cdh5.9.3/SqoopUserGuide.html

1.sqoop从关系型数据库oracle导数据到hive,数据存储为parquet格式。

首先,在hive中建表,按月增量存储的建分区表。存储为parquet文件格式而不是默认的textfile是为了便于在impala中访问数据。

1
2
3
4
5
6
7
create table if not exists table_name(columns  string/double/ , ..)
comment  "备注"
partition by (month string)
 row format delimited
fields terminated by '\t'
lines terminated by '\n'
stroed as parquet;

用sqoop导数据到hive分区表,Sqoop 1.4.6 导入实战参考示例  http://www.sohu.com/a/149043703_572440

1
  sqoop import \<br>--connect jdbc:oracle:thin:@IP:DATABASE\<br>--username USERNAME\<br>--password -P\<br> --table 大写表名 \<br> --columns "多列逗号隔开,不留空格" \<br> --where "过滤条件" \<br> --hcatalog-database 数据库 \<br> --hcatalog-table hive表\<br> --hcatalog-partition-keys 分区字段\<br> --hcatalog-partition-values 分区值 \<br>--fields-terminated-by '\t' \<br>--lines-terminated-by '\n' \<br> --split-by '切片字段'  \<br> --as-parquetfile -m 3

分区表无法实现覆盖导入,非分区表可考虑覆盖导入的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sqoop job --create job名 \
-- import \
--connect jdbc:oracle:thin:@ip:1521:数据库 \
--username username\
--password -P\
--hive-import \
 --query "SELECT 多列, \
多列 \
FROM pec.sku_no where "过滤条件 AND \$CONDITIONS" \
--fields-terminated-by '\t' \
--lines-terminated-by '\n' \
--as-parquetfile \
--hive-overwrite \
--hive-database hive数据库\
--hive-table hive表名\
--delete-target-dir \
--target-dir /hive/warehouse/hive数据库.db/hive表名 \
--map-column-java "部分需要转换类型的列" \
--split-by 切片字段" \
-m 1 \
--fetch-size 5000

 

hive中检查数据导入是否成功。

熟悉这些语句后可建批处理脚本,简化批量操作:

 

 

2.qoop export工具被用来将文件集合从HDFS中导入到关系型数据库中。从hive导数据到oracle数据库(exports a set of files from HDFS back to an RDBMS)参考示例 https://blog.csdn.net/waterkong/article/details/78708809

第一步:将hive表数据转到hdfs文件系统,已存储在HDFS文件系统的不用再处理。

查询hive表所在文件系统路径:show create table table_name;

第二部:sqoop导出到oracle数据库,目标表必须事先已经在数据库中创建好。注意parquet格式的hive表无法导出到关系型数据库,通过“--hcatalog-database 数据库 --hcatalog-table hive表 "参数指定数据库及表的方式据说可以实现,目前没有测试通,可以试试。

全表导出

 

增量导出

posted on   lanshure  阅读(925)  评论(0编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示