分享

sqoop 常用命令整理

pig2 2014-11-13 08:25:42 发表于 收藏型 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 19 221128
问题导读
1.sqoop是否支持复杂语句
2.sqoop如果存在就更新,不存在就插入,这是什么模式?





      
               
这些内容是从sqoop的官网整理出来的,是1.4.3版本的Document,如果有错误,希望大家指正。
      1.使用sqoop导入数据
  
  1. sqoop import --connect jdbc:mysql://localhost/db --username foo --table TEST
复制代码


  
  2.账号密码
  
  1. sqoop import --connect jdbc:mysql://database.example.com/employees \
  2.     --username aaron --password 12345
复制代码


  
   
  3.驱动
  
  1.   sqoop import --driver com.microsoft.jdbc.sqlserver.SQLServerDriver \
  2.     --connect string> ...
复制代码


  
   4.写sql语句导入的方式
  
  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
复制代码


  
  如果是顺序导入的话,可以只开一个线程
   
  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
复制代码


  如果where语句中有要用单引号的,就像这样子写就可以啦"SELECT * FROM x WHERE a='foo' AND \$CONDITIONS"

  5.  1.4.3版本的sqoop不支持复杂的sql语句,不支持or语句
  
  6. --split-by
  默认是主键,假设有100行数据,它会执行那个SELECT * FROM sometable WHERE id >= lo AND id
  
  7. --direct 是为了利用某些数据库本身提供的快速导入导出数据的工具,比如mysql的mysqldump
  性能比jdbc更好,但是不知大对象的列,使用的时候,那些快速导入的工具的客户端必须的shell脚本的目录下
  
  8.导入数据到hdfs目录,这个命令会把数据写到/shared/foo/ 目录
  
  1.     sqoop import --connnect  --table foo --warehouse-dir /shared \
复制代码


  
  或者
  
  1.     sqoop import --connnect  --table foo --target-dir /dest \
复制代码


  
  9.传递参数给快速导入的工具,使用--开头,下面这句命令传递给mysql默认的字符集是latin1
  
  1.     sqoop import --connect jdbc:mysql://server.foo.com/db --table bar \
  2.     --direct -- --default-character-set=latin1
复制代码


  
  10.转换为对象
  1.   --map-column-java   转换为java数据类型
  2.   --map-column-hive   转转为hive数据类型
复制代码



  11.增加导入
  1.   --check-column (col)  Specifies the column to be examined when determining which rows to import.
  2.   --incremental (mode)  Specifies how Sqoop determines which rows are new. Legal values for mode include append and lastmodified.
  3.   --last-value (value)  Specifies the maximum value of the check column from the previous import.
复制代码


  增加导入支持两种模式append和lastmodified,用--incremental来指定

  12.在导入大对象

      比如BLOB和CLOB列时需要特殊处理,小于16MB的大对象可以和别的数据一起存储,超过这个值就存储在_lobs的子目录当中
  它们采用的是为大对象做过优化的存储格式,最大能存储2^63字节的数据,我们可以用--inline-lob-limit参数来指定每个lob文件最大的限制是多少  如果设置为0,则大对象使用外部存储

  13.分隔符、转移字符
  下面的这句话
  Some string, with a comma.
  Another "string with quotes"
  使用这句命令导入$ sqoop import --fields-terminated-by , --escaped-by \\ --enclosed-by '\"' ...
  会有下面这个结果
  "Some string, with a comma.","1","2","3"...
  "Another \"string with quotes\"","4","5","6"...
  使用这句命令导入$ sqoop import --optionally-enclosed-by '\"' (the rest as above)...
  "Some string, with a comma.",1,2,3...
  "Another \"string with quotes\"",4,5,6...

  14.hive导入参数
  --hive-home   重写$HIVE_HOME
  --hive-import          插入数据到hive当中,使用hive的默认分隔符
  --hive-overwrite  重写插入
  --create-hive-table  建表,如果表已经存在,该操作会报错!
  --hive-table [table]  设置到hive当中的表名
  --hive-drop-import-delims  导入到hive时删除 \n, \r, and \01
  --hive-delims-replacement  导入到hive时用自定义的字符替换掉 \n, \r, and \01
  --hive-partition-key          hive分区的key
  --hive-partition-value   hive分区的值
  --map-column-hive           类型匹配,sql类型对应到hive类型

  15.hive空值处理
  sqoop会自动把NULL转换为null处理,但是hive中默认是把\N来表示null,因为预先处理不会生效的
  我们需要使用 --null-string 和 --null-non-string来处理空值 把\N转为\\N
  
  1.     sqoop import  ... --null-string '\\N' --null-non-string '\\N'
