一、Sqoop Help

Java代码  
  1. $ sqoop help
  2. usage: sqoop COMMAND [ARGS]
  3. Available commands:
  4. codegen            Generate code to interact with database records
  5. create-hive-table  Import a table definition into Hive
  6. eval               Evaluate a SQL statement and display the results
  7. export             Export an HDFS directory to a database table
  8. help               List available commands
  9. import             Import a table from a database to HDFS
  10. import-all-tables  Import tables from a database to HDFS
  11. list-databases     List available databases on a server
  12. list-tables        List available tables in a database
  13. version            Display version information
  14. See '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

Java代码  
  1. usage: sqoop import [GENERIC-ARGS] [TOOL-ARGS]
  2. Common arguments:
  3. --connect <jdbc-uri>     Specify JDBC connect string
  4. --connect-manager <jdbc-uri>     Specify connection manager class to use
  5. --driver <class-name>    Manually specify JDBC driver class to use
  6. --hadoop-home <dir>      Override $HADOOP_HOME
  7. --help                   Print usage instructions
  8. -P                          Read password from console
  9. --password <password>    Set authentication password
  10. --username <username>    Set authentication username
  11. --verbose                Print more information while working
  12. [...]
  13. Generic Hadoop command-line arguments:
  14. (must preceed any tool-specific arguments)
  15. Generic options supported are
  16. -conf <configuration file>     specify an application configuration file
  17. -D <property=value>            use value for given property
  18. -fs <local|namenode:port>      specify a namenode
  19. -jt <local|jobtracker:port>    specify a job tracker
  20. -files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster
  21. -libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.
  22. -archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.
  23. The general command line syntax is
  24. bin/hadoop command [genericOptions] [commandOptions]

其中Generic option的设置要在Common arguments之前,-conf,-fs,-jt-,-D都是对hadoop服务进行设置的,例如 -D mapred.job.name=<job_name>能够制定job的名字,如果不指定的话Job的名字将以用到的Jar包作为Job的名字。 
例如:

Java代码  
  1. User: hdfs
  2. Job Name: cn_opda_a_phonoalbumshoushou_json_120901.jar
  3. Job File: hdfs://vm-nba01.in.dx:9000/home/hdfs/tmp/mapred/staging/hdfs/.staging/job_201210171559_0391/job.xml
  4. Submit Host: vm-nba01.in.dx
  5. Submit Host Address: 10.18.102.101
  6. Job-ACLs: All users are allowed
  7. Job Setup: Successful
  8. Status: Succeeded
  9. Started at: Tue Oct 23 15:18:41 CST 2012
  10. Finished at: Tue Oct 23 15:23:20 CST 2012
  11. Finished in: 4mins, 39sec
  12. Job Cleanup: Successful

而files、libjars 、archives 选项则不具有代表性质,因为这些选项在Hadoop内部命令中已经被支持了,可以查看hadoop job的帮助。

