企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 概述 数据传输服务DTS(Data Transmission Service)是阿里云提供的实时数据流服务,支持关系型数据库(RDBMS)、非关系型的数据库(NoSQL)、数据多维分析(OLAP)等数据源间的数据交互,集数据同步、迁移、订阅、集成、加工于一体,助您构建安全、可扩展、高可用的数据架构。其底层基础设施采用阿里双11异地多活架构,为数千下游应用提供实时数据流,已在线上稳定运行7年之久。 本章节通过RDS MySQL中的生产数据实时同步到阿里云Elasticsearch中进行搜索查询,通过数据传输服务DTS(Data Transmission Service)进行数据同步操作。 ## 前提条件 * 已创建源RDS MySQL实例,详情请参见快速创建RDS MySQL实例。 * 已创建目标Elasticsearch实例,详情请参见创建阿里云Elasticsearch实例。 * 目标Elasticsearch实例的存储空间须大于源RDS MySQL实例占用的存储空间。 ## 概念对应关系 ![](https://img.kancloud.cn/b6/e4/b6e41b5ef664d47d3e46095e6bd74d0e_714x281.png) ## 创建DTS实例 创建同步任务 ![](https://img.kancloud.cn/fc/40/fc40c9f3f596269014edc6f7fe7935e2_1891x505.png) 选择需要同步的RDS MySQL表 ![](https://img.kancloud.cn/28/93/28931f2cd73ae46498283d1b9734d7da_1526x209.png) 任务同步进展 ![](https://img.kancloud.cn/ec/53/ec534232442ee9c5f469934b41209694_1059x707.png) 第一次同步是全量同步 ## 验证数据同步结果 默认情况下,您还需要同时选中库表结构同步和全量同步。预检查完成后,DTS会将源实例中待同步对象的全量数据在目标集群中初始化,作为后续增量同步数据的基线数据。 待全量同步完成,增量同步进行中时,您即可在Elasticsearch中查看同步成功的数据。 ![](https://img.kancloud.cn/d7/10/d7105c77ac827fd6ca9de44cc1a6115d_1300x121.png) 数据同步完成后,我们通过Kibana访问实例进行数据验证 ![](https://img.kancloud.cn/4b/bb/4bbb919f4fa9a565d365b62a8976f3e7_800x427.png) 1. 在Kibana区域,单击公网入口 2. 在登录页面输入账号和密码,单击登录 3. 单击Kibana页面左上角的image图标,选择Management > 开发工具(Dev Tools)。 4. 在Console页签下,执行如下命令访问Elasticsearch实例 ### 全局查询 ``` GET /mall_category/_search ``` 搜索成功后,返回结果如下,以下结果表示全量同步到Elasticsearch成功 ``` { "took" : 4, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1041, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2513", "_score" : 1.0, "_source" : { "id" : 2513, "name" : "有商品无法删2", "parent_id" : 2512, "is_show" : 1, "sort_order" : 0, "create_time" : 1679537485, "update_time" : 1679537485, "delete_time" : 1679542632 } }, .... { "_index" : "mall_category", "_type" : "mall_category", "_id" : "1310", "_score" : 1.0, "_source" : { "id" : 1310, "name" : "发饰", "parent_id" : 1258, "is_show" : 1, "sort_order" : 0, "create_time" : 0, "update_time" : 0, "delete_time" : 0 } } ] } } ``` ### 条件查询 ``` GET /mall_category/_search { "query": { "match": { "name": "有商品无法删2" } } } ``` 搜索成功后,返回结果如下 ``` { "took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 59, "relation" : "eq" }, "max_score" : 21.80254, "hits" : [ { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2513", "_score" : 21.80254, "_source" : { "id" : 2513, "name" : "有商品无法删2", "parent_id" : 2512, "is_show" : 1, "sort_order" : 0, "create_time" : 1679537485, "update_time" : 1679537485, "delete_time" : 1679542632 } }, { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2512", "_score" : 16.643557, "_source" : { "id" : 2512, "name" : "有商品无法删1", "parent_id" : 2511, "is_show" : 1, "image_url" : "", "sort_order" : 0, "create_time" : 1679537458, "update_time" : 1679537458, "delete_time" : 1679554114 } }, { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2511", "_score" : 15.356989, "_source" : { "id" : 2511, "name" : "测试有商品无法删", "parent_id" : 0, "is_show" : 1, "image_url" : "", "sort_order" : 0, "create_time" : 1679537448, "update_time" : 1679537448, "delete_time" : 1679554191 } }, { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2508", "_score" : 4.6345234, "_source" : { "id" : 2508, "name" : "无糖 代糖", "parent_id" : 0, "is_show" : 1, "sort_order" : 0, "create_time" : 1679534815, "update_time" : 1679534815, "delete_time" : 1679642540 } }, { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2045", "_score" : 4.5389233, "_source" : { "id" : 2045, "name" : "无痕塑身", "parent_id" : 1350, "is_show" : 1, "sort_order" : 0, "create_time" : 0, "update_time" : 0, "delete_time" : 0 } }, { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2240", "_score" : 4.3105736, "_source" : { "id" : 2240, "name" : "无人机", "parent_id" : 1363, "is_show" : 1, "sort_order" : 0, "create_time" : 0, "update_time" : 0, "delete_time" : 0 } } ] } } ``` ### 精准查询 ``` GET /mall_category/_search { "query": { "match_phrase": { "name": "有商品无法删2" } } } ``` 搜索成功后,返回结果如下 ``` { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 21.80254, "hits" : [ { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2513", "_score" : 21.80254, "_source" : { "id" : 2513, "name" : "有商品无法删2", "parent_id" : 2512, "is_show" : 1, "sort_order" : 0, "create_time" : 1679537485, "update_time" : 1679537485, "delete_time" : 1679542632 } } ] } } ``` ## 增量同步 在MySQL中插入一条数据,在Elasticsearch中查看增量数据同步结果。例如通过以下SQL语句插入一条数据 ```sql INSERT INTO `mall_category` ( `name`, `parent_id`, `is_show`, `image_url`, `sort_order`, `create_time`, `update_time`, `delete_time` ) VALUES ( '开源技术小栈', 1361, 1, 'https://img.tinywan.com/shop/img/2024-12/3a5cbd823.png', 0, 0, 0, 0 ); ``` 这里通过精准查询方式查询 ``` GET /mall_category/_search { "query": { "match_phrase": { "name": "开源技术小栈" } } } ``` 在Elasticsearch中查看结果,预期结果如下 ``` { "took" : 1, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 21.59761, "hits" : [ { "_index" : "mall_category", "_type" : "mall_category", "_id" : "2537", "_score" : 21.59761, "_source" : { "update_time" : 0, "delete_time" : 0, "create_time" : 0, "image_url" : "https://img.tinywan.com/shop/img/2024-12/3a5cbd823.png", "parent_id" : 1361, "name" : "开源技术小栈", "id" : 2537, "sort_order" : 0, "is_show" : 1 } } ] } } ``` ![](https://img.kancloud.cn/33/02/3302016c844b30c19eaa648d7f8afd19_1282x704.png)