• 分布式ETL工具Sqoop实践


    Mysql数据准备

    1、在node02节点登录Mysql

    mysql -uroot -proot
    
    • 1

    2、新建数据库testdb。

    create database testdb;
    
    • 1

    3、新建数据表ts。

    use testdb;
    create table ts(id int, name varchar(10), age int, sex char(1));
    
    • 1
    • 2

    4、向表中插入数据。

    insert into ts values(10001,'张三',18,'m');
    insert into ts values(10002,'李四',20,'m');
    insert into ts values(10003,'王小姐',18,'w');
    
    • 1
    • 2
    • 3

    Sqoop基本使用

    1、查看MySQL中的所有数据库。

    sqoop list-databases --connect jdbc:mysql://node02:3306/ --username root --password root
    
    • 1

    2、在/root目录下新建sqoop_file目录用于存放作业配置文件。

    mkdir /root/sqoop
    
    • 1

    3、在/root/sqoop_file目录编写配置文件list_dbs.conf,减少重复参数配置。

    vim list_dbs.conf
    # 添加以下内容
    list-databases
    --connect
    jdbc:mysql://node02:3306/
    --username
    root
    --password
    root
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    4、使用配置文件启动sqoop作业。

    sqoop --options-file list_dbs.conf
    
    • 1

    5、查看testdb中所有表,使用非明文方式,手动输入密码。

    sqoop list-tables \
       --connect jdbc:mysql://node02:3306/testdb \
       --username root \
       -P
    
    • 1
    • 2
    • 3
    • 4

    6、在Mysql中执行SQL并将结果展示在控制台。

    sqoop eval \
      --connect jdbc:mysql://node02:3306/testdb \
      --query "SELECT * FROM ts LIMIT 10" \
      --username root \
      -P
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7、将testdb.ts表中所有数据全量导入到hdfs的/tmp/sqoop/testdb/ts目录中。

    sqoop import \
       --connect jdbc:mysql://node02:3306/testdb \
       --username root \
       --query "select * from ts where \$CONDITIONS" \
       --target-dir /tmp/sqoop/testdb/ts \
       --fields-terminated-by "," \
       --hive-drop-import-delims \
       --split-by id \
       -m 1\
       -P
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    8、查看最终结果。

    hadoop fs -ls /tmp/sqoop/testdb/ts
    hadoop fs -cat /tmp/sqoop/testdb/ts/part-m-00000
    
    • 1
    • 2

    9、在MySQL中新增一条数据。

    insert into ts values(10004,'张飞',28,'m');
    
    • 1

    9、使用append增量方式将新增数据导入到/tmp/sqoop/testdb/ts/目录中。

    sqoop import \
       --connect jdbc:mysql://node02:3306/testdb \
       --username root \
       --password root \
       --query "select * from ts where \$CONDITIONS" \
       --target-dir /tmp/sqoop/testdb/ts/ \
       --split-by id \
       -m 1  \
       --incremental append \
       --check-column id \
       --last-value 10003
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    11、查看导入结果。

    hadoop fs -ls /tmp/sqoop/testdb/ts
    hadoop fs -cat /tmp/sqoop/testdb/ts/part-m-00001
    
    • 1
    • 2

    Sqoop Job操作

    将sqoop任务编写为sqoop job,能够减少每次作业执行前,进行的参数配置工作。

    1. 将增量导入任务制作为sqoop job。
    sqoop job --create im_ts \
       -- import \
       --connect jdbc:mysql://node02:3306/testdb \
       --username root \
       --password root \
       --query "select * from ts where \$CONDITIONS" \
       --target-dir /tmp/sqoop/testdb/ts/ \
       --split-by id \
       -m 1  \
       --incremental append \
       --check-column id \
       --last-value 10004
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 查看所有作业。
    sqoop job --list
    
    • 1
    1. 查看指定作业的详细信息。
    sqoop job --show im_ts
    
    • 1
    1. 在MySQL中新增数据。
    insert into ts values(10005,'赵子龙',27,'m');
    
    • 1
    1. 运行作业,增量导入数据,这里需要手动输入数据库密码,Sqoop job默认不保存。
    sqoop job --exec im_ts
    
    • 1
    1. 查看导入结果。
    hadoop fs -cat /tmp/sqoop/testdb/ts/part-m-00002
    
    • 1
    1. 再次在MySQL中新增数据。
    insert into ts values(10006,'悟空',5000,'m');
    
    • 1
    1. 运行作业,此时不需要手动修改--last-value,作业会自动根据上次的结果进行更新。
    sqoop job --exec im_ts
    
    • 1
    1. 修改$SQOOP_HOME/conf/sqoop-site.xml配置文件,可以使sqoop job自动保存密码。
      
        sqoop.metastore.client.record.password
        true
        If true, allow saved passwords in the metastore.
        
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    leetcode算法题--机器人的运动范围
    Elasticsearch 在地理信息空间索引的探索和演进
    Python BeautifulSoup4 入门使用
    【送面试题】GET与POST的区别
    SpringBoot+Dubbo+Nacos 开发实战教程
    Git实战技巧-如何查找哪一次提交导致了项目运行错误
    网页采集工具-免费的网页采集工具
    博云入选 Gartner 中国 DevOps 代表厂商
    MySQL数据库管理
    Cannot find module ‘prop-types‘
  • 原文地址:https://blog.csdn.net/qq_33876553/article/details/132966475