Hadoop实战(10)_Sqoop import与抽取框架封装

来源:互联网 发布:下载刷枪软件 编辑:程序博客网 时间:2024/06/05 02:14

CDH Hadoop系列目录:

Hadoop实战(3)_虚拟机搭建CDH的全分布模式

Hadoop实战(4)_Hadoop的集群管理和资源分配

Hadoop实战(5)_Hadoop的运维经验

Hadoop实战(8)_CDH添加Hive服务及Hive基础

Hadoop实战(9)_Hive进阶及UDF开发

Sqoop语法说明

Sqoop官方学习文档:http://archive.cloudera.com/cdh5/cdh/5/sqoop-1.4.6-cdh5.9.0/

Sqoop import是相对于HDFS来讲,即从关系数据库import到HDFS上。

mysql的驱动包放到sqoop/lib下。

案例一:把数据导入到HDFS上

/root/projectmkdir sqoop_prjcd sqoop_prj/mkdir DBScd DBS/touch DBS.opthadoop fs -mkdir /user/hive/warehouse/DBSwhich sqoop

执行opt文件,不能传参,sqoop --options-file aa.opt-m,指定map数,如果抽取的表数据量大,则调大map数。如果-m设置为5,5个线程,则在HDFS上产生5个文件。

把sqoop写到shell脚本的好处,可以传参数。

#!/bin/sh. /etc/profilehadoop fs -rmr /user/hive/warehouse/DBSsqoop import  --connect "jdbc:mysql://cdhmaster:3306/hive"    \--username root                                                          \--password 123456                                                        \-m    1                                                             \--table  DBS                                                           \--columns   "DB_ID,DESC,DB_LOCATION_URI,NAME,OWNER_NAME,OWNER_TYPE"         \--target-dir  "/user/hive/warehouse/DBS"    #--where "length(DESC)>0"                                               \                               #--null-string ''

bug,驱动问题

ERROR manager.SqlManager: Error reading from database: java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@3c1a42fa is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@3c1a42fa is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.

增加参数,参考

https://stackoverflow.com/questions/29162447/sqoop-import-issue-with-mysql

https://stackoverflow.com/questions/26375269/sqoop-error-manager-sqlmanager-error-reading-from-database-java-sql-sqlexcept

--driver com.mysql.jdbc.Driver

增加参数后的警告,

WARN sqoop.ConnFactory: Parameter --driver is set to an explicit driver however appropriate connection manager is not being set (via --connection-manager). Sqoop is going to fall back to org.apache.sqoop.manager.GenericJdbcManager. Please specify explicitly which connection manager should be used next time.

bug,sql语法问题,

Error: java.io.IOException: SQLException in nextKeyValue

去掉关键词列DESC,参考,

https://community.cloudera.com/t5/Data-Ingestion-Integration/sqoop-throws-SQLException-in-nextKeyValue/m-p/42653

案例二:数据写Hive普通表(非分区表)

# mysqlcreate table test (id int, pdate date);insert into test(id, pdate) values (1, '2017-11-05');insert into test(id, pdate) values (2, '2017-11-06');insert into test(id, pdate) values (3, '2017-11-05');insert into test(id, pdate) values (4, '2017-11-06');# hivedrop table if exists test;create table test(id int, pdate string);

--hive-import,指定要写入hive表,该参数无value。

--hive-overwrite

--hive-table,test。

案例三:写Hive分区表,so,salesorder

注意事项:

1、用什么字段做分区?
创建时间,而不是last_modify_time

Q: 用创建时间抽取至hive分区,订单状态变化周期是45天,订单状态变化后,hive数据如何同步?

# cdhmastercd ~mysql -uroot -p123456 < so.sqlERROR 1046 (3D000) at line 3: No database selectedvi so.sqluse test;mysql -uroot -p123456 < so.sql
# hiveCREATE TABLE so (  order_id bigint,  user_id bigint,  order_amt double ,  last_modify_time string) partitioned by (date string);

Sqoop执行后,注意:
- 会在该用户HDFS的home目录下,产生一个与源表同名的目录,如/user/root/so
如果sqoop import至hive成功,该目录会自动删掉。
- 在执行的目录下产生一个java文件,即opt转化的MR Job代码。
- sqoop import中,无论hive表是什么列分隔符,均可以自动兼容。

Sqoop抽取框架封装:
- 建一个mysql配置表,配置需要抽取的表及信息;
- Java读取mysql配置表,动态生成opt文件;
- Java中执行Process类调本地系统命令—sqoop –options-file opt文件;

Sqoop-imp -task 1 “2015-04-21”

Sqoop-imp “2015-04-21”

Sqoop export

# mysql testcreate table so1 as select * from so where 1=0;

源头必须是HDFS/Hive,目标关系数据库。

表so1的datelast_modify_time修改为varchar

Sqoop工具封装

Flow etl 执行所有已配置的表抽取。

Flow etl -task 1

Flow etl -task 1 2017-01-01

  • 读取mysql的extract_to_hdfsextract_db_info,根据配置信息生成.opt文件。
  • 通过Java的Process类调Linux命令:sqoop --options-file opt文件

idea打包Flow.jar,'D:/Java/idea/IdeaProjects/Hive_Prj/src/META-INF/MANIFEST.MF' already exists in VFS,删掉文件夹META-INF

db.properties是访问mysql数据库的配置。

extract_db_info,抽取的表来自的数据库的配置。

Flow.jar上传至/root/project/lib

/root/project/bin,创建Flow命令。

配置FLOW_HOME

vi /etc/profileexport FLOW_HOME=/root/projectsource /etc/profile

配置db.properties

# FLOW_HOMEmkdir confvi db.propertiesdb.driver=com.mysql.jdbc.Driverdb.url=jdbc:mysql://cdhmaster:3306/testdb.user=rootdb.password=123456

配置sqoop option目录sqoop/opts

# FLOW_HOMEmkdir -p sqoop/opts

如果要在执行时产生日志,需要开发jar时配置log4j。

ERROR manager.SqlManager: Error reading from database: java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@310d117d is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.java.sql.SQLException: Streaming result set com.mysql.jdbc.RowDataDynamic@310d117d is still active. No statements may be issued when any streaming result sets are open and in use on a given connection. Ensure that you have called .close() on any active streaming result sets before attempting more queries.

HDFSExtract.java,增加配置--driver com.mysql.jdbc.Driver,重新打包上传。

作业可以相应做修改,如sh ./so.sh

# /root/project/sqoop_prj/DBSvi so.shFlow etl -task 1 $yestoday

您可能还想看

数据分析/数据挖掘/机器学习

Python数据挖掘与机器学习_通信信用风险评估实战(1)——读数据

Python数据挖掘与机器学习_通信信用风险评估实战(2)——数据预处理

Python数据挖掘与机器学习_通信信用风险评估实战(3)——特征工程

Python数据挖掘与机器学习_通信信用风险评估实战(4)——模型训练与调优

爬虫

Python爬虫实战之爬取链家广州房价_01简单的单页爬虫

Python爬虫实战之爬取链家广州房价_02把小爬虫变大

Python爬虫实战之爬取链家广州房价_03存储

Python爬虫实战之爬取链家广州房价_04链家的模拟登录(记录)

搜狗词库爬虫(1):基础爬虫架构和爬取词库分类

搜狗词库爬虫(2):基础爬虫框架的运行流程


微信公众号「数据分析」,分享数据科学家的自我修养,既然遇见,不如一起成长。

这里写图片描述

转载请注明:转载自微信公众号「数据分析」


原创粉丝点击