初次接触Mongodb与Elasticsearch,学习Mongodb与Elasticsearch的实时同步,参考了“铭毅天下”的“mongo-connector实现MongoDB与elasticsearch实时同步深入详解”,记录配置过程,留作笔记。
mongo-connector GitHub
#下载mongo-connector
pip install mongo-connector
若出现Pymongo>=2.9的错误提醒需要安装Pymongo
pip install pymongo
#对pymongo进行升级
pip --upgrade pymongo
可用如下测试安装是否成功,在Terminal键入python,进入python
>>> import pymongo
#以上这步报错就重新安装吧
>>> client = pymongo.MongoClient("localhost", 27017)
>>> db = client.test
>>> db.name
u'test'
>>> db.my_collection
Collection(Database(MongoClient('localhost', 27017), u'test'), u'my_collection')
>>> db.my_collection.insert_one({"x": 10}).inserted_id
ObjectId('4aba15ebe23f6b53b0000000')
>>> db.my_collection.insert_one({"x": 8}).inserted_id
ObjectId('4aba160ee23f6b543e000000')
>>> db.my_collection.insert_one({"x": 11}).inserted_id
ObjectId('4aba160ee23f6b543e000002')
>>> db.my_collection.find_one()
{u'x': 10, u'_id': ObjectId('4aba15ebe23f6b53b0000000')}
>>> for item in db.my_collection.find():
... print(item["x"])
...
10
8
11
>>> db.my_collection.create_index("x")
u'x_1'
>>> for item in db.my_collection.find().sort("x", pymongo.ASCENDING):
... print(item["x"])
...
8
10
11
>>> [item["x"] for item in db.my_collection.find().limit(2).skip(1)]
[8, 11]
#Elasticsearch 1.x
pip install 'mongo-connector[elastic]'
#Elasticsearch 2.x
pip install 'mongo-connector[elastic2]'
#Elasticsearch 5.x
pip install 'mongo-connector[elastic5]'
MongoDB复制是将数据同步在多个服务器的过程。
复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。
复制还允许您从硬件故障和服务中断中恢复数据。
通过指定 –replSet 选项来启动mongoDB。–replSet 基本语法格式如下:
./mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
实例
./mongod --port 27017 --dbpath "/data/rs0" --replSet rs0
注意:这里需要在/目录下创建好/data/rs0这个文件。
以上实例会启动一个名为rs0的MongoDB实例,其端口号为27017。
进入mongodb/bin下
#启动Mongo客户端并连接上mongoDB服务
./mongo
在Mongo客户端输入rs.initiate(),初始化副本集。
> rs.initiate()
{
"info2" : "no configuration specified. Using a default configuration for the set",
"me" : "b48eafd69929:27017",
"ok" : 1
}
我们可以使用rs.conf()来查看副本集的配置。
rs0:SECONDARY> rs.conf()
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "test:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("577b74bd0ba41a313110ad62")
}
}
看副本集状态使用 rs.status() 命令。
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2016-07-05T08:50:55.272Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "test:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 115,
"optime" : {
"ts" : Timestamp(1467708606, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-07-05T08:50:06Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1467708605, 2),
"electionDate" : ISODate("2016-07-05T08:50:05Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
[root@test]# mongo-connector -m localhost:27017 -t localhost:9200 -d elastic2_doc_manager
Logging to mongo-connector.log.
参数含义:
-m: mongodb的地址与端口,端口默认为27017。
-t:ES的地址与端口,端口默认为9200。
-d:doc manager的名称,2.x版本为: elastic2-doc-manager。
注意:
Elasticsearch1.x的版本使用elastic-doc-manager
Elasticsearch2.x以上的版本使用elastic2-doc-manager。
(个人理解,因为博主的Elasticsearch5.x使用elastic5-doc-manager不管用,使用elastic2-doc-manager连接上了)
#Mongo创建数据库(对应ES的Index)
rs0:PRIMARY> use test_index
switched to db test_index
#Mongo中插入数据(其中col_02对应ES中的Type)
rs0:PRIMARY> db.col_02.insert({name:"zhangsan", birth:"1964-03-21", sex:"man", company:"baidu"});
WriteResult({ "nInserted" : 1 })
rs0:PRIMARY> db.col_02.insert({name:"lisi", birth:"1954-03-21", sex:"man", company:"huawei"});
[root@test]# curl -XGET http://localhost:9200/test_index/col_02/_search?pretty
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 8,
"successful" : 8,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [ {
"_index" : "test_index",
"_type" : "col_02",
"_id" : "577b7d8ceb8e3dc2d1db12a9",
"_score" : 1.0,
"_source" : {
"company" : "huawei",
"name" : "lisi",
"birth" : "1954-03-21",
"sex" : "man"
}
}, {
"_index" : "test_index",
"_type" : "col_02",
"_id" : "577b7d4aeb8e3dc2d1db12a7",
"_score" : 1.0,
"_source" : {
"company" : "baidu",
"name" : "zhangsan",
"birth" : "1964-03-21",
"sex" : "man"
}
} ]
}
}
至此Mongodb与Elasticsearch实现同步,可在Mongodb 中对数据进行增删改然后查询Elasticsearch看数据的变化。