MongoDB与Elasticsearch实现实时同步

 

Step1:安装mongo-connector和elastic-doc-namager插件

 

pip installmongo-connector

#Elasticsearch 1.x

pip install 'mongo-connector[elastic]'

#Elasticsearch 2.x

pip install 'mongo-connector[elastic2]'

#Elasticsearch 5.x

pip install 'mongo-connector[elastic5]'


Step2:设置MongoDB复制集(在搭建MongoDB复制集集群时已经做好)

 

Step3:Elasticsearch与MongoDB开启同步

# mongo-connector-m MongoDB_IP: MongoDB_port-tElasticsearch_IP: Elasticsearch_port-delastic2_doc_manager

 注:-m,-t分别为MongoDB和Elasticsearch的ip地址和端口

         -d:doc_manager的名称,2.x版本为elastic_2_doc_manager

Step4:同步验证:

(1)MongoDB端先插入数据

#Mongo创建数据库(对应ESIndex

rs0:PRIMARY>use test_index

switched to dbtest_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"});

(2)可视化查看及ES端检索验证

MongoDB与Elasticsearch实现实时同步_第1张图片

# curl -XGEThttp://es_ip: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"

  }

  } ]

  }

}

你可能感兴趣的:(编程珠玑)