Sqoop介绍、安装及使用

Sqoop介绍、安装及使用

1、介绍

在这里插入图片描述
Apache Sqoop是在Hadoop生态体系和RDBMS体系之间传送数据的一种工具
实质:是将导入或导出命令翻译成mapreduce程序来实现。在翻译出的mapreduce中主要是对inputformatoutputformat进行定制

2、安装

前提:具备java和Hadoop环境
在官网下载对应压缩包:sqoop
解压后,修改配置文件:
cd sqoop/conf/
mv sqoop-env-template.sh sqoop-env.sh
vi sqoop-env.sh
export HADOOP_COMMON_HOME= hadoop路径
export HADOOP_MAPRED_HOME= hadoop路径
export HIVE_HOME= hive路径
export HBASE_HOME=hbase路径
拷贝mysql的jdbc驱动包到lib目录下
验证:

bin/sqoop list-databases \
 --connect jdbc:mysql://localhost:3306/ \
 --username root --password mysql

本命令会列出所有mysql的数据库。

3、Sqoop导入

1)导入:导入单个表从RDBMS到HDFS
下面的命令用于从Mysql数据库中的demo表导入到HDFS

[root@hadoop01 sqoop]# bin/sqoop import \
> --connect jdbc:mysql://hadoop01:3306/test \
> --username root \
> --password mysql \
> --delete-target-dir \
> --target-dir /input/sqoop/test \
> --table demo -m 1

在这里插入图片描述
在这里插入图片描述
2)全量导入mysql表数据到Hive

[root@hadoop01 sqoop]# bin/sqoop import \
> --connect jdbc:mysql://hadoop01:3306/test \
> --username root \
> --password mysql \
> --table demo \
> --hive-import \
> --m 1 \
> --hive-database default;

在这里插入图片描述
3)导入表数据子集(where)
–where可以指定从关系数据库导入数据时的查询条件

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/sqoopdb \
--username root \
--password mysql \
--where "city ='sec-bad'" \
--target-dir /wherequery \
--table emp_add --m 1

4)导入表数据子集(query)
使用query sql语句来进行查找不能加参数–table ;
并且必须要添加where条件;
并且where条件后面必须带一个$CONDITIONS 这个字符串;
并且这个sql语句必须用单引号,不能用双引号;

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root \
--password mysql \
--target-dir /wherequery12 \
--query 'select id,name,deg from emp WHERE  id>1203 and $CONDITIONS' \
--split-by id \
--fields-terminated-by '\t' \
--m 2

–split-by id通常配合-m 10参数使用。用于指定根据哪个字段进行划分并启动多少个maptask。

5)增量导入
增量导入是仅导入新添加的表中的行的技术
参数:
–incremental 指定增量导入的模式 append追加模式 lastmodified 上次更新模式
–check-column 指定判断检查的字段
–last-value 指定判断的值

Append模式增量导入

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root  --password mysql \
--table emp --m 1 \
--target-dir /appendresult \
--incremental append \
--check-column id \
--last-value  1205

Lastmodified模式增量导入

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root \
--password mysql \
--table customertest \
--target-dir /lastmodifiedresult \
--check-column last_mod \
--incremental lastmodified \
--last-value "2020-02-25 18:42:06" \
--m 1 \
--append

Lastmodified模式:merge-key
使用lastmodified模式进行增量处理要指定增量数据是以append模式(附加)还是merge-key(合并)模式添加
merge-key:
合并模式,新增增量数据,更新已有的数据,并且不会重复

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root \
--password mysql \
--table customertest \
--target-dir /lastmodifiedresult \
--check-column last_mod \
--incremental lastmodified \
--last-value "2020-02-25 18:42:06" \
--m 1 \
--merge-key id

4、Sqoop导出

将数据从Hadoop生态体系导出到RDBMS数据库导出前,目标表必须存在于目标数据库中

默认模式导出HDFS数据到Mysql
–input-fields-terminated-by ‘\t’

bin/sqoop export \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root \
--password mysql \
--table employee \
--export-dir /emp/emp_data

更新导出(updateonly模式)
– updatemod,指定updateonly(默认模式)
– update-key,更新标识,即根据某个字段进行更新

bin/sqoop export \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root --password mysql \
--table updateonly \
--export-dir /updateonly_2/ \
--update-key id \
--update-mode updateonly

更新导出(allowinsert模式)

bin/sqoop export \
--connect jdbc:mysql://hadoop01:3306/userdb \
--username root --password mysql \
--table allowinsert \
--export-dir /allowinsert_2/ \
--update-key id \
--update-mode allowinsert

5、Sqoop集成Hbase

导入:

bin/sqoop import \
--connect jdbc:mysql://hadoop01:3306/library \
--username root \
--password mysql \
--table book \
--columns "id,name,price" \			
--hbase-table "hbase_book" \		//表名
--hbase-create-table \
--column-family "info" \			//列族
--hbase-row-key "id" \			  //rowKey
-m 1 \
--split-by id 

导出:
Hbase→hive外部表→hive内部表→通过sqoop→mysql

发布了14 篇原创文章 · 获赞 12 · 访问量 818

猜你喜欢

转载自blog.csdn.net/qq_45094921/article/details/104506785