MongoDB 4.x 实时同步到 ElasticSearch 6.x +

标签 mongodb elasticsearch spring-data-mongodb

我试图找到一种简单的方法将 mongoDB 4.x 中的数据同步到 elasticsearch 6.x 。我的用例是用于由 elasticsearch 支持但不受 mongodb 支持的部分文本搜索。 MongoDB 是我的应用程序的主要数据库。

我发现的所有解决方案似乎都过时了,只支持旧版本的 mongoDB/elasticsearch。这些包括 mongodb-connector, mongodb river

使 mongoDB 中数据的任何更改 (CRUD) 自动同步到 elasticsearch 的最佳工具是什么?

最佳答案

有一个叫做 Monstache 的工具可以将 MOngoDB 数据实时迁移到 ElasticSearch。此工具支持最新的 MongoDB 版本。
它的同步守护进程是用 Go 编写的,它不断地将你的 MongoDB 集合索引到 Elasticsearch 中。 Monstache 使您能够使用 Elasticsearch 对 MongoDB 数据进行复杂的搜索和聚合,并轻松构建实时 Kibana 可视化和仪表板。
它是一个实时同步 mongodb 到 elasticsearch 的 go 守护进程。
它的Monstache。它可以在:Monstache
monstache 需要 mongodb 以复制模式运行。
下面开始配置和使用它的初始设置。
步骤1:

C:\Program Files\MongoDB\Server\4.0\bin>mongod --smallfiles --oplogSize 50 --replSet test
第2步 :
C:\Program Files\MongoDB\Server\4.0\bin>mongo

C:\Program Files\MongoDB\Server\4.0\bin>mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
Server has startup warnings:
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten]
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten]
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-01-18T16:56:44.931+0530 I CONTROL  [initandlisten]
MongoDB Enterprise test:PRIMARY>
第 3 步:验证复制。
MongoDB Enterprise test:PRIMARY> rs.status();
{
        "set" : "test",
        "date" : ISODate("2019-01-18T11:39:00.380Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1547811537, 1),
                        "t" : NumberLong(2)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1547811537, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1547811537, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1547811537, 1),
                        "t" : NumberLong(2)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1547811517, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 736,
                        "optime" : {
                                "ts" : Timestamp(1547811537, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2019-01-18T11:38:57Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1547810805, 1),
                        "electionDate" : ISODate("2019-01-18T11:26:45Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1547811537, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1547811537, 1),
                "signature" : {
                        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
                        "keyId" : NumberLong(0)
                }
        }
}
MongoDB Enterprise test:PRIMARY>
第四步。
下载“https://github.com/rwynn/monstache/releases”。
解压缩下载并调整您的 PATH 变量以包含您平台的文件夹路径。
转到 cmd 并输入 "monstache -v"# 4.13.1
Monstache 使用 TOML 格式进行配置。配置名为 config.toml 的迁移文件
第 5 步。
我的 config.toml -->
mongo-url = "mongodb://127.0.0.1:27017/?replicaSet=test"
elasticsearch-urls = ["http://localhost:9200"]

direct-read-namespaces = [ "admin.users" ]

gzip = true
stats = true
index-stats = true

elasticsearch-max-conns = 4
elasticsearch-max-seconds = 5
elasticsearch-max-bytes = 8000000 

dropped-collections = false
dropped-databases = false

resume = true
resume-write-unsafe = true
resume-name = "default"
index-files = false
file-highlighting = false
verbose = true
exit-after-direct-reads = false

index-as-update=true
index-oplog-time=true
第 6 步。
D:\15-1-19>monstache -f config.toml
Monstache Running...
Confirm Migrated Data at Elasticsearch
Add Record at Mongo
Monstache Captured the event and migrate the data to elasticsearch

关于MongoDB 4.x 实时同步到 ElasticSearch 6.x +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661890/

相关文章:

node.js - 将 Mongoose 与 IntelliJ IDEA 一起使用导致无法解析的函数或方法

elasticsearch - 如何使 Elasticsearch 更灵活?

mongodb - 如何使用spring数据mongodb聚合在组中使用sum和condition

javascript - Node.js 中复杂调用的流程/模式

ruby-on-rails - 在 Ruby on Rails 中显示类别文章和标签

javascript - winston Elasticsearch : TypeError: Elasticsearch is not a constructor

elasticsearch - ELASTICSEARCH-Inner_hits汇总

XML 中的 spring-data mongodb POJO 映射而不是注释

Spring Data Mongo MongoDB DBRef 延迟初始化

javascript - 有什么方法可以识别 mongoose/mongoDB 中的更新/更新?