sqoop常用命令整理


1、列出mysql数据库中的所有数据库
 sqoop list-databases --connect jdbc:mysql://localhost:3306/ -username root -password 000000
2、连接mysql并列出数据库中的表
sqoop list-tables --connect jdbc:mysql://localhost:3306/test --username root --password 000000
3、把数据从mysql导入到hdfs中
Sqoop import --table avatar --fields-terminated-by '==' -m 1 --connect jdbc:mysql://localhost:3306/test --username root --password 000000
写sql语句导入的方式:
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 1 用1个map,多个的话,记录的顺序变了):   
    sqoop import \
  --query 'SELECT a.*, b.* FROM a JOIN b on (a.id == b.id) WHERE $CONDITIONS' \
  -m 1 --target-dir /user/foo/joinresults
注意:默认导入到hdfs的当前目录中
如果where语句中有要用单引号的,就像这样子写就可以"SELECT * FROM x WHERE a='foo' AND \$CONDITIONS"
--split-by表示划分列,默认是主键,见《hadoop权威指南》
ERROR tool.ImportTool: Imported Failed: There is no column found in the target table t_guide. Please ensure that your table name is correct. 将表名改成大写就ok了
4、将关系型数据的表结构复制到hive中
sqoop create-Hive-table --connect jdbc:mysql://localhost:3306/test --table users --username root --password 000000 --hive-table users  --fields-terminated-by "\0001"  --lines-terminated-by "\n";
参数说明:
--fields-terminated-by "\0001"  是设置每列之间的分隔符,"\0001"是ASCII码中的1,它也是hive的默认行内分隔符, 而sqoop的默认行内分隔符为"," 
--lines-terminated-by "\n"  设置的是每行之间的分隔符,此处为换行符,也是默认的分隔符;
注意:只是复制mysql表的结构,mysql表中的内容没有复制,复制得到hive表,但hive表中无内容
5、将数据从关系数据库导入文件到hive表中
sqoop import --connect jdbc:mysql://localhost:3306/test --username root --password 000000 
--table users --hive-import --hive-table users -m 2 --fields-terminated-by "\0001";
参数说明:
-m 2 表示由两个map作业执行;
--fields-terminated-by "\0001"  需同创建hive表时保持一致;
注意:先会更具mysql表的结构得到hive表,然后将mysql表中的数据导入到hive表(也就是导入到HDFS,因为hive表的数据是存在hdfs上的warehouse)


6、将hive中的表数据导入到mysql数据库表中
sqoop export --connect jdbc:mysql://192.168.20.118:3306/test --username root --password 000000 
--table users --export-dir /user/hive/warehouse/users/part-m-00000 
--input-fields-terminated-by '\0001'
注意:
1、在进行导入之前,mysql中的表userst必须已经提前创建好了。
2、jdbc:mysql://192.168.20.118:3306/test中的IP地址改成localhost会报异常,因为是集群


7、将数据从关系数据库导入文件到hive表中,--columns  --where 语句使用
sqoop import --append --connect jdbc:mysql://192.168.20.118:3306/test --username root --password 000000 --table userinfos --columns "id,age,name"  --where "id > 3 and (age = 88 or age = 80)"  -m 1  --target-dir /user/hive/warehouse/userinfos2 --fields-terminated-by ",";
注意:--target-dir /user/hive/warehouse/userinfos2   可以用  --hive-import --hive-table userinfos2 进行替换


8.    1.4.3版本的sqoop不支持复杂的sql语句,不支持or语句
--direct 是为了利用某些数据库本身提供的快速导入导出数据的工具,比如mysql的mysqldump性能比jdbc更好,但是不知大对象的列,使用的时候,那些快速导入的工具的客户端必须的shell脚本的目录下,见《hadoop权威指南》


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


9.增加导入
  --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.
增加导入支持两种模式append和lastmodified,用--incremental来指定


10.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类型
11.hive空值处理
  sqoop会自动把NULL转换为null处理,但是hive中默认是把\N来表示null,因为预先处理不会生效的
  我们需要使用 --null-string 和 --null-non-string来处理空值 把\N转为\\N
sqoop import  ... --null-string '\\N' --null-non-string '\\N'
12.导入数据到hbase
  导入的时候加上--hbase-table,它就会把内容导入到hbase当中,默认是用主键作为split列
  也可以用--hbase-row-key来指定,列族用--column-family来指定,它不支持--direct。
  如果不想手动建表或者列族,就用--hbase-create-table参数
13.代码生成参数
  --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.
14.通过配置文件conf/sqoop-site.xml来配置常用参数
<property>
  <name>property.name</name>
   <value>property.value</value>
</property>
  如果不在这里面配置的话,就需要像这样写命令
sqoop import -D property.name=property.value ...


15.两个特别的参数 
  sqoop.bigdecimal.format.string  大decimal是否保存为string,如果保存为string就是 0.0000007,否则则为1E7
  sqoop.hbase.add.row.key          是否把作为rowkey的列也加到行数据当中,默认是false的
16.例子
#指定列
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --columns "employee_id,first_name,last_name,job_title"
#使用8个线程
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    -m 8
#快速模式
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --direct
#使用sequencefile作为存储方式
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --class-name com.foocorp.Employee --as-sequencefile
#分隔符
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --fields-terminated-by '\t' --lines-terminated-by '\n' \
    --optionally-enclosed-by '\"'
#导入到hive
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --hive-import
#条件过滤
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --where "start_date > '2010-01-01'"
#用dept_id作为分个字段
$ sqoop import --connect jdbc:mysql://db.foo.com/corp --table EMPLOYEES \
    --split-by dept_id
#追加导入
$ sqoop import --connect jdbc:mysql://db.foo.com/somedb --table sometable \
--where "id > 100000" --target-dir /incremental_dataset –append
17.导入所有的表sqoop-import-all-tables
  每个表都要有主键,不能使用where条件过滤
 sqoop import-all-tables --connect jdbc:mysql://db.foo.com/corp
18.export
  我们采用sqoop-export插入数据的时候,如果数据已经存在了,插入会失败
  如果我们使用--update-key,它会认为每个数据都是更新,比如我们使用下面这条语句
   sqoop-export --table foo --update-key id --export-dir /path/to/data --connect …
  UPDATE foo SET msg='this is a test', bar=42 WHERE id=0;
  UPDATE foo SET msg='some more data', bar=100 WHERE id=1;
...
这样即使找不到它也不会报错
19.如果存在就更新,不存在就插入
  加上这个参数就可以啦--update-mode allowinsert
20.事务的处理
  它会一次statement插入100条数据,然后每100个statement提交一次,所以一次就会提交10000条数据
21.例子 
$ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar  \
    --export-dir /results/bar_data
$ sqoop export --connect jdbc:mysql://db.example.com/foo --table bar  \
    --export-dir /results/bar_data --validate
$ sqoop export --connect jdbc:mysql://db.example.com/foo --call barproc \
    --export-dir /results/bar_data









  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值