• 转载 —— 通过Monstache实时同步MongoDB数据至Elasticsearch


    通过Monstache实时同步MongoDB数据至Elasticsearch

    当您的业务数据存储在MongoDB中,并且需要进行语义分析和大图展示时,可借助阿里云Elasticsearch实现全文搜索、语义分析、可视化展示等。本文介绍如何通过Monstache将MongoDB数据实时同步至阿里云Elasticsearch,并对数据进行分析及展示。

    背景信息
    本文以解析及统计热门电影数据为例,提供的解决方案可以帮助您完成以下需求:
    通过Monstache快速同步及订阅全量或增量数据。
    将MongoDB数据实时同步至高版本Elasticsearch。
    解读Monstache常用配置参数,应用于更多的业务场景。

    方案优势

    Monstache基于MongoDB的oplog实现实时数据同步及订阅,支持MongoDB与高版本Elasticsearch之间的数据同步,同时支持MongoDB的变更流和聚合管道功能,并且拥有丰富的特性。
    Monstache不仅支持软删除和硬删除,还支持数据库删除和集合删除,能够确保Elasticsearch端实时与源端数据保持一致。

    操作流程
    步骤一:环境准备
    部署 MongoDB 主从复制模式,注意:一定要主从复制模式,因为同步需要使用 oplog 日志,这个日志是主从复制用的
    部署 Elasticsearch
    部署 kibana 用于前端页面与 es 交互,版本与 es 相同
    安装 Monstache,修改配置文件,进行同步

    步骤二:搭建Monstache环境

    安装Go,并配置环境变量。
    说明 由于Monstache数据同步依赖于Go语言,因此需要先在ECS中准备Go环境。

    下载Go安装包并解压。

    wget https://dl.google.com/go/go1.14.4.linux-amd64.tar.gz
    tar -C /usr/local -xzf go1.14.4.linux-amd64.tar.gz
    

    配置环境变量。

    使用vim /etc/profile命令打开环境变量配置文件,并将如下内容写入该文件中。其中GOPROXY用来指定阿里云Go模块代理。

    export GOROOT=/usr/local/go
    export GOPATH=/home/go/
    export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
    export GOPROXY=https://mirrors.aliyun.com/goproxy/
    

    应用环境变量配置。

    source /etc/profile
    

    安装Monstache。

    进入安装路径。

    cd /usr/local/
    

    从Git库中下载安装包。

    git clone https://github.com/rwynn/monstache.git
    

    说明 如果出现git: command not found的错误提示,需要先执行yum install -y git命令安装Git。

    进入monstache目录。

    cd monstache
    

    切换版本。
    本文以rel5版本为例。

    git checkout rel5
    

    安装Monstache。

    go install
    

    这一步可能出现问题:running gcc failed: exec: "gcc": executable file not found in $PATH

    解决:

    gcc -v
    

    没有就安装 gcc

    参考链接:https://blog.csdn.net/benben_2015/article/details/80565676

    查看Monstache版本。

    monstache -v
    

    执行成功后,预期结果如下。

    5.5.7
    

    步骤三:配置实时同步任务
    Monstache配置使用TOML格式,默认情况下,Monstache会使用默认端口连接本地主机上的Elasticsearch和MongoDB,并追踪MongoDB oplog。在Monstache运行期间,MongoDB的任何更改都会同步到Elasticsearch中。

    由于本文使用阿里云MongoDB和Elasticsearch,并且需要指定同步对象(mydb数据库中的hotmovies和col集合),因此要修改默认的Monstache配置文件。修改方式如下:

    进入Monstache安装目录,创建并编辑配置文件。

    cd /usr/local/monstache/
    vim config.toml
    

    参考以下示例,修改配置文件。
    简单的配置示例如下,详细配置请参见Monstache Usage。

    # connection settings
    
    # connect to MongoDB using the following URL
    mongo-url = 'mongodb://aaa:111@172.31.3.1:8010'
    # connect to the Elasticsearch REST API at the following node URLs
    elasticsearch-urls = ["http://140.179.9.1:8009"]
    
    # frequently required settings
    
    # if you need to seed an index from a collection and not just listen and sync changes events
    # you can copy entire collections or views from MongoDB to Elasticsearch
    # 可以直接全量复制过去
    direct-read-namespaces = ["mydb.hotmovies","mydb.col"]
    
    # if you want to use MongoDB change streams instead of legacy oplog tailing use change-stream-namespaces
    # change streams require at least MongoDB API 3.6+
    # if you have MongoDB 4+ you can listen for changes to an entire database or entire deployment
    # in this case you usually don't need regexes in your config to filter collections unless you target the deployment.
    # to listen to an entire db use only the database name.  For a deployment use an empty string.
    #change-stream-namespaces = ["mydb.col"]
    
    # additional settings
    
    # if you don't want to listen for changes to all collections in MongoDB but only a few
    # e.g. only listen for inserts, updates, deletes, and drops from mydb.mycollection
    # this setting does not initiate a copy, it is only a filter on the change event listener
    #namespace-regex = '^mydb\.col$'
    # compress requests to Elasticsearch
    #gzip = true
    # generate indexing statistics
    #stats = true
    # index statistics into Elasticsearch
    #index-stats = true
    # use the following PEM file for connections to MongoDB
    #mongo-pem-file = "/path/to/mongoCert.pem"
    # disable PEM validation
    #mongo-validate-pem-file = false
    # use the following user name for Elasticsearch basic auth
    # elasticsearch-user = "elastic"
    # use the following password for Elasticsearch basic auth
    # elasticsearch-password = "<your_es_password>"
    # use 4 go routines concurrently pushing documents to Elasticsearch
    elasticsearch-max-conns = 4
    # use the following PEM file to connections to Elasticsearch
    #elasticsearch-pem-file = "/path/to/elasticCert.pem"
    # validate connections to Elasticsearch
    #elastic-validate-pem-file = true
    # propogate dropped collections in MongoDB as index deletes in Elasticsearch
    dropped-collections = true
    # propogate dropped databases in MongoDB as index deletes in Elasticsearch
    dropped-databases = true
    # do not start processing at the beginning of the MongoDB oplog
    # if you set the replay to true you may see version conflict messages
    # in the log if you had synced previously. This just means that you are replaying old docs which are already
    # in Elasticsearch with a newer version. Elasticsearch is preventing the old docs from overwriting new ones.
    #replay = false
    # resume processing from a timestamp saved in a previous run
    resume = true
    # do not validate that progress timestamps have been saved
    #resume-write-unsafe = false
    # override the name under which resume state is saved
    #resume-name = "default"
    # use a custom resume strategy (tokens) instead of the default strategy (timestamps)
    # tokens work with MongoDB API 3.6+ while timestamps work only with MongoDB API 4.0+
    resume-strategy = 0
    # exclude documents whose namespace matches the following pattern
    #namespace-exclude-regex = '^mydb\.ignorecollection$'
    # turn on indexing of GridFS file content
    #index-files = true
    # turn on search result highlighting of GridFS content
    #file-highlighting = true
    # index GridFS files inserted into the following collections
    #file-namespaces = ["users.fs.files"]
    # print detailed information including request traces
    verbose = true
    # enable clustering mode
    # cluster-name = 'es-cn-mp91kzb8m00******'
    # do not exit after full-sync, rather continue tailing the oplog
    #exit-after-direct-reads = false
    [[mapping]]
    namespace = "mydb.hotmovies"
    index = "hotmovies"
    type = "movies"
    
    [[mapping]]
    namespace = "mydb.col"
    index = "mydbcol"
    type = "collection"
    

    参数说明

    mongo-url	MongoDB实例的主节点访问地址。
    elasticsearch-urls	阿里云Elasticsearch实例的访问地址,格式为http://<阿里云Elasticsearch实例的内网地址>:9200。
    direct-read-namespaces	指定待同步的集合,详情请参见direct-read-namespaces。本文同步的数据集为mydb数据库下的hotmovies和col集合。
    change-stream-namespaces	如果要使用MongoDB变更流功能,需要指定此参数。启用此参数后,oplog追踪会被设置为无效。
    namespace-regex	通过正则表达式指定需要监听的集合。此设置可以用来监控符合正则表达式的集合中数据的变化。
    elasticsearch-user	访问阿里云Elasticsearch实例的用户名,默认为elastic。
    注意 实际业务中不建议使用elastic用户,这样会降低系统安全性。建议使用自建用户,并给予自建用户分配相应的角色和权限,详情请参见通过Elasticsearch X-Pack角色管理实现用户权限管控。
    elasticsearch-password	对应用户的密码。elastic用户的密码在创建实例时指定,如果忘记可进行重置,重置密码的注意事项和操作步骤请参见重置实例访问密码。
    elasticsearch-max-conns	定义连接Elasticsearch的线程数。默认为4,即使用4个Go线程同时将数据同步到Elasticsearch。
    dropped-collections	默认为true,表示当删除MongoDB集合时,会同时删除Elasticsearch中对应的索引。
    dropped-databases	默认为true,表示当删除MongoDB数据库时,会同时删除Elasticsearch中对应的索引。
    resume	默认为false。设置为true,Monstache会将已成功同步到Elasticsearch的MongoDB操作的时间戳写入monstache.monstache集合中。当Monstache因为意外停止时,可通过该时间戳恢复同步任务,避免数据丢失。如果指定了cluster-name,该参数将自动开启,详情请参见resume。
    resume-strategy	指定恢复策略。仅当resume为true时生效,详情请参见resume-strategy。
    verbose	默认为false,表示不启用调试日志。
    cluster-name	指定集群名称。指定后,Monstache将进入高可用模式,集群名称相同的进程将进行协调,详情请参见cluster-name。
    mapping	指定Elasticsearch索引映射。默认情况下,数据从MongoDB同步到Elasticsearch时,索引会自动映射为数据库名.集合名。如果需要修改索引名称,可通过该参数设置,详情请参见Index Mapping。
    说明 Monstache支持丰富的参数配置,以上配置仅使用了部分参数完成数据实时同步,如果您有更复杂的同步需求,请参见Monstache config和Advanced进行配置。
    

    原文链接:https://help.aliyun.com/document_detail/171650.html
    参考链接:https://www.cnblogs.com/agopher/p/15704630.html

  • 相关阅读:
    perf脚本示例
    pacman安装时允许覆盖文件
    uniapp 使用重启方法后页面卡在启动页的解决方法
    yarn global add 后找不到命令
    讯飞语音 aiui集成
    Pad登录不上网的问题解决方案
    Python 统计指定目录下文件夹的数量(不包含文件)【转】
    tensorflow中使用Adam出现name ‘Adam‘ is not defined【转】
    unet神经网络报错‘Keyword argument not understood:‘,‘input‘解决方法【转】
    Blender中用pip安装包的办法
  • 原文地址:https://www.cnblogs.com/pythonwl/p/16243307.html
Copyright © 2020-2023  润新知