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中访问数据。
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
sqoop import \
--connect jdbc:oracle:thin:@IP:DATABASE\
--username USERNAME\
--password -P\
--table 大写表名 \
--columns "多列逗号隔开,不留空格" \
--where "过滤条件" \
--hcatalog-database 数据库 \
--hcatalog-table hive表\
--hcatalog-partition-keys 分区字段\
--hcatalog-partition-values 分区值 \
--fields-terminated-by '\t' \
--lines-terminated-by '\n' \
--split-by '切片字段' \
--as-parquetfile -m 3
分区表无法实现覆盖导入,非分区表可考虑覆盖导入的方式
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表 "参数指定数据库及表的方式据说可以实现,目前没有测试通,可以试试。
全表导出
增量导出