[Hadoop]Sqoop 1.4.2中文文档(一)之数据导入
$ sqoop help
usage: sqoop COMMAND [ARGS]Available commands:codegen Generate code to interact with database recordscreate-hive-table Import a table definition into Hiveeval Evaluate a SQL statement and display the resultsexport Export an HDFS directory to a database tablehelp List available commandsimport Import a table from a database to HDFSimport-all-tables Import tables from a database to HDFSlist-databases List available databases on a serverlist-tables List available tables in a databaseversion Display version informationSee 'sqoop help COMMAND' for information on a specific command.
你可以使用sqoop help (tool-name)也可以使用sqoop (tool-name)--help来使用帮助。
例如: sqoop help import. sqoop import --help.
二、Sqoop的别名
例如:sqoop import --help 等同于 sqoop-import --help,即sqoop-import是sqoop import的别名。
三、sqoop-import
$ sqoop help import
usage: sqoop import [GENERIC-ARGS] [TOOL-ARGS]Common arguments:--connect <jdbc-uri> Specify JDBC connect string--connect-manager <jdbc-uri> Specify connection manager class to use--driver <class-name> Manually specify JDBC driver class to use--hadoop-home <dir> Override $HADOOP_HOME--help Print usage instructions
-P Read password from console--password <password> Set authentication password--username <username> Set authentication username--verbose Print more information while working[...]Generic Hadoop command-line arguments:
(must preceed any tool-specific arguments)
Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]
其中Generic option的设置要在Common arguments之前,-conf,-fs,-jt-,-D都是对hadoop服务进行设置的,例如 -D mapred.job.name=<job_name>能够制定job的名字,如果不指定的话Job的名字将以用到的Jar包作为Job的名字。
例如:
User: hdfs
Job Name: cn_opda_a_phonoalbumshoushou_json_120901.jar
Job File: hdfs://vm-nba01.in.dx:9000/home/hdfs/tmp/mapred/staging/hdfs/.staging/job_201210171559_0391/job.xml
Submit Host: vm-nba01.in.dx
Submit Host Address: 10.18.102.101
Job-ACLs: All users are allowed
Job Setup: Successful
Status: Succeeded
Started at: Tue Oct 23 15:18:41 CST 2012
Finished at: Tue Oct 23 15:23:20 CST 2012
Finished in: 4mins, 39sec
Job Cleanup: Successful
而files、libjars 、archives 选项则不具有代表性质,因为这些选项在Hadoop内部命令中已经被支持了,可以查看hadoop job的帮助。
[work@vm-nba01 ~]$ hadoop job
Usage: JobClient <command> <args>[-submit <job-file>][-status <job-id>][-counter <job-id> <group-name> <counter-name>][-kill <job-id>][-set-priority <job-id> <priority>]. Valid values for priorities are: VERY_HIGH HIGH NORMAL LOW VERY_LOW[-events <job-id> <from-event-#> <#-of-events>][-history <jobOutputDir>][-list [all]][-list-active-trackers][-list-blacklisted-trackers][-list-attempt-ids <job-id> <task-type> <task-state>][-kill-task <task-id>][-fail-task <task-id>]Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.
四、sqoop脚本
举例:
$ sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST
下面把这些选项参数做成脚本进行传递:(import.txt)
$ sqoop --options-file /users/homer/work/import.txt --table TEST
那么import.txt中的参数要按照行来进行分隔,内容如下:
#
# Options file for Sqoop import
## Specifies the tool being invoked
import# Connect parameter and value
--connect
jdbc:mysql://localhost/db# Username parameter and value
--username
foo#
# Remaining options should be specified in the command line.
#
举个sqoop连接数据库,将数据库内的数据导入到HDFS中的例子:
sqoop import --connect jdbc:mysql://database.example.com/employees \--username aaron --password 12345
这样连接例子需要把mysql driver的jar包放到你的环境Path中,否则请这样使用:
$ sqoop import --driver com.microsoft.jdbc.sqlserver.SQLServerDriver \--connect <connect-string> ...
sqoop-import控制参数:
usage: sqoop import [GENERIC-ARGS] [TOOL-ARGS]Common arguments:--connect <jdbc-uri> Specify JDBC connectstring--connection-manager <class-name> Specify connection managerclass name--connection-param-file <properties-file> Specify connectionparameters file--driver <class-name> Manually specify JDBCdriver class to use--hadoop-home <dir> Override $HADOOP_HOME--help Print usage instructions
-P Read password from console--password <password> Set authenticationpassword--username <username> Set authenticationusername--verbose Print more informationwhile workingImport control arguments:--append Imports data in append mode--as-avrodatafile Imports data to Avro data files--as-sequencefile Imports data to SequenceFiles--as-textfile Imports data as plain text (default)--boundary-query <statement> Set boundary query for retrieving maxand min value of the primary key--columns <col,col,col...> Columns to import from table--compression-codec <codec> Compression codec to use for import--direct Use direct import fast path--direct-split-size <n> Split the input stream every 'n' byteswhen importing in direct mode
-e,--query <statement> Import results of SQL 'statement'--fetch-size <n> Set number 'n' of rows to fetch fromthe database when more rows are needed--inline-lob-limit <n> Set the maximum size for an inline LOB
-m,--num-mappers <n> Use 'n' map tasks to import in parallel--split-by <column-name> Column of the table used to split workunits--table <table-name> Table to read--target-dir <dir> HDFS plain table destination--warehouse-dir <dir> HDFS parent for table destination--where <where clause> WHERE clause to use during import
-z,--compress Enable compressionIncremental import arguments:--check-column <column> Source column to check for incrementalchange--incremental <import-type> Define an incremental import of type'append' or 'lastmodified'--last-value <value> Last imported value in the incrementalcheck columnOutput line formatting arguments:--enclosed-by <char> Sets a required field enclosingcharacter--escaped-by <char> Sets the escape character--fields-terminated-by <char> Sets the field separator character--lines-terminated-by <char> Sets the end-of-line character--mysql-delimiters Uses MySQL's default delimiter set:fields: , lines: \n escaped-by: \optionally-enclosed-by: '--optionally-enclosed-by <char> Sets a field enclosing characterInput parsing arguments:--input-enclosed-by <char> Sets a required field encloser--input-escaped-by <char> Sets the input escapecharacter--input-fields-terminated-by <char> Sets the input field separator--input-lines-terminated-by <char> Sets the input end-of-linechar--input-optionally-enclosed-by <char> Sets a field enclosingcharacterHive arguments:--create-hive-table Fail if the target hivetable exists--hive-delims-replacement <arg> Replace Hive record \0x01and row delimiters (\n\r)from imported string fieldswith user-defined string--hive-drop-import-delims Drop Hive record \0x01 androw delimiters (\n\r) fromimported string fields--hive-home <dir> Override $HIVE_HOME--hive-import Import tables into Hive(Uses Hive's defaultdelimiters if none areset.)--hive-overwrite Overwrite existing data inthe Hive table--hive-partition-key <partition-key> Sets the partition key touse when importing to hive--hive-partition-value <partition-value> Sets the partition value touse when importing to hive--hive-table <table-name> Sets the table name to usewhen importing to hive--map-column-hive <arg> Override mapping forspecific column to hivetypes.HBase arguments:--column-family <family> Sets the target column family for theimport--hbase-create-table If specified, create missing HBase tables--hbase-row-key <col> Specifies which input column to use as therow key--hbase-table <table> Import to <table> in HBaseCode generation arguments:--bindir <dir> Output directory for compiledobjects--class-name <name> Sets the generated class name.This overrides --package-name.When combined with --jar-file,sets the input class.--input-null-non-string <null-str> Input null non-stringrepresentation--input-null-string <null-str> Input null string representation--jar-file <file> Disable code generation; usespecified jar--map-column-java <arg> Override mapping for specificcolumns to java types--null-non-string <null-str> Null non-string representation--null-string <null-str> Null string representation--outdir <dir> Output directory for generatedcode--package-name <name> Put auto-generated classes inthis packageGeneric Hadoop command-line arguments:
(must preceed any tool-specific arguments)
Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]
五、利用查询结果作为sqoop的导入内容
举例:其中split-by是导入后的数据按照a.id进行分割,--target-dir目标地址,查询后的结果将放入这个文件
$ sqoop import \--query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \--split-by a.id --target-dir /user/foo/joinresults
举例:m代表只查询一次并且边查询边导入
$ sqoop import \--query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \-m 1 --target-dir /user/foo/joinresults
导入时候可以控制分割文件大小,或者字符串转义例如:--direct-split-size 以及--warehouse-dir ,--default-character-set
例如:
$ sqoop import --connect jdbc:mysql://server.foo.com/db --table bar \--direct -- --default-character-set=latin1
$ sqoop import --connnect <connect-str> --table foo --warehouse-dir /shared \...
sqoop对java以及hive提供支持,所以你可以导入key/value这样的map数据,例如:
Argument Description
--map-column-java <mapping> Override mapping from SQL to Java type for configured columns.
--map-column-hive <mapping> Override mapping from SQL to Hive type for configured columns.
$ sqoop import ... --map-column-java id=String,value=Integer
六、sqoop的增量导入
Argument Description
--check-column (col) Specifies the column to be examined when determining which rows to import.
--incremental (mode) Specifies how Sqoop determines which rows are new. Legal values for mode include append and lastmodified.
--last-value (value) Specifies the maximum value of the check column from the previous import.
通过增量导入你可以只导入一个已经存在表的增列值,或者表后面的值。
增量导入需要给定必要的参数,详情一个增量导入的例子。
当然你也可以指定导入到Hive后的文件格式:有2种
1.--as-textfile
这个参数你可以查看到hive内的原数据就是文本文件模式没有压缩
2.-z or --compress or --compression-codec
这个参数有3种写法不过导入到hive内的数据就是压缩过的了
七、sqoop的Hive与Hbase的数据导入
前六点都能看完后,Hive与Hbase的导入也就很简单了,其实就是多了一步导入的数据放在哪里而已。
Hive举例:
sqoop import --verbose --connect jdbc:mysql://10.18.102.133/tjss_opda --username tongji --password dx_tj --table opda_start_120604 --hive-import --hive-table opda_start_120604_incr --hive-overwrite --directsqoop job --create opda_start_120604 -- import --connect jdbc:mysql://10.18.102.133/tjss_opda --username tongji --password dx_tj --table opda_start_120604 --hive-import --hive-table opda_start_120604_incr --check-column id --incremental append --last-value 0 -m 8 --hive-overwrite --hive-delims-replacement="\t"
注意事项:
1.注意Sqoop是在Hadoop上跑的,所以jdbc url不要写localhost,--direct的要求同理。
2.Sqoop也有metastore,目前看来,没有启动metastore service时其不是线程安全的。另外就是它只能使用hsqldb,暂不支持其他数据库,对hsqldb可靠性没有太高信心。
3.Metastore里password是明文存储的,所以它不建议存储password。
4.Sqoop有bug,目前数据库里有特殊表名时有问题。
5.Sqoop导入到hive里的表只能是TEXTFILE,不过可以选择压缩格式
6.Sqoop可以导入到一个已经存在的空hive表,但是是使用Load data导入数据,所以目标表的schema实际上是被无视了。
7.Sqoop导入hive若不加hive-overwirte,会留下hadoop目录,对下次若执行相同任务有影响。
8.注意加入delims-replace,否则很容易因为分隔符问题出现错误行。
9.Hive进行dynamic partition时,一次partition数量过多有Bug,必须加distribute by
10.Hive对大小写不区分,尽量全小写,否则有潜在bug
11.Sqoop要求运行时当前目录可写(code-gen)。
12.只要有jdbc驱动,所有jdbc兼容的数据库均可导入
导入时除了用到sqoop相关的hive与Hbase的参数外,还会用到导入时候专用的参数:
Input parsing arguments:--input-enclosed-by <char> Sets a required field encloser--input-escaped-by <char> Sets the input escapecharacter--input-fields-terminated-by <char> Sets the input field separator--input-lines-terminated-by <char> Sets the input end-of-linechar--input-optionally-enclosed-by <char> Sets a field enclosingcharacter
这个部分的参数有可能你会用到的。另外如果导入语句没有添加目的表或者地址则导入的内容会写在HDFS当前的操作目录下。
八、sqoop导入全部表和数据
举个例子,其他参数均与sqoop help参数相同:
$ sqoop import-all-tables --connect jdbc:mysql://db.foo.com/corp
验证结果:
$ hadoop fs -ls
Found 4 items
drwxr-xr-x - someuser somegrp 0 2010-04-27 17:15 /user/someuser/EMPLOYEES
drwxr-xr-x - someuser somegrp 0 2010-04-27 17:15 /user/someuser/PAYCHECKS
drwxr-xr-x - someuser somegrp 0 2010-04-27 17:15 /user/someuser/DEPARTMENTS
drwxr-xr-x - someuser somegrp 0 2010-04-27 17:15 /user/someuser/OFFICE_SUPPLIES
[Hadoop]Sqoop 1.4.2中文文档(一)之数据导入相关推荐
- React-Redux 中文文档
介绍 快速开始 React-Redux是Redux的官方React绑定库.它能够使你的React组件从Redux store中读取数据,并且向store分发actions以更新数据 安装 在你的Rea ...
- kafka中文文档(0.10.0)
kafka中文文档(0.10.0) 作者:链上研发-老杨叔叔 时间:2016-07-22 版本:Apache Kafka 0.10.0 (2016年5月底发布) .目录 kafka中文文档0100 目 ...
- Phoenix综述(史上最全Phoenix中文文档)
个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/users/6cb45a00b49c/latest_articles 网上关于P ...
- PyTorch官方中文文档:torch.optim 优化器参数
内容预览: step(closure) 进行单次优化 (参数更新). 参数: closure (callable) –...~ 参数: params (iterable) – 待优化参数的iterab ...
- golang中文文档_Golang 标准库 限流器 time/rate 设计与实现
限流器是后台服务中十分重要的组件,在实际的业务场景中使用居多,其设计在微服务.网关.和一些后台服务中会经常遇到.限流器的作用是用来限制其请求的速率,保护后台响应服务,以免服务过载导致服务不可用现象出现 ...
- springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列
源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...
- Apache Spark 2.2.0 中文文档 翻译活动
为什么80%的码农都做不了架构师?>>> Spark 2.2.0 已然发布(2017-07-11 发布) 5 天了,更新了一些新套路吧! 此版本从 Structured Str ...
- Python爱好者周知:Scikit-Learn中文文档正式发布
整理 | 费棋 出品 | AI科技大本营(公众号ID:rgznai100) 近日,Scikit-Learn 中文文档已由开源组织 ApacheCN 完成校对.该中文文档依然包含了 Scikit-Lea ...
- GitHub 中文文档正式发布
点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试资料 中国作为全球最大的人口大国,所属开发者在 GitHub 上的占比自 ...
最新文章
- SAP SD基础知识之自动信用控制
- 基于struts2和hibernate的登录和注册功能——完整实例
- 前端学习(3173):react-hello-react之todoList教程
- 关于腾讯云丢数据事件的一些看法
- gorm软删除_删除 |《GORM 中文文档 v1》| Go 技术论坛
- html制作清明上河图
- qpython3下载不了_qpython3手机版
- 关于安卓图片剪切造成软件崩溃问题的解决方法
- servlet工作流程
- Latex 希腊字母大写
- cad相贯展开图lisp_cad相贯线的画法
- Kafka常用命令(1):kafka-topics
- 211毕业生3天被辞退:offer不等于工作稳了!
- 佟年计算机大赛,《亲爱的,热爱的》热播,吴白见到佟年第一眼,这眼神亮了!...
- 小学四年级计算机考试题,小学四年级信息技术试卷题库.doc
- python评价指标_[Python人工智能] 六.神经网络的评价指标、特征标准化和特征选择...
- 8. Android MultiMedia框架完全解析 - prepareAsync的过程分析
- 每日一道leetcode(python)46. 全排列
- 【工具推荐】图像界的魔术师 ImageMagick
- 曙光大数据战略:解析XData一体机产品
热门文章
- BMC——服务器运维管理利器
- linux lighttpd php,安装配置Linux+lighttpd+mysql+PHP(FastCGI)
- 模仿淘宝首页横向导航栏
- 自定义View——游动锦鲤实践
- 【Kotlin】空安全 ⑥ ( 先决条件函数 | checkNotNull 函数 | require 函数 | requireNotNull 函数 | error 函数 | assert 函数 )
- 英特尔的驱动程序与Windows 10 V1903出现兼容问题
- 卸载EX的深信服EDR
- 数据压缩实验三--Huffman编解码及压缩率的比较
- 质检总局要求:做好口岸核与辐射物质监测工作
- php采集节目单,电视节目预告