use*_*934 4 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
Run Code Online (Sandbox Code Playgroud)
第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>
Run Code Online (Sandbox Code Playgroud)
第 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>
Run Code Online (Sandbox Code Playgroud)
步骤 4. 下载“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
Run Code Online (Sandbox Code Playgroud)
第 6 步。
D:\15-1-19>monstache -f config.toml
Run Code Online (Sandbox Code Playgroud)
如果您使用 docker,您可以获得本教程
https://github.com/ziedtuihri/Monstache_Elasticsearch_Mongodb
Monstache 是一个用 Go 编写的同步守护进程,它不断地将你的 MongoDB 集合索引到 Elasticsearch 中。Monstache 使您能够使用 Elasticsearch 对 MongoDB 数据进行复杂的搜索和聚合,并轻松构建实时 Kibana 可视化和仪表板。Monstache 的文档:
https
: //rwynn.github.io/monstache-site/ github:https :
//github.com/rwynn/monstache
docker-compose.yml
version: '2.3'
networks:
test:
driver: bridge
services:
db:
image: mongo:3.0.2
expose:
- "27017"
container_name: mongodb
volumes:
- ./mongodb:/data/db
- ./mongodb_config:/data/configdb
ports:
- "27018:27017"
command: mongod --smallfiles --replSet rs0
networks:
- test
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.8.7
container_name: elasticsearch
volumes:
- ./elastic:/usr/share/elasticsearch/data
- ./elastic/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
command: elasticsearch -Enetwork.host=_local_,_site_ -Enetwork.publish_host=_local_
healthcheck:
test: "wget -q -O - http://localhost:9200/_cat/health"
interval: 1s
timeout: 30s
retries: 300
ulimits:
nproc: 65536
nofile:
soft: 65536
hard: 65536
memlock:
soft: -1
hard: -1
networks:
- test
monstache:
image: rwynn/monstache:rel4
expose:
- "8080"
ports:
- "8080:8080"
container_name: monstache
command: -mongo-url=mongodb://db:27017 -elasticsearch-url=http://elasticsearch:9200 -direct-read-namespace=Product_DB.Product -direct-read-split-max=2
links:
- elasticsearch
- db
depends_on:
db:
condition: service_started
elasticsearch:
condition: service_healthy
networks:
- test
Run Code Online (Sandbox Code Playgroud)
复制集.sh
#!/bin/bash
# this configuration is so important
echo "Starting replica set initialize"
until mongo --host 192.168.144.2 --eval "print(\"waited for connection\")"
do
sleep 2
done
echo "Connection finished"
echo "Creating replica set"
mongo --host 192.168.144.2 <<EOF
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "db:27017", priority : 1 }
]
}
)
EOF
echo "replica set created"
Run Code Online (Sandbox Code Playgroud)
1) 运行这个命令终端 $ sysctl -w vm.max_map_count=262144
如果你在服务器上工作,我不知道是否有必要
2)运行终端docker-compose build
3) 运行终端 $ docker-compose up -d
不要放下你的容器。
$码头工人
复制 mongo db 镜像的 Ipadress
$ docker 检查 id_of_mongo_image
复制 IPAddress 并将其设置在 replicaset.sh 中并运行 replicaset.sh
$ ./replicaset.sh
在终端上,您应该看到 => 已创建副本集
$ docker-compose down
4)运行终端 $ docker-compose up
最后 .......
副本集是一组维护相同数据集的mongod实例。一个副本集包含多个数据承载节点和可选的一个仲裁节点。数据承载节点中,只有一个成员为主节点,其他节点为辅节点。
所述主节点接收所有的写操作。一个副本集只能有一个能够确认写入的主节点{ w: "majority" }写关注;尽管在某些情况下,另一个 mongod 实例可能会暂时认为自己也是主要的。
查看副本集配置。使用rs.conf()
| 归档时间: |
|
| 查看次数: |
2043 次 |
| 最近记录: |