热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDFS与关系型数据库数据交换利器—sqoop初探

Sqoop是一种用于hadoop与RDBMS进行数据传输的工具。配置比较简单。去apache官网下载最新的sqoop包。下载地址:www.apache.orgdistsqoop1.99.1解压缩到服务器上。服务器要求本身有jdk,hadoop,hive。配置:confsqoop-env.sh#

Sqoop是一种用于 hadoop 与 RDBMS 进行数据传输的工具。 配置比较简单。 去apache官网下载最新的 sqoop 包。 下载地址:http://www.apache.org/dist/ sqoop /1.99.1/ 解压缩到服务器上。服务器要求本身有jdk, hadoop , hive 。 配置: conf/sqoop-env.sh #

Sqoop是一种用于hadoopRDBMS进行数据传输的工具。
配置比较简单。
去apache官网下载最新的sqoop包。
下载地址:http://www.apache.org/dist/sqoop/1.99.1/
解压缩到服务器上。服务器要求本身有jdk,hadoophive
配置:
conf/sqoop-env.sh
#Set path to where bin/hadoop is available
export HADOOP_HOME=/home/hadoop/hadoop-0.20.205.0
#Set the path to where bin/hive is available
export HIVE_HOME=/home/hadoop/hive-0.8.1
这时候就可以进行试验了。我们主要是利用其与hive进行交互,实际就是将关系型的数据库中的数据提交到hive,保存到HDFS中,以便于大数据的计算。

sqoop主要包含了以下命令,或者说功能。

 codegen             Import a table definition into Hive eval                Evaluate a SQL statement and display the results export              Export an HDFS directory to a database table help                List available commands import              Import a table from a database to HDFS import-all-tables   Import tables from a database to HDFS job                 Work with saved jobs list-databases      List available databases on a server list-tables         List available tables in a database merge               Merge results of incremental imports metastore           Run a standalone Sqoop metastore version             Display version information  这里主要是使用其中的import功能。export功能的命令语法类似。

示例

./sqoop import --connect jdbc:mysql://lcoalhost:3306/dbname--username dbuser --password dbpassword --table tablename --hive-import --hive-table hivedb.hivetable --hive-drop-import-delims --hive-overwrite --num-mappers 6

以上命令的意思就是要将本地数据库dbname中的tablename表的数据导入到hivedb的hivetable表中。
其中一些常用的参数就不进行解释了。

–hive-import 标识本次导入的地址为hive
–hive-table 标识hive中的表信息
–hive-drop-import-delims 这个比较重要,因为数据从数据库中导入到HDFS中,如果包含了特殊的字符,对MR解析是存在问题的,比如数据库中
有text类型的字段,有可能包含\t,\n等参数,加入这个参数后,会自动将特殊字符进行处理。
–hive-overwrite 如果原有的hive表已经存在,则会进行覆盖操作。
–num-mappers 会指定执行本次导入的mapper任务数量。

还有一个比较重要的参数 –direct 这个参数可以通过数据库的dump功能进行数据导入,这样的性能比上例更好,但是其不能与–hive-drop-import-delims参数功能使用。所以还是要根据自己数据库的情况来进行判断使用何种命令。

如下是sqoop的import命令

Argument Description
--connect Specify JDBC connect string
--connection-manager Specify connection manager class to use
--driver Manually specify JDBC driver class to use
--hadoop-home Override $HADOOP_HOME
--help Print usage instructions
-P Read password from console
--password Set authentication password
--username Set authentication username
--verbose Print more information while working
--connection-param-file Optional properties file that provides connection parameters
Argument Description
--hive-home Override $HIVE_HOME
--hive-import Import tables into Hive (Uses Hive’s default delimiters if none are set.)
--hive-overwrite Overwrite existing data in the Hive table.
--create-hive-table If set, then the job will fail if the target hive
table exits. By default this property is false.
--hive-table Sets the table name to use when importing to Hive.
--hive-drop-import-delims Drops \n, \r, and \01 from string fields when importing to Hive.
--hive-delims-replacement Replace \n, \r, and \01 from string fields with user defined string when importing to Hive.
--hive-partition-key Name of a hive field to partition are sharded on
--hive-partition-value String-value that serves as partition key for this imported into hive in this job.
--map-column-hive Override default mapping from SQL type to Hive type for configured columns.

以下为一些参考示例

写入条件
sqoop import –table test –columns “id,name” –where “id>400″
使用dump功能
sqoop import –connect jdbc:mysql://server.foo.com/db –table bar –direct — –default-character-set=latin1
列类型重新定义
sqoop import … –map-column-java id=String,value=Integer
定义分割符
sqoop import –connect jdbc:mysql://db.foo.com/corp –table EMPLOYEES –fields-terminated-by ‘\t’ –lines-terminated-by ‘\n’ –optionally-enclosed-by ‘\”‘

推荐阅读
author-avatar
潘月飞--_758
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有