• 分布式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
  • 相关阅读:
    stream流
    【Python零基础入门篇 · 34】:进程间的通信-Queue、进程池的构建
    按照图片名称移动到文件夹中保存
    超火的低代码平台长什么样
    LeetCode刷题复盘笔记——491. 递增子序列(一文搞懂回溯解决递增子序列问题)
    小国王——状压DP
    股票交易接口list接口索引的方法
    如何通过SK集成chatGPT实现DotNet项目工程化?
    linux
    为什么大企业都更愿意采用私有化部署?
  • 原文地址:https://blog.csdn.net/qq_33876553/article/details/132966475