{"id":20578764,"url":"https://github.com/cclient/scalamysqlsync","last_synced_at":"2026-04-19T12:35:31.604Z","repository":{"id":93977611,"uuid":"96341065","full_name":"cclient/ScalaMysqlSync","owner":"cclient","description":"因canal不可用，sclala+slick+akka事件驱动实现mysql异构表电梯法同步，项目只是用来练手，功能可由logstash实现","archived":false,"fork":false,"pushed_at":"2018-05-07T12:12:52.000Z","size":21,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-16T22:19:58.696Z","etag":null,"topics":["akka","akka-actors","mysql","scala","slick"],"latest_commit_sha":null,"homepage":"https://www.cnblogs.com/zihunqingxin/p/8981931.html","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cclient.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-05T16:47:05.000Z","updated_at":"2023-06-30T03:19:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"c7c958ef-4540-40bd-a3a5-319ecb476951","html_url":"https://github.com/cclient/ScalaMysqlSync","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cclient%2FScalaMysqlSync","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cclient%2FScalaMysqlSync/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cclient%2FScalaMysqlSync/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cclient%2FScalaMysqlSync/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cclient","download_url":"https://codeload.github.com/cclient/ScalaMysqlSync/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242206001,"owners_count":20089251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["akka","akka-actors","mysql","scala","slick"],"created_at":"2024-11-16T06:14:30.216Z","updated_at":"2026-04-19T12:35:26.564Z","avatar_url":"https://github.com/cclient.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"## sql同步工具\n\n### 外采数据只提供数据，不提供查询服务(只能用基本的id查询)\n\n### 首先，项目以Mysql实现，slick支持其他数据库，代码生成脚本是完全以mysql的需求写的，如需其他sql服务同步需修改slick_generator.py\n\n### 此目标是多库多表异构同步（只字段类别相同，索引，主键，触发器等不同）\n\n### 最好的方案是用[canal](https://github.com/alibaba/canal)，但对方不提供mysql主从（技术上可行，但业务上对方不干）\n\n### 无奈只好用最原始的办法作同步,写个数据库表同步工具，没用scala写过sql操作，正好就拿来练手了\n\n## 基本概念\n\n#### 源库OD，源表OT（orginal主）\n\n#### 目的库DD，目的表DT(destination从)\n\n#### 偏移表LT（保存每张表的上次同步到的id，保存在目的库DD\n\n#### 数据同步依赖源表的自增id，同步数据时以id作电梯法实现\n\n## 流程\n\n### 启动每张表的同步服务\n\n  * 从last表先拿对应的上次的id,作为同步起始点\n  \n    * 失败 查询相应目的表max(id)值，以这个值,作为同步起始点\n    \n    * 成功 从OT offset同步起始点拿page_sise数据\n    \n      * 成功更新LT表中相应值\n\n#### 遇到异常或拿到最后，则休眠段时间后重试       \n       \n## 尚未完全自动化，需手动操作部分\n\n### 在DD建立last表（目前只实现了根据自增id同步，pub_time，spider_time冗余）\n\n```chef\nCREATE TABLE `last` (\n  `table_name` char(20) NOT NULL,\n  `id` bigint(20) DEFAULT NULL,\n  `pub_time` datetime DEFAULT NULL,\n  `spider_time` datetime DEFAULT NULL,\n  PRIMARY KEY (`table_name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n```\n* 1 创建目标表，表结构与源表相同\n\n* 2 生成表对应实体及actor代码\n\n* 3 src/main/tables/下创建scala类文件，前一步生成的代码写入\n\n* 4 src/main/task/Sync.scala 添加表同步任务\n\n##示例（代码生成脚本在tool下）\n\n#### 1 执行 show create table from pdf\n\n```sql\nCREATE TABLE `pdf` (\n  `id` int(11) NOT NULL,\n  `engine` char(10) NOT NULL,\n  `source_id` char(13) NOT NULL,\n  `title` varchar(200) NOT NULL,\n  `url` varchar(200) NOT NULL,\n  `desc` varchar(500) NOT NULL,\n  PRIMARY KEY (`id`),\n  UNIQUE KEY `source_id_UNIQUE` (`source_id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;\n```\n\n#### 2 添加至table_pdf.py\n\n#### 3 执行python slick_generator.py 生成代码\n\n#### 4 创建文件src/main/tables/Pdf.scala 前一步生成的代码写入\n\n#### 5 修改src/main/task/Sync.scala下\n\n```$xslt\n类:class Sync\n  方法:receive\n添加 \n val pdfactor=context.actorOf(Props[PdfActor],\"pdf\")\n pdfactor !BYOFFSET_TASK_START\n```\n\n## 部署启动服务\n\n* 1 编译打包(结果为SqlSync.jar):\n\n    sbt assembly\n  \n* 2 运行\n\n    java -jar SqlSync.jar\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcclient%2Fscalamysqlsync","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcclient%2Fscalamysqlsync","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcclient%2Fscalamysqlsync/lists"}