复制代码


  
  
  
  16.导入数据到hbase
  导入的时候加上--hbase-table,它就会把内容导入到hbase当中,默认是用主键作为split列
  也可以用--hbase-row-key来指定,列族用--column-family来指定,它不支持--direct。
  如果不想手动建表或者列族,就用--hbase-create-table参数

  17.代码生成参数,没看懂
  --bindir   Output directory for compiled objects
  --class-name   Sets the generated class name. This overrides --package-name. When combined with --jar-file, sets the input class.
  --jar-file   Disable code generation; use specified jar
  --outdir   Output directory for generated code
  --package-name   Put auto-generated classes in this package
  --map-column-java   Override default mapping from SQL type to Java type for configured columns.

  18.通过配置文件conf/sqoop-site.xml来配置常用参数
  
  1. <property>
  2.     <name>property.name</name>
  3.     <value>property.value</value>
  4. </property>
复制代码


  如果不在这里面配置的话,就需要像这样写命令
  
  1.     sqoop import -D property.name=property.value ...
复制代码


  
  
  19.两个特别的参数
  sqoop.bigdecimal.format.string  大decimal是否保存为string,如果保存为string就是 0.0000007,否则则为1E7
  sqoop.hbase.add.row.key          是否把作为rowkey的列也加到行数据当中,默认是false的
  
  20.例子
  
  1. #指定列
  2. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  3.     --columns "employee_id,first_name,last_name,job_title"
  4. #使用8个线程
  5. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  6.     -m 8
  7. #快速模式
  8. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  9.     --direct
  10. #使用sequencefile作为存储方式
  11. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  12.     --class-name com.foocorp.Employee --as-sequencefile
  13. #分隔符
  14. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  15.     --fields-terminated-by '\t' --lines-terminated-by '\n' \
  16.     --optionally-enclosed-by '"'
  17. #导入到hive
  18. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  19.     --hive-import
  20. #条件过滤
  21. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  22.     --where "start_date > '2010-01-01'"
  23. #用dept_id作为分个字段
  24. $ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
  25.     --split-by dept_id
  26. #追加导入
  27. $ sqoop import --connect jdbc:mysql://db.foo.com/somedb --table sometable \
  28.     --where "id > 100000" --target-dir /incremental_dataset --append
复制代码


  
   
  21.导入所有的表sqoop-import-all-tables
  每个表都要有主键,不能使用where条件过滤
  
  1.     sqoop import-all-tables --connect jdbc:mysql://db.foo.com/corp
复制代码

  
  
  22.export
  我们采用sqoop-export插入数据的时候,如果数据已经存在了,插入会失败
  如果我们使用--update-key,它会认为每个数据都是更新,比如我们使用下面这条语句
  
  1.    sqoop-export --table foo --update-key id --export-dir /path/to/data --connect …
  2.   UPDATE foo SET msg='this is a test', bar=42 WHERE id=0;
  3.   UPDATE foo SET msg='some more data', bar=100 WHERE id=1;
  4.   ...
复制代码


  这样即使找不到它也不会报错

  23.如果存在就更新,不存在就插入
  加上这个参数就可以啦--update-mode allowinsert
  
  
  24.事务的处理
  它会一次statement插入100条数据,然后每100个statement提交一次,所以一次就会提交10000条数据
  
  
  25.例子
  
  1. $ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar  \
  2.     --export-dir /results/bar_data
  3. $ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar  \
  4.     --export-dir /results/bar_data --validate
  5. $ sqoop export --connect jdbc:mysql://db.example.com/foo --call barproc \
  6.     --export-dir /results/bar_data
复制代码



  

本帖被以下淘专辑推荐:

已有(21)人评论

跳转到指定楼层
pig2 发表于 2014-11-13 08:36:13
(1)insert mode:生成insert语句然后执行,这是默认的方式
(2)update mode:生成update语句,替换数据库中的记录
(3)call mode:调用存储过程处理每一条记录

回复

使用道具 举报

pig2 发表于 2014-11-13 08:37:16
sqoop export   -D oracle.sessionTimeZone=CST --connect jdbc:oracle:thin:@192.168.78.6:1521:hexel \
--username TRX --password trx --table SQOOP_UPDATE  --fields-terminated-by  '\t' \
--export-dir  /user/root/sqoop_update/ -m 1 --update-key ID --update-mode allowinsert \
--input-null-string '\\N'  --input-null-non-string '\\N'

回复

使用道具 举报

韩克拉玛寒 发表于 2014-11-14 09:14:42
回复

使用道具 举报

hb1984 发表于 2014-11-14 20:33:31
谢谢楼主分享。               
回复

使用道具 举报

zqh_2014 发表于 2014-11-17 12:53:09
使用sqoop api接口,应该导入什么软件包.com.cloudera.sqoop?从哪下载??
回复

使用道具 举报

ouyang705 发表于 2014-12-26 16:18:34
主持楼主。正要这方面的东西
回复

使用道具 举报

wubaozhou 发表于 2015-1-1 18:00:35
回复

使用道具 举报

12下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条