sqoop基本操作

1、显示数据库

sqoop list-databases \
--connect jdbc:mysql://192.168.10.151:3306 \
--username root \
--password 123456

2、显示某数据库下的表

sqoop list-tables \
--connect jdbc:mysql://192.168.10.151:3306/test \
--username root \
--password 123456

3、mysql导入hive

sqoop import  \
--connect jdbc:mysql://192.168.10.151:3306/test  \
--username root  \
--password 123456  \
--table userinfo  \
--hive-import  \
--hive-overwrite  \
--fields-terminated-by "\t"  \
--lines-terminated-by "\n"  \
--create-hive-table  \
--delete-target-dir \
--hive-database hivetest \
--hive-table hive_userinfo

hive 参数

  • --hive-import #必须参数,指定导入hive
  • --hive-overwrite #重写重复字段
  • --create-hive-table #创建hive表,但是表存在会出错。不建议使用这个参数,因为到导入的时候,字段类型有出入
  • --delete-target-dir #数据会先加载到HDFS上然后再导入到Hive中,指定该参数将删除HDFS的中间数据
  • --hive-database hivetest #指定hive库名
  • --hive-table hive_userinfo #指定hive表名
  • --fields-terminated-by "\t" #hive的列分隔符
  • --lines-terminated-by "\n" #hive的行分隔符
  • --hive-overwrite #重写重复字段
  • --hive-partition-key "id" #指定分区表的字段
  • --hive-partition-value "2019-06-28" #指定分区表的值
  • --null-string '' #任意字符串类型的空置用空白替换,如char、varchar,string等类型
  • --null-non-string '0' #非字符串类型的空置用0替换

其他参数

  • --direct #提高数据库到hadoop的传输速度,mysql5.0 以上,oracle 10.2.0 以上,直连模式,速度更快(HBase不支持)(需要在所有的slave节点上存在“mysqldump"可执行文件,最快的解决方法是在每一台slave上安装mysql数据库,或者将mysqldump复制到slave上并将其加入到环境变量PATH中即可。参考:http://blog.sina.com.cn/s/blog_af56c5560102x0so.html
  • -z,--compress #开启压缩
  • -m,--num-mappers <n>    #建立n个并发执行task import
  • --mapreduce-job-name XXX #默认是要导入的表的名称加上.job,就是“userinfo.job
  • --where "age>18" #匹配条件,如果有双引号,注意转义 \$CONDITIONS,不能用or,子查询,join
  • --columns "name,age" #选择要导入的指定列
  • -e,--query 'select * from people where age>18 and $CONDITIONS' #sql语句查询的结果集
  1. 不能与 --table 一起使用
  2. 需要指定 --target-dir 路径(HDFS存储路径)

4、增量导入(mysql->hive)

sqoop import  \
--connect jdbc:mysql://192.168.10.151:3306/test  \
--username root  \
--password 123456  \
--table userinfo  \
--hive-import  \
--hive-database  hivetest  \
--hive-table hive_userinfo  \
--m 1 \ 
--incremental append \ 
--check-column id \ 
--last-value 1208

参数

  • --check-column #指定一些列,导入时判断是否要被作为增量数据,在一般关系型数据库中,都存在类似updatetime的字段或主键。注意:被检查的列不能是任意字符类型,例如char,varchar等
  • --incremental #用来指定增量导入的模式(Mode),append和lastmodified
  • --last-value #指定上一次导入中检查列指定字段最大值
--incremental append \ #或lastmodified
--check-column updatetime \
--last-value "2019-06-28"

注意:增量导入不能与 --delete-target-dir 一起使用,必须指定增量的模式 

5、 数据导出

sqoop export \
--connect jdbc:mysql://192.168.10.151:3306/test \
--username root \
--password 123456 \
-m 2 \
--mapreduce-job-name hdfs2mysql \
--table a \
--export-dir /user/hadoop/a \ #从HDFS导出数据
--input-fields-terminated-by '\t' \
--input-null-string '' \
--input-null-non-string '0'


sqoop export \
--connect jdbc:mysql://192.168.10.151:3306/test \
--username root \
--password 123456 \
-m 2 \
--mapreduce-job-name hive2mysql \
--table a \
--export-dir /apps/hive/warehouse/test.db/a \ #从hive导出数据
--input-fields-terminated-by '\t'

#增量导出
sqoop export \
--connect jdbc:mysql://192.168.3.94:3306/test \
--username root \
--password 123456 \
--mapreduce-job-name hive2mysql \
--table userinfo_copy  \
--fields-terminated-by '|||'  \
--update-key id  \
--update-mode allowinsert  \
--export-dir /user/hive/warehouse/userinfo

6、删除mysql数据库数据

sqoop eval  \
--connect jdbc:mysql://192.168.3.94:3306/test  \
--username root  \
--password 123456  \
--query "delete from userinfo"

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sxjxrxm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值