Sqoop 之 数据迁移

2 篇文章 0 订阅


在Sqoop中,“导入”概念指:从非大数据集群(RDBMS)向大数据集群(HDFS,HIVE,HBASE)中传输数据,叫做:导入,即使用import关键字。

1.RDBMS到HDFS

  • 确定Mysql服务开启正常

  • 在Mysql中新建一张表并插入一些数据

create database student;
create table student (
sid int auto_increment primary key,
sname varchar(10),
age int default 20,
gender varchar(10) default 'male'
);

image-20200929151054395

  • 导入数据

1.1全部导入

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--delete-target-dir \
--target-dir /student \
--fields-terminated-by '\t' \
--split-by sid \
--m 2

image-20200929151331315

1.2导入指定列 --columns

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--columns sid,sname \
--delete-target-dir \
--target-dir /student \
--fields-terminated-by '\t' \
--split-by sid \
--m 2

提示:columns中如果涉及到多列,用逗号分隔,分隔时不要添加空格

image-20200929151722184

1.3导入指定行–where

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--where 'sid between 10 and 20' \
--delete-target-dir \
--target-dir /student \
--fields-terminated-by '\t' \
--split-by sid \
--m 2

image-20200929152433594

1.4查询导入

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--query "select sid from student where \$CONDITIONS" \
--delete-target-dir \
--target-dir /student \
--fields-terminated-by '\t' \
--split-by sid \
--m 1

提示:must contain \$CONDITIONS' in WHERE clause.
如果query后使用的是双引号,则​CONDITIONS前必须加转移符,防止shell识别为自己的变量。

image-20200929153322248

1.5增量导入数据

  • incremental指定增量导入的模式
  • append:追加数据记录
  • lastmodified:可追加更新的数据
sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--table student \
--where "sid>10" \
--username root \
--password ok \
--incremental append \
--check-column sid \
--last-value 10 \
--target-dir /student \
--fields-terminated-by '\t' \
--split-by sid \
--m 1

image-20200929164810465

1.6导入文件格式

  • –as-textfile 导入数据为text文件(默认)
  • –as-avrodatafile 导入数据为avro文件
  • –as-sequencefile 导入数据为sequence文件
  • –as-parquetfile 导入数据为parquet文件

2.RDBMS到Hive

导入必要jar包

cp /opt/hive/lib/hive-common-1.1.0-cdh5.14.2.jar /opt/sqoop/lib
cp /opt/hive/lib/hive-exec-1.1.0-cdh5.14.2.jar /opt/sqoop/lib

–create-hive-table:自动创建表,生产中一般不使用

–hive-overwrite:覆盖原有表数据

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--m 1 \
--hive-import \
--fields-terminated-by '\t' \
--hive-overwrite \
--create-hive-table \
--hive-database student \
--hive-table student

提示:该过程分为两步,第一步将数据导入到HDFS,第二步将导入到HDFS的数据迁移到Hive仓库,第一步默认的临时目录是/user/root/表名

image-20200929173436917

导入数据到Hive分区

sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--m 1 \
--hive-import \
--fields-terminated-by '\t' \
--hive-overwrite \
--create-hive-table \
--hive-database student \
--hive-table student \
--hive-partition-key "date" \
--hive-partition-value '20200929'

--hive-partition-key "date" \
--hive-partition-value '20200929'

指定分区字段和值

image-20200929182737296

3.RDBMS到Hbase

导入必要jar包

cp /opt/hbase/lib/* /opt/sqoop/lib/ -n
sqoop-import \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student \
--m 1 \
--hbase-create-table \
--hbase-table student \
--hbase-row-key sid \
--column-family info

提示:sqoop1.4.6只支持HBase1.0.1之前的版本的自动创建HBase表的功能

image-20200929185232718

4.HIVE/HDFS到RDBMS

sqoop-export \
--connect jdbc:mysql://hadoop100:3306/student \
--username root \
--password ok \
--table student2 \
--m 1 \
--export-dir /student \
--input-fields-terminated-by "\t"

提示:Mysql中如果表不存在,不会自动创建

image-20200929191345493

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值