sqoop简介及基本简单应用实例

一、 sqoop简介

    Apache Sqoop™是一种旨在有效地在 Apache Hadoop 和诸如关系数据库等结构化数据存储之间传输大量数据的工具。
    2021年5月06日,Apache董事会宣布终止Apache Sqoop项目,但是我们依然可以继续使用Sqoop工具,只不过官方没有后续的bug修复了。由于Sqoop的实用性和普及性,我们还是有必要认识和了解Sqoop是如何使用的。

二、Sqoop 原理

   将导入或导出命令翻译成 mapreduce 程序来实现。在翻译出的 mapreduce 中主要是对 inputformat 和 outputformat 进行定制。
   在 Sqoop 中,“导入”概念指:从非大数据集群( RDBMS) 向大数据集群( HDFS, HIVE,HBASE) 中传输数据,叫做:导入,即使用 import 关键字。

三、常用语法实例

---------- 从hdfs导数据到mysql ----------

sqoop export \
--connect jdbc:mysql://hostname:端口号/数据库 \
--username 用户名\
--password 密码\
--table 表名 \
-m 1 \	启动1个map来导入数据,默认是4--export-dir /...hdfs中文件的路径 \
--fields-terminated-by '\t' 分隔符默认是Tab

---------- 从mysql导数据到hdfs ----------

//==========全量导入==========
sqoop import \
--connect jdbc:mysql://hostname:端口号/数据库 \
--username 用户名 \
--password 远程连接数据库的密码 \
--table 表名 \
-m 1 \
--delete-target-dir \
--target-dir /...hdfs文件保存的路径,到最后一层文件夹 \
--fields-terminated-by '\t' \
--lines-terminated-by '\n'


//==========列裁剪==========
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 用户名 \
--password 远程连接数据库的密码 \
--table 表名 \
--columns col1,col2,... 指定需要的列 \ 
-m 1 \ 
--delete-target-dir \
--target-dir /...hdfs文件保存的路径,到最后一层文件夹 \
--fields-terminated-by '\t' \
--lines-terminated-by '\n'


//=======行列裁剪+多个reducer===
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--table 表名 \
--columns col1,col2,... 指定需要的列 \
--where 指定行条件如:id>12 \
-m 2 \ 启动1个map来导入数据根据split-by来确定分割字段
--split-by col...(split项) \
--delete-target-dir \
--target-dir /.../.../...路径,到最后一层文件夹 \
--fields-terminated-by ',' \
--lines-terminated-by '\n'
//=======以下写法也能达到相同效果
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--query "select ... from ... where ... and \$CONDITIONS" \
-m 2 \
--split-by column_name \
--delete-target-dir \
--target-dir /.../.../... \
--fields-terminated-by ',' \
--lines-terminated-by '\n'

//==========增量导入append|merge
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--query "select ... from 更新后的表 where \$CONDITIONS" \ 
-m 1 \
--target-dir /.../.../... \
--fields-terminated-by ',' \
--lines-terminated-by '\n' \
--check-column col_name(:stuId) \
--incremental append \ 导入方式是append
--last-value ...(上次导出的check-column最后的值,:48)


//=========增量导入lastmodified=======
//该增量方式要根据表上次的修改时间来修改,所以表数据中必须要有时间这一列
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--query "select ... from 更新后的表 where \$CONDITIONS" \
-m 1 \
--target-dir /.../.../... \
--fields-terminated-by ',' \
--lines-terminated-by '\n' \
--check-column col_name \
--incremental lastmodified \
--append \
--last-value ...(:'2021-06-29 12:26:08')

//==========#分区表单分区导入===========
#mysql数据表
create table sqp_partition(
id int auto_increment primary key,
name varchar(20),
dotime datetime
);
insert into sqp_partition(name,dotime) values
('henry','2021-06-01 12:14:14'),
('pola','2021-06-01 12:44:14'),
('ariel','2021-06-01 12:54:14'),
('rose','2021-06-01 13:14:14'),
('jack','2021-06-01 13:33:14');
#hive数据表
create table sqp_partition(
id int,
name string,
dotime timestamp
)
partitioned by (dodate date)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;
#sqoop语句
sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--table sqp_partition \
--where "cast(dotime as date)='2021-06-01'" \
-m 1 \
--delete-target-dir \
--target-dir /user/hive/warehouse/test.db/sqp_partition/dodate=2021-06-01 \(hive表数据存放目录)
--fields-terminated-by ',' \
--lines-terminated-by '\n'
#hive语句
alter table sqp_partition add partition(dodate='2021-06-01');

//============分区导入=============
create table sqp_user_par1(
stuId int,
stuName string,
stuAge int,
mobile string,
tutition decimal(10,2),
fkClassId string
)
partitioned by (id_range string)
row format delimited
fields terminated by ','
lines terminated by '\n'
stored as textfile;

sqoop import \
--connect jdbc:mysql://你的hostname:端口号/数据库 \
--username 数据库用户名 \
--password 远程连接数据库的密码 \
--table studentinfo \
--where "stuId between 1 and 20" \
-m 1 \
--hive-import \
--hive-table test.sqp_user_par1 \
--hive-partition-key id_range \
--hive-partition-value '1-20' \
--fields-terminated-by ',' \
--lines-terminated-by '\n'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值