SkillAgentSearch skills...

Dbswitch

异构数据库数据结构转换与离线同步工具,fork: https://gitee.com/inrgihc/dbswitch.git

Install / Use

/learn @sqlking22/Dbswitch
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

该项目来源于:https://gitee.com/inrgihc/dbswitch.git

异构数据库数据与结构同步工具

一、工具介绍

1、功能描述

一句话,dbswitch工具提供源端数据库向目的端数据库的批量迁移同步功能,支持数据的全量和增量方式同步。包括:

  • 结构迁移

支持字段类型、主键信息、建表语句等的转换,并生成建表SQL语句。

支持基于正则表达式转换的表名与字段名映射转换。

  • 数据同步

基于JDBC的分批次读取源端数据库数据,并基于insert/copy方式将数据分批次写入目的数据库。

支持有主键表的 增量变更同步 (变化数据计算Change Data Calculate)功能(千万级以上数据量的性能尚需在生产环境验证)

2、功能设计

function

3、详细功能

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为Greenplum/PostgreSQL/HighGo的迁移(支持绝大多数常规类型字段)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为Oracle的迁移(支持绝大多数常规类型字段)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为SQLServer/Sybase的迁移(字段类型兼容测试中...)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为MySQL/MariaDB/GBase8a的迁移(字段类型兼容测试中...)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为DB2的迁移(字段类型兼容测试中...)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为Kingbase8的迁移(支持绝大多数常规类型字段...)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为DM的迁移(支持绝大多数常规类型字段)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为OSCAR的迁移(支持绝大多数常规类型字段)

  • 源端oracle/SqlServer/Sybase/MySQL/MariaDB/PostgreSQL/DB2/DM/OSCAR/Kingbase8/GBase8a/HighGo/Hive/SQLite向目的端为SQLite的迁移(支持部分常规类型字段...)

** 注:** 目前Hive只支持Hive version 3.x的账号密码方式认证。

4、结构设计

  • 模块结构设计

    structure

  • 模块结构功能

└── dbswitch
    ├── dbswitch-common    // dbswitch通用定义模块
    ├── dbswitch-pgwriter  // PostgreSQL的二进制写入封装模块
    ├── dbswitch-dbwriter  // 数据库的通用批量Insert封装模块
    ├── dbswitch-core      // 数据库元数据抽取与建表结构语句转换模块
    ├── dbswitch-dbcommon  // 数据库操作通用封装模块
    ├── dbswitch-dbchange  // 基于全量比对计算变更(变化量)数据模块
    ├── dbswitch-dbsynch   // 将dbchange模块计算的变更数据同步入库模块
    ├── dbswitch-data      // 工具入口模块,读取配置文件中的参数执行异构迁移同步
    ├── dbswitch-admin     // 在以上模块的基础上引入Quartz的调度服务与接口
    ├── dbswitch-admin-ui  // 基于Vue2的前段WEB交互页面
    ├── package-tool       // 基于maven-assembly-plugin插件的项目打包模块

二、编译打包

本工具纯Java语言开发,代码中的依赖全部来自于开源项目。

1、编译打包

  • 环境要求:

    JDK:>=1.8 (建议用JDK 1.8)

    maven:>=3.6

Maven 仓库默认在国外, 国内使用难免很慢,可以更换为阿里云的仓库。 参考教程: 配置阿里云的仓库教程

  • 编译命令:

(1) windows下:

 双击build.cmd脚本文件即可编译打包

(2) Linux下:

git clone https://gitee.com/inrgihc/dbswitch.git
cd dbswitch/
sh ./build.sh

(3) Docker下:

git clone https://gitee.com/inrgihc/dbswitch.git
cd dbswitch/
sh ./docker-maven-build.sh

特别注意: 在Java9及以上版本默认情况下不允许应用程序查看来自JDK的所有类,但在dbswitch中利用反射计算对象的字节大小,所以需要在JVM启动时需要增加如下参数:

--add-opens java.base/jdk.internal.loader=ALL-UNNAMED --add-opens jdk.zipfs/jdk.nio.zipfs=ALL-UNNAMED

2、安装部署

(1) 当编译打包命令执行完成后,会在dbswitch/target/目录下生成dbswitch-relase-x.x.x.tar.gz的打包文件,将文件拷贝到已安装JRE的部署机器上解压即可。

(2) 基于docker-compose提供linux联网环境下的一键安装,安装命令见 发行版链接地址

