分享

Sqoop

 印度阿三17 2019-08-24

                                                 Sqoop

1 什么是Sqoop

sqoop 是Apache的一款“Hadoop 和关系数据库服务器之间传送数据”的工具,Sqoop 的本质是一个命令行工具。

2 Sqoop产生背景

早期数据存储是基于传统的关系型数据库的,但是随着数据量的增长,传统的数据存储方式无法满足需求,随着出现的HDFS分布式文件存储系统等解决方案。那么sqoop就解决了传统的关系型数据库的数据要迁移到大数据平台的问题。

3 数据导入方向

以大数据平台为中心

数据迁入import  :mysql|oracle等关系型数据量 ----> hadoop平台(hdfs,hive,hbase等)

数据迁出 export  : hadoop平台数据----》 mysql|oracle

4 Sqoop的本质(工作机制)

将导入或导出命令翻译成 MapReduce 程序来实现,在翻译出的MapReduce 中主要是对 InputFormat 和 OutputFormat 进行定制

(1)sqoop数据迁入:从mysql读取数据  将数据输出到hdfs

map端:

           默认的FileInputFormat是针对文本的,需要重新定义输入为DBInputFormat

           将数据库的数据,一行一行的读取过来

           map() {输出}

重新定义了数据输入类 InputFormat,只需要maptask 

(2)sqoop数据迁出:从hdfs 读取数据,将数据迁出到 mysql中

map端:   

           输入不用变,FileInputFormat   一行一行的读取过来

           map(){

                      将数据输出到mysql中

                      重新定义  OutputFormat--> DBOutputFormat  

           }
    重新定义了OutputFormat,只需要maptask

5 Sqoop安装

(1)上传,解压,配置环境变量

(2)修改配置文件:mv sqoop-env-template.sh sqoop-env.sh 


#Set path to where bin/hadoop is available
export HADOOP_COMMON_HOME=/home/refuel/opt/module/hadoop-2.7.7

#Set path to where hadoop-*-core.jar is available
export HADOOP_MAPRED_HOME=/home/refuel/opt/module/hadoop-2.7.7

#set the path to where bin/hbase is available
export HBASE_HOME=/home/home/refuel/opt/module/hbase-1.2.6

#Set the path to where bin/hive is available
export HIVE_HOME=/home/home/refuel/opt/module/apache-hive-2.3.2-bin

#Set the path for where zookeper config dir is
export ZOOCFGDIR=/home/refuel/opt/module/zookeeper-3.4.10/conf

由于CDH版本的Haoop的Yarn和haoop是分开安装的,所以有两个haoop目录需要配置

(3)将mysql的驱动添加到sqoop的lib下

(4)测试

查看mysql中所有数据库
sqoop list-databases --connect jdbc:mysql://bigdata01:3306/ --username root --password 123456

6 Sqoop操作

6.1 数据导入

参数说明:
--connect     指定mysql连接
--password    指定mysql的密码
--username    指定mysql的用户名
-m   指定maptask的任务数量 
--target-dir   指定hdfs的输出目录
--fields-terminated-by  指定输入到hdfs的文件的字段之间的分隔符
--columns      指定需要导入的列
--where        指定过滤条件
--split-by    指定切分maptask的依据字段的
--table       指定mysql中需要导入到大数据平台的表名

(1)Mysql导入到Hdfs

①普通导入

sqoop import --connect jdbc:mysql://bigdata01:3306/mysql --username root --password 123456 --table help_keyword -m 1

默认输出目录  /user/用户名/表名
默认的分隔符 ,

②指定输出目录和分隔符 

sqoop import --connect jdbc:mysql://bigdata01:3306/mysql --username root --password 123456 --table help_keyword --target-dir /user/data01/mydata/help_keyword --fields-terminated-by '\t' -m 1

③指定需要导入的字段

// 查询指定列
sqoop import   --connect jdbc:mysql://bigdata01:3306/mysql   --username root  --password 123456 --columns "name" --table help_keyword  --target-dir /user/sqoop/outputdata01  -m 1

④指定过滤条件

sqoop import   --connect jdbc:mysql://bigdata01:3306/mysql   --username root  --password 123456   --columns "name" --where "help_keyword_id>100" --table help_keyword  --target-dir /user/sqoop/outputdata02  -m 1

⑤指定sql查询语句结果导入

sqoop import   --connect jdbc:mysql://bigdata01:3306/mysql   --username root  --password 123456   --query 'select * from help_keyword where  help_keyword_id>200 and $CONDITIONS' --target-dir /user/sqoop/outputdata03  -m 1

注意:
    ① --query 和  --where  --columns  --table不可以一起使用的
    ②报错
    Query [select * from help_keyword where help_keyword_id>200] must contain '$CONDITIONS' in WHERE clause
    $CONDITIONS 没有实际含义,但语法要求要有
    ③)使用单引号

⑥指定启动多个maptask任务

sqoop import   --connect jdbc:mysql://bigdata01:3306/  --username root  --password 123456   --target-dir /user/sqoop/outputdata04  --query 'select * from mysql.help_keyword where help_keyword_id > 100 and $CONDITIONS' --split-by  help_keyword_id --fields-terminated-by '\t'  -m 3

注意: -m是多个   必须配合 --split-by 整型,可以是自增主键  须配合plit-by不然会报错,
多个maptask任务数据的划分是先获取最小id,然后获取最大id (最大id-最小id  1 ) /maptask 求的是每一个maptask分配的数据条数,每一个maptask顺序获取数据,所以也是有可能造成数据倾斜的

(2)Mysql导入到Hive 

步骤:①先把这个数据导入到hdfs的默认路径下;②在hive中建表;③将hdfs的文件  加载到hive的表中

注意:在sqoop导入数据到hive 时候,默认会建表,但是数据库不会创建的,数据库需要手动创建的

参数说明
--hive-import   指定导入到hive中
--hive-overwrite   覆盖导入
--hive-database  指定数据库
--hive-table 指定hive中的表

①普通导入

sqoop import   --connect jdbc:mysql://bigdata01:3306/mysql   --username root  --password 123456   --table help_keyword   --hive-import -m 1

②指定导入的hive的库和表

sqoop import  --connect jdbc:mysql://bigdata01:3306/mysql  --username root  --password 123456  --table help_keyword  --fields-terminated-by "\t"  --lines-terminated-by "\n"  --hive-import  --hive-overwrite  --create-hive-table  --delete-target-dir --hive-database  test_sqoop --hive-table new_help_keyword

(3)Mysql导入到HBase

参数说明
--hbase-table  指定hbase的表名
--column-family 指定hbase对应的列族下
--hbase-row-key 指定hbase的rowkey 
注意:hbase中的表  需要手动创建的
create "new_help_keyword","info"
sqoop import --connect jdbc:mysql://bigdata01:3306/mysql --username root --password 123456 --table help_keyword --hbase-table new_help_keyword --column-family info --hbase-row-key help_keyword_id

(4)增量数据导入

全量数据导入:每次导入全部的数据

增量数据导入:每次导入新增的数据

三个重要参数
Incremental import arguments:

   --check-column <column>        Source column to check for incremental change  校验键,一般选主键,因为主键是自增

   --incremental <import-type>    Define an incremental import of type 'append' or 'lastmodified'  指定增量导入的类型
        
        append  追加
        lastmodified  最后一次修改的时间或建

   --last-value <value>           Last imported value in the incremental check column  指定上一次的最后最后一个增量的建的值,这次导入则是从这个值的下一个值开始导入
   
sqoop import   --connect jdbc:mysql://bigdata01:3306/mysql   --username root  --password 123456   --table help_keyword  --target-dir /user/outputdata06  --incremental  append  --check-column  help_keyword_id --last-value 200  -m 1

6.2 数据导出

(1)Hdfs导出到Mysql

sqoop export --connect jdbc:mysql://bigdata01:3306/sqoopdb  --username root --password 123456 --table sqoopfur --export-dir /user/outputdata03/help_keyword --fields-terminated-by '\t'



--export-dir 指定导出的hdfs的路径
--fields-terminated-by  指定hdfs的文件的字段分割符
--table  指定的导出的mysql中的表

mysql中的数据库和表都需要自己手动创建的
create database sqoopdb;
use sqoopdb;
CREATE TABLE sqoopfur ( 
   id INT, 
   name VARCHAR(60)
);

(2)Hive导出到Mysq

sqoop export --connect jdbc:mysql://bigdata01:3306/sqoopdb --username root --password 123456 --table uv_info --export-dir /user/hive/warehouse/test_sqoop.db/new_help_keyword --input-fields-terminated-by '\t'

--export-dir   指定hive表所在的hdfs的路径
--input-fields-terminated-by  指定hive的表文件的分隔符

CREATE TABLE uv_info ( 
   id INT, 
   name VARCHAR(60)
);

(3)HBase导入Mysql

没有一种直接的方式 可以hbase的数据直接导出mysql中。可以将hbase和hive整合,导出mysql 

来源:https://www./content-4-406851.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多