Python:使用 mysqlsmom 模块实时同步MySQL数据到ElasticSearch

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
Elasticsearch Serverless通用抵扣包,测试体验金 200元
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: Python:使用 mysqlsmom 模块实时同步MySQL数据到ElasticSearch

mysqlsmom 文档: https://mysqlsmom.readthedocs.io/en/latest/hello.html

github: https://github.com/m358807551/mysqlsmom


环境要求:

1、python2.7
2、redis
3、Mysql 配置 binlog-format=row

安装

 pip install mysqlsmom

全量同步

# 创建全量同步配置文件


$ mom new test_mom/init_config.py -t init --force

# 编辑配置文件
$ vim ./test_mom/init_config.py # 按注释提示修改配置

# 开始同步
$ mom run -c ./test_mom/init_config.py

增量同步

配置三个文件

test_mom
├── binlog_config.py # 配置文件
├── my_filters.py # 过滤器 配置于 watched
└── my_handlers.py # 处理器 配置于 pipeline

新建配置

mom new test_mom/binlog_config.py -t binlog --force

1、binlog_config.py

# coding=utf-8

STREAM = "BINLOG" # "BINLOG" or "INIT"
SERVER_ID = 99
SLAVE_UUID = name

# 一次同步 BULK_SIZE 条数据到elasticsearch,不设置该配置项默认为1
BULK_SIZE = 1

BINLOG_CONNECTION = {
'host': '127.0.0.1',
'port': 3306,
'user': 'root',
'passwd': '123456'
}

# redis存储上次同步位置等信息
REDIS = {
"host": "127.0.0.1",
"port": 6379,
"db": 0,
# "password": "password", # 不需要密码则注释或删掉该行
}

NODES = [{"host": "127.0.0.1", "port": 9200}]

TASKS = [
{
"stream": {
"database": "demo",
"table": "student"
},
"jobs": [{
"actions": ["insert", "update"],
"watched": {
"filter_display": {}
},
"pipeline": [
{"only_fields": {"fields": ["id", "name", "age"]}},
{"change_name": {"key": "name", "prefix": "hot-"}},
{"set_id": {"field": "id"}}
],
"dest": {
"es": {
"action": "upsert",
"index": "demo",
"type": "student",
"nodes": NODES
}
}
},
{
"actions": ["delete"],
"pipeline": [
# {"only_fields": {"fields": ["id", "name", "age"]}},
{"set_id": {"field": "id"}}
],
"dest": {
"es": {
"action": "delete",
"index": "demo",
"type": "student",
"nodes": NODES
}
}
}
]
}
]

CUSTOM_ROW_HANDLERS = "./my_handlers.py"
CUSTOM_ROW_FILTERS = "./my_filters.py"

自定义处理器 my_handlers.py

# -- coding: utf-8 --


import copy

def change_name(row, key, prefix):
new_row = copy.deepcopy(row)
new_row[key] = "{}{}".format(prefix, row[key])
# 返回数据字典,下一工序继续处理
return new_row

自定义过滤器 my_filters.py

# -- coding: utf-8 --

def filter_display(event):
# 返回True 或 False,使用或丢弃
return event"values" == 1

启动

mom run -c test_mom/binlog_config.py 


            </div>
相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
JSON 负载均衡 JavaScript
带你读《Elastic Stack 实战手册》之65:——3.5.19.1.Elasticsearch语言开发(Python)(上)
带你读《Elastic Stack 实战手册》之65:——3.5.19.1.Elasticsearch语言开发(Python)(上)
461 1
|
索引 Python
python操作Elasticsearch7.17.0
python操作Elasticsearch7.17.0 作者主页:https://www.couragesteak.com/
|
索引 Python
【Python】已解决:elasticsearch.exceptions.RequestError: TransportError(400, ‘search_phase_execution_exc
【Python】已解决:elasticsearch.exceptions.RequestError: TransportError(400, ‘search_phase_execution_exc
772 0
|
Python
python执行elasticsearch异常【已解决】
python执行elasticsearch异常【已解决】
273 2
|
API 数据安全/隐私保护 开发者
用 Python 优雅地玩转 Elasticsearch:实用技巧与最佳实践
用 Python 优雅地玩转 Elasticsearch:实用技巧与最佳实践
|
索引 Python
python使用elasticsearch的详细过程
python使用elasticsearch的详细过程
924 1
|
API 索引 Python
Python更新Elasticsearch数据方法大全
Python更新Elasticsearch数据方法大全
|
存储 人工智能 运维
带你读《Elastic Stack 实战手册》之65:——3.5.19.1.Elasticsearch语言开发(Python)(下)
带你读《Elastic Stack 实战手册》之65:——3.5.19.1.Elasticsearch语言开发(Python)(下)
231 0
|
存储 Ubuntu Linux
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(上)
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(上)
279 0
|
存储 API 索引
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(中)
带你读《Elastic Stack 实战手册》之78:——4.2.4.Elasticsearch和Python构建面部识别系统(中)
217 0