一個數(shù)據(jù)開發(fā)人員使用的輔助工具
介紹
需求背景:
有很多業(yè)務(wù)系統(tǒng),他們的數(shù)據(jù)庫是相互獨立的,俗稱數(shù)據(jù)孤島,為了做數(shù)據(jù)統(tǒng)計分析,就需要把這些數(shù)據(jù)歸集在一個數(shù)據(jù)庫中,比如數(shù)據(jù)倉庫,然后多表關(guān)聯(lián)查詢,方便開發(fā)數(shù)據(jù)應(yīng)用。希望能有這樣的工具,指定兩個數(shù)據(jù)庫和表名,就可以將表從源數(shù)據(jù)庫拷貝到目標(biāo)數(shù)據(jù)庫中。具體需求如下:
- 能自動同步表結(jié)構(gòu),如:源表加字段,目標(biāo)表自動加字段。
- 支持增量或全量復(fù)制數(shù)據(jù),比如按日期進(jìn)行復(fù)制數(shù)據(jù)。
- 支持指定字段同步,只同步關(guān)心的那些字段。
- 支持主流的關(guān)系型數(shù)據(jù)庫: mysql、db2、postgresql、oracle、sqlserver
- 源表和目標(biāo)表表名可以不同,字段名也可以不同(已存在目標(biāo)表的情況下)
因為自己要用,我就自己寫了一個,順便熟悉下 java 開發(fā)(之前一直用 Python,不得不說,Java 真浪費時間),本程序的最大用處就是構(gòu)建集市或數(shù)倉所需要的基礎(chǔ)層數(shù)據(jù)源,歡迎感興趣的朋友一起加入。
程序的使用方法
Docker 方式:
這里用到三個容器:
- app 也就是主程序本身,app 容器使用的程序文件就是 release 目錄下的文件,已經(jīng)做了綁定。
- mysql 測試用的,作為源數(shù)據(jù)庫,已提前放好了有 7000 條測試數(shù)據(jù)的表 somenzz_users。
- postgres 測試用的,作為目標(biāo)數(shù)據(jù)庫,沒有數(shù)據(jù)。
先部署,執(zhí)行 docker-compose up -d 就會自動完成應(yīng)用和數(shù)據(jù)庫的部署:
- $ git clone https://github.com/somenzz/database-sync.git
- $ cd database-sync
- $ docker-compose up -d
- Creating database-sync_postgres_1 ... done
- Creating database-sync_app_1 ... done
- Creating database-sync_mysql_1 ... done
這樣三個容器就啟動了,使用 docker ps -a |grep database-sync 可以查看到三個正在運行的容器:
現(xiàn)在直接使用 docker exec -i database-sync_app_1 java -jar database-sync-1.3.jar 來執(zhí)行程序:
mysql 容器已有測試數(shù)據(jù),release/config/config.json 已經(jīng)配置好了數(shù)據(jù)庫的連接,因此可以直接試用,以下演示的是從 mysql 復(fù)制表和數(shù)據(jù)到 postgres:
1. 全量復(fù)制,自動建表:
- docker exec -i database-sync_app_1 java -jar database-sync-1.3.jar mysql_test testdb somenzz_users postgres_test public users --sync-ddl
如果你不想每次都敲 docker exec -i database-sync_app_1 ,可以進(jìn)入容器內(nèi)部執(zhí)行:
- (py38env) ➜ database-sync git:(master) ✗ docker exec -it database-sync_app_1 /bin/bash
- root@063b1dc76fe1:/app# ls
- config database-sync-1.3.jar lib logs
- root@063b1dc76fe1:/app# java -jar database-sync-1.3.jar mysql_test testdb somenzz_users postgres_test public users -sd
2. 增量復(fù)制:
- root@063b1dc76fe1:/app# java -jar database-sync-1.3.jar mysql_test testdb somenzz_users postgres_test public zz_users "create_at >= '2018-01-09'"
3. 指定字段:
- root@063b1dc76fe1:/app# java -jar database-sync-1.3.jar mysql_test testdb somenzz_users postgres_test public zz_users -ff="user_id,name,age" -tf="user_id,name,age" "create_at >= '2018-01-09'"
普通方式
程序運行前確保已安裝 java 1.8 或后續(xù)版本,已經(jīng)安裝 maven,然后 clone 源碼,打包:
- git clone https://gitee.com/somenzz/database-sync.git
- cd database-sync
- mvn package
此時你會看到 target 目錄,將 target 下的 lib 目錄 和 database-sync-1.3.jar 復(fù)制出來,放在同一目錄下,然后再創(chuàng)建一個 config 目錄,在 config 下新建一個 config.json 文件寫入配置信息,然后將這個目錄壓縮,就可以傳到服務(wù)器運行了,請注意先充分測試,jdk 要求 1.8+
- [aaron@hdp002 /home/aaron/App/Java/database-sync]$ ls -ltr
- total 48
- drwxr-xr-x 2 aaron aaron 4096 Apr 23 2020 lib
- -rwxrw-r-- 1 aaron aaron 157 Jun 23 2020 run.sh
- drwxrwxr-x 2 aaron aaron 4096 Jul 3 2020 logs
- -rw-rw-r-- 1 aaron aaron 24773 Mar 16 2021 database-sync-1.3.jar
- drwxr-xr-x 7 aaron aaron 4096 Aug 3 2020 jdk1.8.0_231
- drwxrwxr-x 2 aaron aaron 4096 Feb 19 17:07 config
你也可以直接下載我打包好的使用。
程序名稱叫 database-sync,運行方式是這樣的:
- (py38env) ➜ target git:(master) ✗ java -jar database-sync-1.3.jar -h
- Usage:
- java -jar database-sync-1.0.jar [options] {fromDB} {fromSchema} {fromTable} {toDB} {toSchema} {toTable} [whereClause]
- options:
- -v or --version :print version then exit
- -h or --help :print help info then exit
- -sd or --sync-ddl :auto synchronize table structure
- -ff=col1,col2 or --from-fields=col1,col2 :specify from fields
- -tf=col3,col4 or --to-fields=col3,col4 :specify to fields
- --no-feature or -nf :will not use database's feature
幫助說明:
[] 中括號里的內(nèi)容表示選填,例如 [options] 表示 options 下的參數(shù)不是必須的。
1、其中 options 參數(shù)解釋如下:
- --sync-ddl 或者 -sd : 加入該參數(shù)會自動同步表結(jié)構(gòu)。
- --from_fields=col1,col2 或者 -ff=col1,col2 : 指定原表的字段序列,注意 = 前后不能有空格。
- --to_fields=col3,col4 或者 -tf=col3,col4 : 指定目標(biāo)表的字段序列,注意 = 前后不能有空格。
2、whereClause 表示 where 條件,用于增量更新,程序再插入數(shù)據(jù)前先按照 where 條件進(jìn)行清理數(shù)據(jù),然后按照 where 條件從原表進(jìn)行讀取數(shù)據(jù)。whereClause 最好使用雙引號包起來,表示一個完整的參數(shù)。如:"jyrq='2020-12-31'"
{} 大括號里的內(nèi)容表示必填。
fromDb 是指配置在 config.json 的數(shù)據(jù)庫信息的鍵,假如有以下配置文件:
- {
- "postgres":{
- "type":"postgres",
- "driver":"org.postgresql.Driver",
- "url":"jdbc:postgresql://localhost:5432/apidb",
- "user": "postgres",
- "password":"aaron",
- "encoding": "utf-8"
- },
- "aarondb":{
- "type":"mysql",
- "driver":"com.mysql.cj.jdbc.Driver",
- "url":"jdbc:mysql://localhost:3306/aarondb?useSSL=false&characterEncoding=utf8&serverTimezone=UTC",
- "user": "aaron",
- "password":"aaron"
- }
- }
fromDb、toDb 可以是 aarondb 或者 postgres。
- fromSchema 讀取數(shù)據(jù)的表的模式名,可以填寫 "".
- fromTable 讀取數(shù)據(jù)的表明,必須提供。
- toSchema 寫入數(shù)據(jù)表的模式名,可以填寫 "",可以和 fromSchema 不同.
- toTable 寫入數(shù)據(jù)表的表名,必須提供,當(dāng)寫入表不存在時,自動按讀取表的表結(jié)構(gòu)創(chuàng)建,可以和 fromTable 不同。
全量、增量、指定字段的使用樣例請參考 Docker 方式。
配置文件說明
配置文件位于 config/config.json,如下所示:
- {
- "sjwb":{
- "type":"db2",
- "driver":"com.ibm.db2.jcc.DB2Driver",
- "url":"jdbc:db2://192.168.1.*:50000/wbsj",
- "user": "****",
- "password":"****",
- "tbspace_ddl": "/*這里可以放置指定表空間的語句*/",
- "encoding":"utf-8"
- },
- "dw_test":{
- "type":"db2",
- "driver":"com.ibm.db2.jcc.DB2Driver",
- "url":"jdbc:db2://192.168.169.*:60990/dwdb",
- "user": "****",
- "password":"****",
- "encoding":"gbk"
- },
- "postgres":{
- "type":"postgres",
- "driver":"org.postgresql.Driver",
- "url":"jdbc:postgresql://10.99.**.**:5432/apidb",
- "user": "****",
- "password":"****",
- "tbspace_ddl": "WITH (compression=no, orientation=orc, version=0.12)\ntablespace hdfs\n",
- "encoding":"utf-8"
- },
- "aarondb":{
- "type":"mysql",
- "driver":"com.mysql.cj.jdbc.Driver",
- "url":"jdbc:mysql://localhost:3306/aarondb?useSSL=false&characterEncoding=utf8&serverTimezone=UTC",
- "user": "****",
- "password":"****",
- "encoding":"utf-8"
- },
- "buffer-rows": 100000
- }
配置文件說明:
type 表示數(shù)據(jù)庫類型,均為小寫:
- mysql
- postgres
- db2
- oracle
- sqlserver
tbspace_ddl 表示自動建表時指定的表空間,該選項不是必需的,可以刪除。
buffer-rows 表示讀取多少行時一塊寫入目標(biāo)數(shù)據(jù)庫,根據(jù)服務(wù)器內(nèi)存大小自己做調(diào)整,100000 行提交一次滿足大多數(shù)情況了。
encoding 用于表結(jié)構(gòu)同步時確定字段長度,比如說源庫的字段是 gbk varchar(10),目標(biāo)庫是 utf-8,那么就應(yīng)該為 varchar(15),這樣字段有中文就不會出現(xiàn)截斷或插入失敗問題,程序這里 2 倍,也就是 varchar(20) ,這樣字段長度不會出現(xiàn)小數(shù)位。
最后的話
提高數(shù)據(jù)庫間表的復(fù)制效率,如果不需要對源表字段進(jìn)行轉(zhuǎn)換,就丟掉低效的 datastage 和 kettle 吧。