2016-08-19 68 views
0

我需要弄清楚MongoDB在過去一小時內對寫入操作執行了多少次。MongDB級寫入次數

是否有一種簡單的方法來查找這些統計信息,這些統計信息是創建警報所必需的。如果解決方案是命令驅動或基於Java的,這將非常有幫助。

+0

你要解決什麼樣的問題? – profesor79

+0

看看這個,一些命令返回數據庫統計:https://docs.mongodb.com/manual/administration/monitoring/ – yellowB

+0

這是更多的數據庫管理問題比編程問題?你可以在http://dba.stackexchange.com/找到你想要的答案。 –

回答

0

挖掘完MongoDB jdbc驅動程序的源代碼後。我終於能夠得到所需要的東西。下面是它的代碼

public class MongoJmxStat { 

    private MongoClient mongo; 

    public MongoJmxStat(MongoClient mongo) { 
     this.mongo = mongo; 
    } 

    public CommandResult getServerStatus() { 
     CommandResult result = getDb("mongoDB").command("serverStatus"); 
     if (!result.ok()) { 

      throw new MongoException("could not query for server status. Command Result = " + result); 
     } 

     return result; 
    } 

    public DB getDb(String databaseName) { 
     return MongoDbUtils.getDB(mongo, databaseName); 
    } 

    private int getOpCounter(String key) { 
     DBObject opCounters = (DBObject) getServerStatus().get("opcounters"); 
     return (Integer) opCounters.get(key); 
    } 

    @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Write operation count") 
    public int getWriteCount() { 
     return getOpCounter("insert") + getOpCounter("update") + getOpCounter("delete"); 
    } 

    @ManagedMetric(metricType = MetricType.COUNTER, displayName = "Read operation count") 
    public int getReadCount() { 
     return getOpCounter("query") + getOpCounter("getmore"); 
    } 

}