大数据7_03_Sqoop命令操作

3 Sqoop命令操作

3.1查看mysql中的数据库

[atguigu@hadoop102 sqoop]$ bin/sqoop list-databases --connect jdbc:mysql://hadoop102:3306/ --username root -password 123456

出现如下输出:

information_schema
metastore
mysql
oozie
performance_schema

3.2 查看mysql中的表

[atguigu@hadoop102 sqoop]$ bin/sqoop list-tables --connect jdbc:mysql://hadoop102:3306/gmall --username root -password 123456

3.3 导入数据到HDFS

  • target-dir :目标路径
  • delete-target-dir :删除目标路径,以为sqoop是mr所以,对于输出的文件不能存在。
  • num-mappers :切片数:sqoop的切片规则是根据这个参数决定。
  • fields-terminated-by输出的字段以什么分割
  • split-by根据后面的字段去切分。
    • 这种切分有一个缺点是:有可能会切的不平均。
  • 有几个map就有几个文件,因为没有reducer
  • target-dir怎么去确定是HDFS还是本地呢?
    • 这是因为配置HADOOP_HOME。
    • 不配置的话需要些绝对路径:hdfs://hadoop102:8020/test
方式1:
bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/gmall \
--username root \
--password 123456 \
--table user_info \
--columns id,login_name \
--where "id>=10 and id<=30" \
--target-dir /origin_data/db2/do_data \
--delete-target-dir \
--num-mappers 3 \
--fields-terminated-by '\t' \
--split-by id
方式2:
  • query “sql语句” 必须有一个where $CONDITIONS
    • 如果有过滤条件:where 过滤条件 and $CONDITIONS
    • 如果没有过滤条件:where $CONDITIONS
bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/gmall \
--username root \
--password 123456 \
--target-dir /origin_data/$APP/db/$1/$do_date \
--delete-target-dir \
--query "select id, login_name from user_info where id >=10 and id <= 30 and \$CONDITIONS" \
--num-mappers 1 \
--fields-terminated-by '\t'

3.4 指定压缩和压缩格式

--compress \
--compression-codec lzop \
bin/sqoop import \
--connect jdbc:mysql://hadoop102:3306/gmall \
--username root \
--password 123456 \
--query "select id,login_name from user_info where \$CONDITIONS" \
--target-dir /origin_data/test1 \
--delete-target-dir \
--num-mappers 3 \
--fields-terminated-by '\t' \
--split-by id \
--compress \
--compression-codec lzop \

image-20201109213129064

3.5 null值处理

  • mysql中空的字符串和空的非字符串 都替换成\N
  • 这是因为hive中默认的空为:\N
  • 另外双反斜杠是\ \前面的是转义。
--null-string '\\N' \
--null-non-string '\\N'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

最佳第六六六人

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

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

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

打赏作者

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

抵扣说明:

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

余额充值