文档详见: build-docker/install/README.md

三、工具使用

dbswitch工具提供基于conf/config.yml配置的dbswitch-data模块启动方式和基于conf/application.yml的dbswitch-admin模块的WEB端使用方式;

1、基于conf/config.yml配置的dbswitch-data模块启动命令操作方式

(1)、配置文件

配置文件信息请见部署包中的:conf/config.yml(注:也同时支持使用conf/config.properties配置文件名的properties格式),示例配置如下:

dbswitch:
  source:
    # source database connection information
    ## support multiple source database connection
    - url: jdbc:oracle:thin:@172.17.2.10:1521:ORCL
      driver-class-name: 'oracle.jdbc.driver.OracleDriver'
      driver-path: D:/Workspace/dbswitch/driver/oracle/oracle-12c
      username: 'system'
      password: '123456'
      # source database configuration parameters
      ## fetch size for query source database
      fetch-size: 10000
      ## schema name for query source schemas, separate by ','
      source-schema: 'TANG'
      ## table type which include or exclude,option: TABLE,VIEW
      table-type: 'TABLE'
      ## table name include from table lists, separate by ','
      source-includes: ''
      ## table name exclude from table lists, separate by ','
      source-excludes: ''
      ## table name convert mapper by regular expression
      regex-table-mapper:
        - from-pattern: '^'
          to-value: 'T_'
      ## columns name convert mapper by regular expression like regex-table-mapper
      regex-column-mapper:

  target:
    # target database connection information
    ## Best support for Oracle/PostgreSQL/Greenplum/DM(But not is Hive) etc.
    url: jdbc:postgresql://172.17.2.10:5432/test
    driver-class-name: org.postgresql.Driver
    driver-path: D:/Workspace/dbswitch/driver/postgresql/postgresql-11.4
    username: test
    password: 123456
    # target database configuration parameters
    ## schema name for create/insert table data
    target-schema: public
    ## whether drop-create table when target table exist
    target-drop: true
    ## whether create table support auto increment for primary key field
    create-table-auto-increment: false
    ## whether use insert engine to write data for target database
    ## Only useful for PostgreSQL/Greenplum database
    writer-engine-insert: false
    ## whether use change data synchronize to target database table
    change-data-sync: true

| 配置参数 | 配置说明 | 示例 | 备注 | | :------| :------ | :------ | :------ | | dbswitch.source[i].url | 来源端JDBC连接的URL | jdbc:oracle:thin:@10.17.1.158:1521:ORCL | 可为:oracle/mysql/mariadb/sqlserver/postgresql/db2/dm/kingbase8/highgo | | dbswitch.source[i].driver-class-name | 来源端数据库的驱动类名称 | oracle.jdbc.driver.OracleDriver | 对应数据库的驱动类 | | dbswitch.source[i].driver-path | 来源端数据库的驱动JAR所在目录 | D:/Workspace/dbswitch/driver/oracle/oracle-12c | 对应数据库的驱动JAR所在目录 | | dbswitch.source[i].username | 来源端连接帐号名 | test | 无 | | dbswitch.source[i].password | 来源端连接帐号密码 | 123456 | 无 | | dbswitch.source[i].fetch-size | 来源端数据库查询时的fetch_size设置 | 10000 | 需要大于100有效 | | dbswitch.source[i].source-schema | 来源端的schema名称 | dbo,test | 多个之间用英文逗号分隔 | | dbswitch.source[i].table-type | 来源端表的类型 | TABLE | 可选值为:TABLE、VIEW ,分别代表物理表和试图表 | | dbswitch.source[i].source-includes | 来源端schema下的表中需要包含的表名称 | users1,orgs1 | 支持多个表(多个之间用英文逗号分隔);支持支持正则表达式(不能含有逗号) | | dbswitch.source[i].source-excludes | 来源端schema下的表中需要过滤的表名称 | users,orgs | 不包含的表名称,多个之间用英文逗号分隔 | | dbswitch.source[i].regex-table-mapper | 基于正则表达式的表名称映射关系 | [{"from-pattern": "^","to-value": "T_"}] | 为list类型,元素存在顺序关系 | | dbswitch.source[i].regex-column-mapper | 基于正则表达式的字段名映射关系 | [{"from-pattern": "$","to-value": "_x"}] | 为list类型,元素存在顺序关系 | | dbswitch.target.url | 目的端JDBC连接的URL | jdbc:postgresql://10.17.1.90:5432/study | 可为:oracle/sqlserver/postgresql/greenplum,mysql/mariadb/db2/dm/kingbase8/highgo也支持,但字段类型兼容性问题比较多 | | dbswitch.target.driver-class-name |目的端数据库的驱动类名称 | org.postgresql.Driver | 对应数据库的驱动类 | | dbswitch.source[i].driver-path | 目的端数据库的驱动JAR所在目录 | D:/Workspace/dbswitch/driver/postgresql/postgresql-11.4 | 对应数据库的驱动JAR所在目录 | | dbswitch.target.username | 目的端连接帐号名 | test | 无 | | dbswitch.target.password | 目的端连接帐号密码 | 123456 | 无 | | dbswitch.target.target-schema | 目的端的schema名称 | public | 目的端的schema名称只能有且只有一个 | | dbswitch.target.target-drop | 是否执行先drop表然后create表命令,当target.datasource-target.drop=true时有效 | true | 可选值为:true、false | | dbswitch.target.create-table-auto-increment | 是否执启用支持create表时主键自增 | true | 可选值为:true、false | | dbswitch.target.writer-engine-insert | 是否使用insert写入数据 | false | 可选值为:true为insert写入、false为copy写入,只针对目的端数据库为PostgreSQL/Greenplum的有效 | | dbswitch.target.change-data-sync | 是否启用增量变更同步,dbswitch.target.target-drop为false时且表有主键情况下有效,千万级以上数据量建议设为false | false | 可选值为:true、false |

