当前位置: 首页 > 工具软件 > py-postgresql > 使用案例 >

Datax(2)-读写MySQL-To-PostgreSQL

闻人献
2023-12-01

实现:使用DataX读取MYSQL到PG
环境搭建,参照
Datax(1)-读写MYSQL-To-MYSQL-(Win10安装)

一:PG建表

t_user_info

CREATE TABLE "public"."t_user_info" (
"id" int4 NOT NULL,
"name" varchar(255) COLLATE "default",
"content" text COLLATE "default",
"createdate" varchar(32) COLLATE "default",
CONSTRAINT "t_user_info_pkey" PRIMARY KEY ("id")
)
WITH (OIDS=FALSE);

ALTER TABLE "public"."t_user_info" OWNER TO "postgres";

二:编写脚本mysql-2-pg.json

{
    "job": {
        "setting": {
            "speed": {
                "channel": 1
            }
        },
        "content": [
            {
                "reader": {
                    "name": "mysqlreader",
                    "parameter": {
                        "username": "root",
                        "password": "111111",
                        "column": [ "id", "name","content" ,"createdate"],
                        "splitPk": "id",
                        "connection": [
                            {
                                "table": [
                                    "t_user_info"
                                ],
                                "jdbcUrl": [
                                    "jdbc:mysql://127.0.0.1:3306/sourcedb"
                                ]
                            }
                        ]
                    }
                },
                "writer": {
                    "name": "postgresqlwriter",
                    "parameter": {
                        "username": "postgres",
                        "password": "111111",
                        "column": [ "id", "name","content","createdate"],
                        "preSql": [
                            "delete from t_user_info"
                        ],
                        "connection": [
                            {
                                "jdbcUrl": "jdbc:postgresql://127.0.0.1:5432/postgres",
                                "table": [
                                    "t_user_info"
                                ]
                            }
                        ]
                    }
                }
            }
        ]
    }
}

三:执行

datax> python .\bin\datax.py .\task\mysql-2-pg.json
2020-04-20 23:05:04.919 [job-0] INFO  StandAloneJobContainerCommunicator - Total 3 records, 125 bytes | Speed 12B/s, 0 records/s | Error 0 records, 0 bytes |  All Task WaitWriterTime 0.000s |  All Task WaitReaderTime 0.000s | Percentage 100.00%
2020-04-20 23:05:04.926 [job-0] INFO  JobContainer -
任务启动时刻                    : 2020-04-20 23:04:45
任务结束时刻                    : 2020-04-20 23:05:04
任务总计耗时                    :                 19s
任务平均流量                    :               12B/s
记录写入速度                    :              0rec/s
读出记录总数                    :                   3
读写失败总数                    :                   0

备注:
如果出现DataX乱码
命令执行CHCP 65001回车
再执行 python .\bin\datax.py .\task\mysql-2-pg.json

四:验证

PG

select * from "public"."t_user_info"
1	linux重启mysql的命令	linux重启mysql的命令	2020-04-19 21:18:58
2	jQuery给input绑定回车事件	jQuery给input绑定回车事件	2020-04-20 21:19:14
3	Web开发Session超时设置	Web开发Session超时设置	2020-04-21 21:19:19

完成

 类似资料: