简体   繁体   English

嗨,我如何使用pymongo运行同等功能? cfg = rs.conf()db.printSlaveReplicationInfo()

[英]Hi, How do I run the equivalent using pymongo? cfg = rs.conf() db.printSlaveReplicationInfo()

1>How can I run the equivalent using pymongo? 1>如何使用pymongo运行等效项?

a> 一>

cfg = rs.conf()

b> b>

db.printSlaveReplicationInfo()

2>Using PyMongo, how can I get the details of other replica sets CLI output in the created clusters . 2>使用PyMongo,如何在创建的集群中获取其他副本集CLI输出的详细信息。 (Note:I have already successfully created cluster.Just I am writing a python script in primary to check the outputs of rs.conf() and db.printSlaveReplicationInfo() in all the replica sets inside cluster and parse the output.) (注意:我已经成功创建了集群。只需要在primary中编写一个python脚本来检查集群内所有副本集中rs.conf()db.printSlaveReplicationInfo()的输出并解析输出。)

Any help on this regard is greatly appreciable. 在这方面的任何帮助都是极大的。

The replica set configuration is stored in the "local" database in a collection called "system.replset" so the equivalent of rs.conf() would be db.system.replset.findOne() when db is local or its equivalent find_one() in Python. 副本集配置存储在名为“ rs.conf() ”的集合中的“本地”数据库中,因此当db为local或其等效的find_one() db.system.replset.findOne()时, rs.conf()的等效项为rs.conf()find_one()

db.printSlaveReplicationInfo() is a big more involved, but you can get all of that information in the local database as well. db.printSlaveReplicationInfo()涉及更多,但是您也可以在local数据库中获取所有这些信息。

It may be easier to get it via admin database command replSetGetStatus which returns a document containing oplog information for each member of the replica set, along with other details. 通过管理数据库命令replSetGetStatus可以更轻松地获取它,该命令返回包含副本集每个成员的oplog信息以及其他详细信息的文档。 Python MongoDB driver pymongo provides a method to run commands , so you can run it against the admin DB and parse out the output for information about where each member of the replica set is relative to the primary. Python MongoDB驱动程序pymongo 提供了一种运行命令的方法 ,因此您可以针对admin数据库运行该方法,并解析输出以获取有关副本集每个成员相对于主要副本的位置的信息。

I'm basically going to give you a hint rather than directly answer it, because the full answer is to simply code it. 我基本上会给您一个提示,而不是直接回答,因为完整的答案是简单地编写代码。 But you probably are unaware that you can do this simple thing in the shell: 但是您可能不知道可以在shell中执行以下简单操作:

> db.printSlaveReplicationInfo
function () {
    var startOptimeDate = null;

    function getReplLag(st) {
        assert( startOptimeDate , "how could this be null (getReplLag startOptimeDate)"     );
        print("\tsyncedTo: " + st.toString() );
        var ago = (startOptimeDate-st)/1000;
        var hrs = Math.round(ago/36)/100;
        print("\t" + Math.round(ago) + " secs (" + hrs + " hrs) behind the primary ");
    };

    function getMaster(members) {
        var found;
        members.forEach(function(row) {
            if (row.self) {
                found = row;
                return false;
            }
        });

        if (found) {
            return found;
        }
    };

    function g(x) {
        assert( x , "how could this be null (printSlaveReplicationInfo gx)" )
        print("source: " + x.host);
        if ( x.syncedTo ){
            var st = new Date( DB.tsToSeconds( x.syncedTo ) * 1000 );
            getReplLag(st);
        }
        else {
            print( "\tdoing initial sync" );
        }
    };

    function r(x) {
        assert( x , "how could this be null (printSlaveReplicationInfo rx)" );
        if ( x.state == 1 || x.state == 7 ) {  // ignore primaries (1) and arbiters (7)
            return;
        }

        print("source: " + x.name);
        if ( x.optime ) {
            getReplLag(x.optimeDate);
        }
        else {
            print( "\tno replication info, yet.  State: " + x.stateStr );
        }
    };

    var L = this.getSiblingDB("local");

    if (L.system.replset.count() != 0) {
        var status = this.adminCommand({'replSetGetStatus' : 1});
        startOptimeDate = getMaster(status.members).optimeDate;
        status.members.forEach(r);
    }
    else if( L.sources.count() != 0 ) {
        startOptimeDate = new Date();
        L.sources.find().forEach(g);
    }
    else {
        print("local.sources is empty; is this db a --slave?");
        return;
    }
}

I love REPL's , and much like python's own famous REPL you can just get a dump of what the implemented function does. 我喜欢REPL的功能 ,就像python自己的著名REPL一样,您可以仅转储已实现函数的功能。

Simples. Simples。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM