2014년 11월 30일 일요일

setup a tailable cursor

I have a python program that retrieve data from oplog based on the namespace condition.
import pymongoimport datetimefrom pymongo import Connection

con 
= Connection()
db
=con['local']
oplog
= db['oplog.rs']
newCol
=db['newCol']
for doc in oplog.find({'ns':'hospital.patients'}):
     
if (doc['op']!='n'):
           
if(doc['op'] == 'u'):
               key 
= doc['o2']['_id']
           
else:
               key 
= doc['o']['_id']
           
if(key == 'ABCD'):
               newCol
.insert({'key':key, 'time':doc['ts'].as_datetime(), 'op_type':doc['op'],'Operation':str(doc['o'])})
i have to clear the newCol collection and run the script every time when a new record is added to the collection from oplog.
i want to make the script tailable.
ie , when a record is added into the oplog, the scipt check the condition of namespace for that record only and if it satisfies then added into the newCol collection else discard . what i changes i have to make to script
thank you......



There is a project called mongo-connector which watches oplog in one
cluster to replay the operations on another.

Luckily, it's written in Python so you can look at the code there and
see how to do what you want to do.

https://github.com/10gen-labs/mongo-connector and in particular:
https://github.com/10gen-labs/mongo-connector/blob/00d498d24dc2201cc891be06e56a4267b7a1d8b7/mongo_connector/oplog_manager.py



See here for example of tailing the oplog in python:




Thank you sir for ur valuable info...


댓글 없음:

댓글 쓰기