DataX read MySQL example

MySQL

  • Container preparation
    docker pull mysql:5.6
    docker run -p 3306:3306 --name hello_mysql -d -e MYSQL_ROOT_PASSWORD=root mysql:5.6
    docker exec -it hello_mysql bash
  • data preparation
  1. mysql -uroot -prootConnect to mysql
    Insert picture description here
  2. Create a database first
create database db_datax;
use db_datax
  1. Create a table and insert data
create table if not exists tb_employee (id int, salary int);
truncate table tb_employee ;
insert into tb_employee (id, salary) values ('1', '100');
insert into tb_employee  (id, salary) values ('2', '200');
insert into tb_employee (id, salary) values ('3', '300');

After the data is ready, you can exit the container, but do not close it

  • View ip
    docker inspect --format='{ {.NetworkSettings.IPAddress}}' hello_mysql
    Insert picture description here

DataX

ping 172.17.0.3First test whether the DataX container can access the MySQL container. Generally speaking, the default network is used, and the two containers should be able to communicate normally.

cd /opt/modules/datax
vi ./job/mysql2stream.json

{
    "job": {
        "setting": {
            "speed": {
                 "channel": 3
            },
            "errorLimit": {
                "record": 0,
                "percentage": 0.02
            }
        },
        "content": [
            {
                "reader": {
                    "name": "mysqlreader",
                    "parameter": {
                        "username": "root",
                        "password": "root",
                        "column": [
                            "id",
                            "salary"
                        ],
                        "splitPk": "id",
                        "connection": [
                            {
                                "table": [
                                    "tb_employee"
                                ],
                                "jdbcUrl": [
     "jdbc:mysql://172.17.0.3:3306/db_datax"
                                ]
                            }
                        ]
                    }
                },
               "writer": {
                    "name": "streamwriter",
                    "parameter": {
                        "print":true
                    }
                }
            }
        ]
    }
}

python ./bin/datax.py ./job/mysql2stream.json
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44112790/article/details/110244565