package com.xxx.statistic.mongo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.xxx.statistic.constants.Constants;
import com.xxx.statistic.file.ExcelWriter;
import com.xxx.statistic.util.GsonHelper;
import com.mongodb.AggregationOutput;
import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
public class MongoStatistic {
public static Mongo mongo;
public static void genMongoStatisticInfo()throws Exception{
DB db = mongo.getDB(Constants.MONGO_XXX_DB);
Set<String> collectionNames = db.getCollectionNames();
List<Map<String,Object>> records = new ArrayList<Map<String,Object>>();
for(String name : collectionNames){
if(name.startsWith("xxx")){
Map<String,Object> ret = new HashMap<String, Object>();
DBCollection collection = db.getCollection(name);
DBObject date = new BasicDBObject();
date.put("$gte", Integer.parseInt(Constants.START_DATE));
date.put("$lte", Integer.parseInt(Constants.END_DATE));
Long count = collection.count(new BasicDBObject("DATE",date));
ret.put("collection", name);
ret.put("avg_day_count", count.doubleValue()/n+"");
CommandResult result = collection.getStats();
ret.put("total_count",result.get("count").toString());
ret.put("size",result.get("size").toString());
ret.put("avgObjSize",result.get("avgObjSize").toString());
ret.put("storageSize",result.get("storageSize").toString());
ret.put("numExtents",result.get("numExtents").toString());
ret.put("totalIndexSize",result.get("totalIndexSize").toString());
records.add(ret);
}
}
ExcelWriter.write("mongo","xxx",
new String[]{"collection","avg_day_count",
"total_count","size","avgObjSize",
"storageSize","numExtents","totalIndexSize"},
new String[]{"集合名","日均数据增量",
"总数据量","占用空间(字节)","平均每条记录占用空间(字节)",
"分配的空间(字节)","连续分配的数据块","索引总数"},
records);
}
public static void genMongoInfo()throws Exception{
DB db = mongo.getDB(Constants.MONGO_RMM_DB);
DBCollection collection = db.getCollection("xxx");
DBObject date = new BasicDBObject();
date.put("$gte", Integer.parseInt(Constants.START_DATE));
date.put("$lte", Integer.parseInt(Constants.END_DATE));
DBObject match = new BasicDBObject("$match", new BasicDBObject("date", date));
DBObject fields = new BasicDBObject();
fields.put("_id", "$processorId");
fields.put("type", new BasicDBObject("$max", "$type"));
fields.put("triggerType", new BasicDBObject("$max", "$triggerType"));
fields.put("calculate_num", new BasicDBObject("$sum", "$calculate"));
fields.put("receive_num", new BasicDBObject("$sum", "$receive"));
DBObject group = new BasicDBObject("$group", fields);
AggregationOutput output = collection.aggregate(match,group);
CommandResult result = output.getCommandResult();
List<Map<String,Object>> records = GsonHelper.lmso(result.get("result").toString());
for(Map<String,Object> map :records){
//TODO 数据加工
}
ExcelWriter.write(param1,...);
}
@SuppressWarnings("deprecation")
public static void initMongo() throws Exception{
try {
mongo = new Mongo(Constants.MONGO_IP, Integer.parseInt(Constants.MONGO_PORT));
} catch(Exception e) {
throw new Exception(e);
}
DB db = mongo.getDB("admin");
boolean ok = db.authenticate(Constants.MONGO_USER_NAME , Constants.MONGO_PASSWORD.toCharArray());
if(!ok){
mongo = null;
}
}
}
Mongo Group
最新推荐文章于 2024-07-24 01:44:55 发布