注意:

  • (1)支持源端为多个数据源类型,如果dbswitch.source[i]为数组类型,i为编号,从0开始的整数;

  • (2)如果dbswitch.source[i].source-includes不为空,则按照包含表的方式来执行;

  • (3)如果dbswitch.source[i].source-includes为空,则按照dbswitch.source[i].source-excludes排除表的方式来执行。

  • (4)如果dbswitch.target.target-drop=falsedbswitch.target.change-data-synch=true;时会对有主键表启用增量变更方式同步

  • (5)对于regex-table-mapperregex-column-mappe,为基于正则表达式替换的表名映射和字段名映射,均可以为空(代表原名映射,即源的表t_a映射到目的端也为t_a)

提示:如果要将源端所有表名(或者字段名)添加前缀,可以配置"from-pattern": "^","to-value": "T_";

  • (6)支持的数据库产品及其JDBC驱动连接示例如下:

MySQL/MariaDB数据库

jdbc连接地址:jdbc:mysql://172.17.2.10:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&tinyInt1isBit=false&rewriteBatchedStatements=true&useCompression=true
jdbc驱动名称: com.mysql.jdbc.Driver

与:

jdbc连接地址:jdbc:mariadb://172.17.2.10:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&tinyInt1isBit=false&rewriteBatchedStatements=true&useCompression=true
jdbc驱动名称: org.mariadb.jdbc.Driver

Oracle数据库

jdbc连接地址:jdbc:oracle:thin:@172.17.2.10:1521:ORCL  或   jdbc:oracle:thin:@//172.17.2.10:1521/ORCL
jdbc驱动名称:oracle.jdbc.driver.OracleDriver

SQL Server(>=2005)数据库

jdbc连接地址:jdbc:sqlserver://172.17.2.10:1433;DatabaseName=test
jdbc驱动名称:com.microsoft.sqlserver.jdbc.SQLServerDriver

Sybase数据库

jdbc连接地址:jdbc:sybase:Tds:172.17.2.10:5000/test?charset=cp936
jdbc驱动名称:com.sybase.jdbc4.jdbc.SybDriver

JDBC连接Sybase数据库使用中文时只能使用CP936这个字符集

PostgreSQL/Greenplum数据库

jdbc连接地址:jdbc:postgresql://172.17.2.10:5432/test
jdbc驱动名称:org.postgresql.Driver

DB2数据库

jdbc连接地址:jdbc:db2://172.17.2.10:50000/testdb:driverType=4;fullyMaterializeLobData=true;fullyMaterializeInputStreams=true;progressiveStreaming=2;progresssiveLocators=2;
jdbc驱动名称:com.ibm.db2.jcc.DB2Drive
View on GitHub
GitHub Stars10
CategoryDevelopment
Updated2mo ago
Forks6

Languages

Java

Security Score

75/100

Audited on Jan 30, 2026

No findings