sqoop1.4.2数据导入

来源:互联网 发布:台湾奶粉代购 知乎 编辑:程序博客网 时间:2024/05/17 02:20
一、Sqoop Help 
Java代码  收藏代码
  1. $ sqoop help  
  2. usage: sqoop COMMAND [ARGS]  
  3.   
  4. Available commands:  
  5.   codegen            Generate code to interact with database records  
  6.   create-hive-table  Import a table definition into Hive  
  7.   eval               Evaluate a SQL statement and display the results  
  8.   export             Export an HDFS directory to a database table  
  9.   help               List available commands  
  10.   import             Import a table from a database to HDFS  
  11.   import-all-tables  Import tables from a database to HDFS  
  12.   list-databases     List available databases on a server  
  13.   list-tables        List available tables in a database  
  14.   version            Display version information  
  15.   
  16. 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.   
  3. Common arguments:  
  4.    --connect <jdbc-uri>     Specify JDBC connect string  
  5.    --connect-manager <jdbc-uri>     Specify connection manager class to use  
  6.    --driver <class-name>    Manually specify JDBC driver class to use  
  7.    --hadoop-home <dir>      Override $HADOOP_HOME  
  8.    --help                   Print usage instructions  
  9. -P                          Read password from console  
  10.    --password <password>    Set authentication password  
  11.    --username <username>    Set authentication username  
  12.    --verbose                Print more information while working  
  13.   
  14. [...]  
  15.   
  16. Generic Hadoop command-line arguments:  
  17. (must preceed any tool-specific arguments)  
  18. Generic options supported are  
  19. -conf <configuration file>     specify an application configuration file  
  20. -D <property=value>            use value for given property  
  21. -fs <local|namenode:port>      specify a namenode  
  22. -jt <local|jobtracker:port>    specify a job tracker  
  23. -files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster  
  24. -libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.  
  25. -archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.  
  26.   
  27. The general command line syntax is  
  28. 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.   
  15.     [-kill-task <task-id>]  
  16.     [-fail-task <task-id>]  
  17.   
  18. Generic options supported are  
  19. -conf <configuration file>     specify an application configuration file  
  20. -D <property=value>            use value for given property  
  21. -fs <local|namenode:port>      specify a namenode  
  22. -jt <local|jobtracker:port>    specify a job tracker  
  23. -files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster  
  24. -libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.  
  25. -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.   
  5. # Specifies the tool being invoked  
  6. import  
  7.   
  8. # Connect parameter and value  
  9. --connect  
  10. jdbc:mysql://localhost/db  
  11.   
  12. # Username parameter and value  
  13. --username  
  14. foo  
  15.   
  16. #  
  17. # Remaining options should be specified in the command line.  
  18. #  


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