最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用MongoDB中oplog機(jī)制實(shí)現(xiàn)準(zhǔn)實(shí)時(shí)數(shù)據(jù)的操作監(jiān)控

 更新時(shí)間:2017年05月10日 14:06:03   作者:零の雜貨鋪  
MongoDB 的Replication是通過(guò)一個(gè)日志來(lái)存儲(chǔ)寫(xiě)操作的,這個(gè)日志就叫做oplog,而下面這篇文章主要給大家介紹了利用MongoDB中oplog機(jī)制實(shí)現(xiàn)準(zhǔn)實(shí)時(shí)數(shù)據(jù)的操作監(jiān)控的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。

前言

最近有一個(gè)需求是要實(shí)時(shí)獲取到新插入到MongoDB的數(shù)據(jù),而插入程序本身已經(jīng)有一套處理邏輯,所以不方便直接在插入程序里寫(xiě)相關(guān)程序,傳統(tǒng)的數(shù)據(jù)庫(kù)大多自帶這種觸發(fā)器機(jī)制,但是Mongo沒(méi)有相關(guān)的函數(shù)可以用(也可能我了解的太少了,求糾正),當(dāng)然還有一點(diǎn)是需要python實(shí)現(xiàn),于是收集整理了一個(gè)相應(yīng)的實(shí)現(xiàn)方法。

一、引子

首先可以想到,這種需求其實(shí)很像數(shù)據(jù)庫(kù)的主從備份機(jī)制,從數(shù)據(jù)庫(kù)之所以能夠同步主庫(kù)是因?yàn)榇嬖谀承┲笜?biāo)來(lái)做控制,我們知道MongoDB雖然沒(méi)有現(xiàn)成觸發(fā)器,但是它能夠?qū)崿F(xiàn)主從備份,所以我們就從它的主從備份機(jī)制入手。

二、OPLOG

首先,需要以master模式來(lái)打開(kāi)mongod守護(hù),命令行使用–master,或者配置文件增加master鍵為true。

此時(shí),我們可以在Mongo的系統(tǒng)庫(kù)local里見(jiàn)到新增的collection——oplog,此時(shí)oplog.$main里就會(huì)存儲(chǔ)進(jìn)oplog信息,如果此時(shí)還有充當(dāng)從數(shù)據(jù)庫(kù)的Mongo存在,就會(huì)還有一些slaves的信息,由于我們這里并不是主從同步,所以不存在這些集合。

再來(lái)看看oplog結(jié)構(gòu):

"ts" : Timestamp(6417682881216249, 1), 時(shí)間戳
"h" : NumberLong(0), 長(zhǎng)度
"v" : 2, 
"op" : "n", 操作類(lèi)型
"ns" : "", 操作的庫(kù)和集合
"o2" : "_id" update條件
"o" : {} 操作值,即document

這里需要知道op的幾種屬性:

insert,'i'
update, 'u'
remove(delete), 'd'
cmd, 'c'
noop, 'n' 空操作

從上面的信息可以看出,我們只要不斷讀取到ts來(lái)做對(duì)比,然后根據(jù)op即可判斷當(dāng)前出現(xiàn)的是什么操作,相當(dāng)于使用程序?qū)崿F(xiàn)了一個(gè)從數(shù)據(jù)庫(kù)的接收端。

三、CODE

在Github上找到了別人的實(shí)現(xiàn)方式,不過(guò)它的函數(shù)庫(kù)太老舊,所以在他的基礎(chǔ)上進(jìn)行修改。

Github地址:https://github.com/RedBeard0531/mongo-oplog-watcher

mongo_oplog_watcher.py如下:

#!/usr/bin/python
import pymongo
import re
import time
from pprint import pprint # pretty printer
from pymongo.errors import AutoReconnect