Java代码  
  1. [work@vm-nba01 ~]$ hadoop job
  2. Usage: JobClient <command> <args>
  3. [-submit <job-file>]
  4. [-status <job-id>]
  5. [-counter <job-id> <group-name> <counter-name>]
  6. [-kill <job-id>]
  7. [-set-priority <job-id> <priority>]. Valid values for priorities are: VERY_HIGH HIGH NORMAL LOW VERY_LOW
  8. [-events <job-id> <from-event-#> <#-of-events>]
  9. [-history <jobOutputDir>]
  10. [-list [all]]
  11. [-list-active-trackers]
  12. [-list-blacklisted-trackers]
  13. [-list-attempt-ids <job-id> <task-type> <task-state>]
  14. [-kill-task <task-id>]
  15. [-fail-task <task-id>]
  16. Generic options supported are
  17. -conf <configuration file>     specify an application configuration file
  18. -D <property=value>            use value for given property
  19. -fs <local|namenode:port>      specify a namenode
  20. -jt <local|jobtracker:port>    specify a job tracker
  21. -files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster
  22. -libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.
  23. -archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.

四、sqoop脚本 
举例:

Java代码  
  1. $ sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST

下面把这些选项参数做成脚本进行传递:(import.txt)

Java代码  
  1. $ sqoop --options-file /users/homer/work/import.txt --table TEST

那么import.txt中的参数要按照行来进行分隔,内容如下:

Java代码  
  1. #
  2. # Options file for Sqoop import
  3. #
  4. # Specifies the tool being invoked
  5. import
  6. # Connect parameter and value
  7. --connect
  8. jdbc:mysql://localhost/db
  9. # Username parameter and value
  10. --username
  11. foo
  12. #
  13. # Remaining options should be specified in the command line.
  14. #

举个sqoop连接数据库,将数据库内的数据导入到HDFS中的例子:

Java代码  
  1. sqoop import --connect jdbc:mysql://database.example.com/employees \
  2. --username aaron --password 12345

这样连接例子需要把mysql driver的jar包放到你的环境Path中,否则请这样使用:

Java代码  
  1. $ sqoop import --driver com.microsoft.jdbc.sqlserver.SQLServerDriver \
  2. --connect <connect-string> ...

sqoop-import控制参数:

Java代码  
  1. usage: sqoop import [GENERIC-ARGS] [TOOL-ARGS]
  2. Common arguments:
  3. --connect <jdbc-uri>                         Specify JDBC connect
  4. string
  5. --connection-manager <class-name>            Specify connection manager
  6. class name
  7. --connection-param-file <properties-file>    Specify connection
  8. parameters file
  9. --driver <class-name>                        Manually specify JDBC
  10. driver class to use
  11. --hadoop-home <dir>                          Override $HADOOP_HOME
  12. --help                                       Print usage instructions
  13. -P                                              Read password from console
  14. --password <password>                        Set authentication
  15. password
  16. --username <username>                        Set authentication
  17. username
  18. --verbose                                    Print more information
  19. while working
  20. Import control arguments:
  21. --append                        Imports data in append mode
  22. --as-avrodatafile               Imports data to Avro data files
  23. --as-sequencefile               Imports data to SequenceFiles
  24. --as-textfile                   Imports data as plain text (default)
  25. --boundary-query <statement>    Set boundary query for retrieving max
  26. and min value of the primary key
  27. --columns <col,col,col...>      Columns to import from table
  28. --compression-codec <codec>     Compression codec to use for import
  29. --direct                        Use direct import fast path
  30. --direct-split-size <n>         Split the input stream every 'n' bytes
  31. when importing in direct mode
  32. -e,--query <statement>             Import results of SQL 'statement'
  33. --fetch-size <n>                Set number 'n' of rows to fetch from
  34. the database when more rows are needed
  35. --inline-lob-limit <n>          Set the maximum size for an inline LOB
  36. -m,--num-mappers <n>               Use 'n' map tasks to import in parallel
  37. --split-by <column-name>        Column of the table used to split work
  38. units
  39. --table <table-name>            Table to read
  40. --target-dir <dir>              HDFS plain table destination
  41. --warehouse-dir <dir>           HDFS parent for table destination
  42. --where <where clause>          WHERE clause to use during import
  43. -z,--compress                      Enable compression
  44. Incremental import arguments:
  45. --check-column <column>        Source column to check for incremental
  46. change
  47. --incremental <import-type>    Define an incremental import of type
  48. 'append' or 'lastmodified'
  49. --last-value <value>           Last imported value in the incremental
  50. check column
  51. Output line formatting arguments:
  52. --enclosed-by <char>               Sets a required field enclosing
  53. character
  54. --escaped-by <char>                Sets the escape character
  55. --fields-terminated-by <char>      Sets the field separator character
  56. --lines-terminated-by <char>       Sets the end-of-line character
  57. --mysql-delimiters                 Uses MySQL's default delimiter set:
  58. fields: ,  lines: \n  escaped-by: \
  59. optionally-enclosed-by: '
  60. --optionally-enclosed-by <char>    Sets a field enclosing character
  61. Input parsing arguments:
  62. --input-enclosed-by <char>               Sets a required field encloser
  63. --input-escaped-by <char>                Sets the input escape
  64. character
  65. --input-fields-terminated-by <char>      Sets the input field separator
  66. --input-lines-terminated-by <char>       Sets the input end-of-line
  67. char
  68. --input-optionally-enclosed-by <char>    Sets a field enclosing
  69. character
  70. Hive arguments:
  71. --create-hive-table                         Fail if the target hive
  72. table exists
  73. --hive-delims-replacement <arg>             Replace Hive record \0x01
  74. and row delimiters (\n\r)
  75. from imported string fields
  76. with user-defined string
  77. --hive-drop-import-delims                   Drop Hive record \0x01 and
  78. row delimiters (\n\r) from
  79. imported string fields
  80. --hive-home <dir>                           Override $HIVE_HOME
  81. --hive-import                               Import tables into Hive
  82. (Uses Hive's default
  83. delimiters if none are
  84. set.)
  85. --hive-overwrite                            Overwrite existing data in
  86. the Hive table
  87. --hive-partition-key <partition-key>        Sets the partition key to
  88. use when importing to hive
  89. --hive-partition-value <partition-value>    Sets the partition value to
  90. use when importing to hive
  91. --hive-table <table-name>                   Sets the table name to use
  92. when importing to hive
  93. --map-column-hive <arg>                     Override mapping for
  94. specific column to hive
  95. types.
  96. HBase arguments:
  97. --column-family <family>    Sets the target column family for the
  98. import
  99. --hbase-create-table        If specified, create missing HBase tables
  100. --hbase-row-key <col>       Specifies which input column to use as the
  101. row key
  102. --hbase-table <table>       Import to <table> in HBase
  103. Code generation arguments:
  104. --bindir <dir>                        Output directory for compiled
  105. objects
  106. --class-name <name>                   Sets the generated class name.
  107. This overrides --package-name.
  108. When combined with --jar-file,
  109. sets the input class.
  110. --input-null-non-string <null-str>    Input null non-string
  111. representation
  112. --input-null-string <null-str>        Input null string representation
  113. --jar-file <file>                     Disable code generation; use
  114. specified jar
  115. --map-column-java <arg>               Override mapping for specific
  116. columns to java types
  117. --null-non-string <null-str>          Null non-string representation
  118. --null-string <null-str>              Null string representation
  119. --outdir <dir>                        Output directory for generated
  120. code
  121. --package-name <name>                 Put auto-generated classes in
  122. this package
  123. Generic Hadoop command-line arguments:
  124. (must preceed any tool-specific arguments)
  125. Generic options supported are
  126. -conf <configuration file>     specify an application configuration file
  127. -D <property=value>            use value for given property
  128. -fs <local|namenode:port>      specify a namenode
  129. -jt <local|jobtracker:port>    specify a job tracker
  130. -files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster
  131. -libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.
  132. -archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.
  133. The general command line syntax is
  134. bin/hadoop command [genericOptions] [commandOptions]

五、利用查询结果作为sqoop的导入内容

举例:其中split-by是导入后的数据按照a.id进行分割,--target-dir目标地址,查询后的结果将放入这个文件

Java代码  
  1. $ sqoop import \
  2. --query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \
  3. --split-by a.id --target-dir /user/foo/joinresults

举例:m代表只查询一次并且边查询边导入

Java代码  
  1. $ sqoop import \
  2. --query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \
  3. -m 1 --target-dir /user/foo/joinresults

导入时候可以控制分割文件大小,或者字符串转义例如:--direct-split-size 以及--warehouse-dir ,--default-character-set

例如:

Java代码  
  1. $ sqoop import --connect jdbc:mysql://server.foo.com/db --table bar \
  2. --direct -- --default-character-set=latin1
  3. $ sqoop import --connnect <connect-str> --table foo --warehouse-dir /shared \
  4. ...

sqoop对java以及hive提供支持,所以你可以导入key/value这样的map数据,例如:

Java代码  
  1. Argument     Description
  2. --map-column-java <mapping>    Override mapping from SQL to Java type for configured columns.
  3. --map-column-hive <mapping>    Override mapping from SQL to Hive type for configured columns.
  4. $ sqoop import ... --map-column-java id=String,value=Integer

六、sqoop的增量导入

Java代码  
  1. Argument     Description
  2. --check-column (col)     Specifies the column to be examined when determining which rows to import.
  3. --incremental (mode)     Specifies how Sqoop determines which rows are new. Legal values for mode include append and lastmodified.
  4. --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举例:

Java代码  
  1. 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 --direct
  2. sqoop 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"

注意事项:

Java代码  
  1. 1.注意Sqoop是在Hadoop上跑的,所以jdbc url不要写localhost,--direct的要求同理。
  2. 2.Sqoop也有metastore,目前看来,没有启动metastore service时其不是线程安全的。另外就是它只能使用hsqldb,暂不支持其他数据库,对hsqldb可靠性没有太高信心。
  3. 3.Metastore里password是明文存储的,所以它不建议存储password。
  4. 4.Sqoop有bug,目前数据库里有特殊表名时有问题。
  5. 5.Sqoop导入到hive里的表只能是TEXTFILE,不过可以选择压缩格式
  6. 6.Sqoop可以导入到一个已经存在的空hive表,但是是使用Load data导入数据,所以目标表的schema实际上是被无视了。
  7. 7.Sqoop导入hive若不加hive-overwirte,会留下hadoop目录,对下次若执行相同任务有影响。
  8. 8.注意加入delims-replace,否则很容易因为分隔符问题出现错误行。
  9. 9.Hive进行dynamic partition时,一次partition数量过多有Bug,必须加distribute by
  10. 10.Hive对大小写不区分,尽量全小写,否则有潜在bug
  11. 11.Sqoop要求运行时当前目录可写(code-gen)。
  12. 12.只要有jdbc驱动,所有jdbc兼容的数据库均可导入

导入时除了用到sqoop相关的hive与Hbase的参数外,还会用到导入时候专用的参数:

Java代码  
  1. Input parsing arguments:
  2. --input-enclosed-by <char>               Sets a required field encloser
  3. --input-escaped-by <char>                Sets the input escape
  4. character
  5. --input-fields-terminated-by <char>      Sets the input field separator
  6. --input-lines-terminated-by <char>       Sets the input end-of-line
  7. char
  8. --input-optionally-enclosed-by <char>    Sets a field enclosing
  9. character

这个部分的参数有可能你会用到的。另外如果导入语句没有添加目的表或者地址则导入的内容会写在HDFS当前的操作目录下。

八、sqoop导入全部表和数据

举个例子,其他参数均与sqoop help参数相同:

Java代码  
  1. $ sqoop import-all-tables --connect jdbc:mysql://db.foo.com/corp

验证结果:

Java代码  
  1. $ hadoop fs -ls
  2. Found 4 items
  3. drwxr-xr-x   - someuser somegrp       0 2010-04-27 17:15 /user/someuser/EMPLOYEES
  4. drwxr-xr-x   - someuser somegrp       0 2010-04-27 17:15 /user/someuser/PAYCHECKS
  5. drwxr-xr-x   - someuser somegrp       0 2010-04-27 17:15 /user/someuser/DEPARTMENTS
  6. drwxr-xr-x   - someuser somegrp       0 2010-04-27 17:15 /user/someuser/OFFICE_SUPPLIES

sqoop1.4.2数据导入相关推荐

  1. 通过 Sqoop1.4.7 将 Mysql5.7、Hive2.3.4、Hbase1.4.9 之间的数据导入导出

    目录 目录 1.什么是 Sqoop? 2.下载应用程序及配置环境变量 2.1.下载 Sqoop 1.4.7 2.2.设置环境变量 2.3.设置安装所需环境 3.安装 Sqoop 1.4.7 3.1.修 ...

  2. oracle11hadoop混搭,【甘道夫】Hadoop2.2.0环境使用Sqoop-1.4.4将Oracle11g数据导入HBase0.96,并自动生成组合行键...

    目的: 使用Sqoop将Oracle中的数据导入到HBase中,并自动生成组合行键! 环境: Hadoop2.2.0 Hbase0.96 sqoop-1.4.4.bin__hadoop-2.0.4-a ...

  3. mysql数据首次导入hive_sqoop1.4.7环境搭建及mysql数据导入导出到hive

    sqoop文档:http://sqoop.apache.org/docs/1.4.7/SqoopUserGuide.html#_prerequisites 在hive创建表和导入数据时必须添加分隔符, ...

  4. 利用sqoop将hive数据导入导出数据到mysql

    http://niuzhenxin.iteye.com/blog/1726414 运行环境  centos 5.6   hadoop  hive sqoop是让hadoop技术支持的clouder公司 ...

  5. 数据搬运组件:基于Sqoop管理数据导入和导出

    一.Sqoop概述 Sqoop是一款开源的大数据组件,主要用来在Hadoop(Hive.HBase等)与传统的数据库(mysql.postgresql.oracle等)间进行数据的传递. 通常数据搬运 ...

  6. Java将hive数据导入到hdfs_sqoop数据导入到Hdfs 或者hive

    准备一张表 需求 将 bbs_product 表中的前100条数据导 导出来  只要id  brand_id和 name 这3个字段 数据存在 hdfs 目录   /user/xuyou/sqoop/ ...

  7. 使用 Sqoop 将 30W+ MySQL 数据导入 Hive

    本实验完成的是,使用 Sqoop 从 MySQL 导出数据到 Hive. 整体步骤分为: 初始化 MySQL 的 30W+ 数据 安装配置 Sqoop 在 Hive 中初始化目标表 Sqoop 脚本实 ...

  8. sqoop将hbase数据导入mysql_Sqoop将mysql数据导入hbase的血与泪

    Sqoop将mysql数据导入hbase的血与泪(整整搞了大半天) 版权声明:本文为yunshuxueyuan原创文章. 如需转载请标明出处: https://my.oschina.net/yunsh ...

  9. 3.2.3 Sqoop 数据迁移工具, 导入数据import, MySQL到HDFS/Hive, 导出数据export,增量数据导入, Sqoop job,常用命令及参数

    目录 数据迁移工具 -- Sqoop 第一部分 Sqoop概述 第二部分 安装配置 第三部分 应用案例 第 1 节 导入数据import MySQL 到 HDFS MySQL 到 Hive 第 2 节 ...

最新文章

  1. Lab模式的妙用--人像处理
  2. iOS 修改UITabBar的默认点击行为
  3. 怎么使图表居中显示_【Excel技巧】制作柱形图图表完美呈现百分比,提升您的报表颜值...
  4. Mybatis 拦截器介绍
  5. Kettle 学习导航帖整理
  6. ionic 日期选择控件
  7. 12月10日 - 最新一大波迅雷看看白金迅雷钻石·VIP7的会员帐号来袭
  8. Hello Qt(十三)——QT信号与槽机制
  9. C++中智能指针-atuo_ptr、unique_ptr
  10. 【Python-GUI开发】四 pyqt5 UI扁平化设计
  11. 数据库第三次实验报告
  12. oa系统打不开只能重启服务器,oa系统打不开怎么办-oa系统打不开的解决方法 - 河东软件园...
  13. mysql.servers表存在_ERROR 1146 (42S02): Table 'mysql.servers' doesn't exist
  14. 10.图灵学院-----阿里/京东/滴滴/美团整理----安全验证篇
  15. 前端性能优化之“离线缓存manifest”
  16. 计算机网络复习zucc
  17. html5 xdwlnjs cn,最近需要调用一个网站的js,但是发现是加密的,有大佬来解密下吗?...
  18. 2. Golang基本语法
  19. 当高铁遇到地面沉降 如何测量 怎么应对(转载)
  20. signature=66ee059bbdb9aa026cf4962f2fe679a7,a00896.html

热门文章

  1. 闪迪(SanDisk)U盘防伪查询(官方网站)
  2. 写外贸开发信时不知道收件人的名字?可以用这9种方式来称呼对方
  3. XT702:mount命令释疑
  4. 西门子rwd68温控器说明书_西门子控制器RWD68
  5. 嵌入式开发:估算电池寿命的7个技巧
  6. vue显示文本过长,超出部分省略号显示,悬停提示全部
  7. Java小程序之球球大作战(基于Java线程实现)
  8. 蓄“视”待发 TCL显示器在全球最大智能显示终端制造基地下线
  9. VSCode国内镜像下载地址
  10. 在SMT贴片加工设备具有哪些优势?