Sqoop 数据迁移

sqoop原理

  1. 将导入或导出命令翻译成 MapReduce 程序来实现。
  2. 在翻译出的 MapReduce 中主要是对 inputformat 和 outputformat 进行定制

数据库的准备

登录mysql

mysql -u user -p password

创建数据库

create database database_name; 

导入数据

source /root/text/retail_db.sql;

sqoop的使用

导入一个表到 hdfs(需要转义换行符)

sqoop import \
--connect jdbc:mysql://seven:3306/detail \
--driver com.mysql.jdbc.Driver \
--table customers \
--username root \
--password aqws199785 \
--target-dir /data/retail_db/customers \
-m 3

通过where 语句过滤导入表

sqoop import \
--connect jdbc:mysql://seven:3306/detail \
--table orders \
--where "order_id < 500" \
--username root \
--password aqws199785 \
--delete-target-dir \
--target-dir /data/orders \
-m 4

通过columns导入表

sqoop import \
--connect jdbc:mysql://seven:3306/detail \
--table orders \
--columns "order_id,order_date,order_customer_id" \
--username root \
--password aqws199785 \
--delete-target-dir \
--target-dir /data/orders \
-m 3

使用query方式导入数据

sqoop import \
--connect jdbc:mysql://seven:3306/detail \
--query "select * from orders where order_status != 'closed' and \$CONDITIONS" \
--username root \
--password aqws199785 \
--split-by order_status \
--delete-target-dir \
--target-dir /data/order_query \
-m 3

增量导入(在原先的基础上添加数据)

sqoop import \
--connect jdbc:mysql://seven:3306/detail \
--driver com.mysql.jdbc.Driver \
--table orders \
--username root \
--password aqws199785 \
--incremental append \
--check-column order_date \
--last-value '2014-04-15' \
--target-dir /data/orders  \
--m 3
sqoop job
--create order2HDFS -- import \
--connect jdbc:mysql://seven:3306/retail
--driver com.mysql.jdbc

执行job

sqoop job --exec order2HDFS

直接导入数据到Hive

sqoop import \
--connect jdbc:mysql://hadoop101:3306/retail_db \
--driver com.mysql.jdbc.Driver \
--table orders \
--username root \
--passsword 123456 \
--hive-import \
--create-hive-table \
--hive-database retails \
--hive-table orders \
--hive-overwrite \
-m 3

导入数据到hive的分区表

sqoop import /
--connect jdbc:mysql://sever:3306/detail
--dirver com.mysql.jdbc.Driver
--query "select order_id,order_status from orders where order_date>='2013-11-03' and order_date <'2013-11-04' and \$conditions"

导入到 hbase

需要提前创建一张表

导入
需要在mysql建表

create table customerdemo like customers;
sqoop export \
--connect jdbc:mysql://seven:3306/detail \
--username root \
--password aqws199785 \
--table customerdemo \
--export-dir /data/retail_db/customers \
-m 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值