class OplogWatcher(object):
  def __init__(self, db=None, collection=None, poll_time=1.0, connection=None, start_now=True):
    if collection is not None:
      if db is None:
        raise ValueError('must specify db if you specify a collection')
      self._ns_filter = db + '.' + collection
    elif db is not None:
      self._ns_filter = re.compile(r'^%s\.' % db)
    else:
      self._ns_filter = None

    self.poll_time = poll_time
    self.connection = connection or pymongo.Connection()

    if start_now:
      self.start()

  @staticmethod
  def __get_id(op):
    id = None
    o2 = op.get('o2')
    if o2 is not None:
      id = o2.get('_id')

    if id is None:
      id = op['o'].get('_id')

    return id

  def start(self):
    oplog = self.connection.local['oplog.$main']
    ts = oplog.find().sort('$natural', -1)[0]['ts']
    while True:
      if self._ns_filter is None: 
        filter = {}
      else:
        filter = {'ns': self._ns_filter}
      filter['ts'] = {'$gt': ts}
      try:
        cursor = oplog.find(filter, tailable=True)
        while True:
          for op in cursor:
            ts = op['ts']
            id = self.__get_id(op)
            self.all_with_noop(ns=op['ns'], ts=ts, op=op['op'], id=id, raw=op)
          time.sleep(self.poll_time)
          if not cursor.alive:
            break
      except AutoReconnect:
        time.sleep(self.poll_time)

  def all_with_noop(self, ns, ts, op, id, raw):
    if op == 'n':
      self.noop(ts=ts)
    else:
      self.all(ns=ns, ts=ts, op=op, id=id, raw=raw)

  def all(self, ns, ts, op, id, raw):
    if op == 'i':
      self.insert(ns=ns, ts=ts, id=id, obj=raw['o'], raw=raw)
    elif op == 'u':
      self.update(ns=ns, ts=ts, id=id, mod=raw['o'], raw=raw)
    elif op == 'd':
      self.delete(ns=ns, ts=ts, id=id, raw=raw)
    elif op == 'c':
      self.command(ns=ns, ts=ts, cmd=raw['o'], raw=raw)
    elif op == 'db':
      self.db_declare(ns=ns, ts=ts, raw=raw)

  def noop(self, ts):
    pass

  def insert(self, ns, ts, id, obj, raw, **kw):
    pass

  def update(self, ns, ts, id, mod, raw, **kw):
    pass

  def delete(self, ns, ts, id, raw, **kw):
    pass

  def command(self, ns, ts, cmd, raw, **kw):
    pass

  def db_declare(self, ns, ts, **kw):
    pass

class OplogPrinter(OplogWatcher):
  def all(self, **kw):
    pprint (kw)
    print #newline

if __name__ == '__main__':
  OplogPrinter()

首先是實(shí)現(xiàn)一個(gè)數(shù)據(jù)庫(kù)的初始化,設(shè)定一個(gè)延遲時(shí)間(準(zhǔn)實(shí)時(shí)):

self.poll_time = poll_time
self.connection = connection or pymongo.MongoClient()

主要的函數(shù)是start() ,實(shí)現(xiàn)一個(gè)時(shí)間的比對(duì)并進(jìn)行相應(yīng)字段的處理:

def start(self):
 oplog = self.connection.local['oplog.$main']
 #讀取之前提到的庫(kù)
 ts = oplog.find().sort('$natural', -1)[0]['ts']
 #獲取一個(gè)時(shí)間邊際
 while True:
 if self._ns_filter is None:
  filter = {}
 else:
  filter = {'ns': self._ns_filter}
 filter['ts'] = {'$gt': ts}
 try:
  cursor = oplog.find(filter)
  #對(duì)此時(shí)間之后的進(jìn)行處理
  while True:
  for op in cursor:
   ts = op['ts']
   id = self.__get_id(op)
   self.all_with_noop(ns=op['ns'], ts=ts, op=op['op'], id=id, raw=op)
   #可以指定處理插入監(jiān)控,更新監(jiān)控或者刪除監(jiān)控等
  time.sleep(self.poll_time)
  if not cursor.alive:
   break
 except AutoReconnect:
  time.sleep(self.poll_time)

循環(huán)這個(gè)start函數(shù),在all_with_noop這里就可以編寫(xiě)相應(yīng)的監(jiān)控處理邏輯。

這樣就可以實(shí)現(xiàn)一個(gè)簡(jiǎn)易的準(zhǔn)實(shí)時(shí)Mongo數(shù)據(jù)庫(kù)操作監(jiān)控器,下一步就可以配合其他操作來(lái)對(duì)新入庫(kù)的程序進(jìn)行相應(yīng)處理。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB備份與恢復(fù)的實(shí)踐詳解

    分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB備份與恢復(fù)的實(shí)踐詳解

    這篇文章主要介紹了分布式文檔存儲(chǔ)數(shù)據(jù)庫(kù)之MongoDB備份與恢復(fù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • MongoDB常用操作匯總

    MongoDB常用操作匯總

    MongoDB 是由C++語(yǔ)言編寫(xiě)的,是一個(gè)基于分布式文件存儲(chǔ)的開(kāi)源數(shù)據(jù)庫(kù)系統(tǒng)。在高負(fù)載的情況下,添加更多的節(jié)點(diǎn),可以保證服務(wù)器性能。MongoDB 旨在為WEB應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。
    2017-05-05
  • Ubuntu 14.04  安裝 MongoDB 及 PHP MongoDB Driver詳細(xì)介紹

    Ubuntu 14.04 安裝 MongoDB 及 PHP MongoDB Driver詳細(xì)介紹

    這篇文章主要介紹了Ubuntu 14.04 安裝 MongoDB 及 PHP MongoDB Driver詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • ?PostgreSQL?與MongoDB使用對(duì)比分析

    ?PostgreSQL?與MongoDB使用對(duì)比分析

    這篇文章主要介紹了為什么?PostgreSQL?能代替?MongoDB?,需要的朋友可以參考下
    2023-12-12
  • Mongodb增加、移除Arbiter節(jié)點(diǎn)實(shí)例

    Mongodb增加、移除Arbiter節(jié)點(diǎn)實(shí)例

    這篇文章主要介紹了Mongodb增加、移除Arbiter節(jié)點(diǎn)實(shí)例,Arbiter是搭建Mongodb集群的一個(gè)必備節(jié)點(diǎn),需要的朋友可以參考下
    2015-01-01
  • MongoDB中的bson介紹和使用實(shí)例

    MongoDB中的bson介紹和使用實(shí)例

    這篇文章主要介紹了MongoDB中的bson介紹和使用實(shí)例,本文講解了什么是bson、bson在MongoDB中的使用、幾個(gè)BSON的例子等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • mongodb中按天進(jìn)行聚合查詢(xún)的實(shí)例教程

    mongodb中按天進(jìn)行聚合查詢(xún)的實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于mongodb中按天進(jìn)行聚合查詢(xún)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mongodb具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • MongoDB快速入門(mén)及其SpringBoot實(shí)戰(zhàn)教程

    MongoDB快速入門(mén)及其SpringBoot實(shí)戰(zhàn)教程

    MongoDB是一個(gè)開(kāi)源、高性能、無(wú)模式的文檔型數(shù)據(jù)庫(kù),當(dāng)初的設(shè)計(jì)就是用于簡(jiǎn)化開(kāi)發(fā)和方便擴(kuò)展,是NoSQL數(shù)據(jù)庫(kù)產(chǎn)品中的一種,它支持的數(shù)據(jù)結(jié)構(gòu)非常松散,是一種類(lèi)似于JSON的格式叫BSON,本文介紹MongoDB快速入門(mén)及其SpringBoot實(shí)戰(zhàn),感興趣的朋友一起看看吧
    2023-12-12
  • MySQL和MongoDB設(shè)計(jì)實(shí)例對(duì)比分析

    MySQL和MongoDB設(shè)計(jì)實(shí)例對(duì)比分析

    MySQL是關(guān)系型數(shù)據(jù)庫(kù)中的明星,MongoDB是文檔型數(shù)據(jù)庫(kù)中的翹楚。
    2011-07-07
  • mongodb操作的模塊手動(dòng)封裝

    mongodb操作的模塊手動(dòng)封裝

    這篇文章主要介紹了mongodb操作的模塊手動(dòng)封裝的相關(guān)資料,這里提供實(shí)例幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
    2017-08-08

最新評(píng)論

仪陇县| 宽城| 台南县| 金川县| 西宁市| 通城县| 济宁市| 岳普湖县| 友谊县| 子洲县| 宁陵县| 浏阳市| 巴林右旗| 巴马| 万山特区| 武城县| 兰溪市| 大埔区| 盐源县| 库尔勒市| 青神县| 镶黄旗| 泸西县| 大化| 通渭县| 蒙山县| 六安市| 龙游县| 黑水县| 平原县| 色达县| 金湖县| 永福县| 扎兰屯市| 威海市| 陵川县| 从化市| 崇明县| 伊春市| 隆化县| 鹤峰县|