Example usage for com.mongodb MapReduceOutput getOutputCount

List of usage examples for com.mongodb MapReduceOutput getOutputCount

Introduction

In this page you can find the example usage for com.mongodb MapReduceOutput getOutputCount.

Prototype

public int getOutputCount() 

Source Link

Document

Get the number of documents generated as a result of this map reduce

Usage

From source file:com.exorath.service.connector.service.MongoDatabaseProvider.java

License:Apache License

@Override
public ServerInfo getServerInfo(Filter filter, Long minLastUpdate) {
    //Fingers crossed
    BasicDBObjectBuilder builder = BasicDBObjectBuilder.start();
    if (filter.getGameId() != null)
        builder.append("gameId", filter.getGameId());
    if (filter.getMapId() != null)
        builder.append("mapId", filter.getMapId());
    if (filter.getFlavorId() != null)
        builder.append("flavorId", filter.getFlavorId());
    builder.append("expiry", new BasicDBObject("$gte", System.currentTimeMillis()));
    MapReduceCommand mapReduceCommand = new MapReduceCommand(datastore.getDB().getCollection(collectionName),
            "function(){" + "var ret = {pc:0, opc:0, sc:0, osc:0};" + "ret.pc = this.pc; ret.sc = 1;"
                    + "if(this.joinable && this.pc < this.mpc){ ret.opc = this.mpc - this.pc; ret.osc = 1;}"
                    + "emit('server', ret);}",
            "function(key, values){" + "var ret = {pc:0, opc:0, sc:0, osc:0};"
                    + "values.forEach( function(value) {" + "       ret.pc+= value.pc; ret.sc++;"
                    + "       ret.osc+= value.osc; ret.opc+= value.opc" + "   });" + "return ret;" + "}",
            null, MapReduceCommand.OutputType.INLINE, builder.get());
    MapReduceOutput results = datastore.getDB().getCollection(collectionName).mapReduce(mapReduceCommand);
    if (results.getOutputCount() == 0)
        return new ServerInfo(0, 0, 0, 0, System.currentTimeMillis());
    if (results.getOutputCount() > 1)
        throw new IllegalStateException("mapReduce returned multiple results.");
    for (DBObject res : results.results()) {
        DBObject val = (DBObject) res.get("value");
        return new ServerInfo(((Double) val.get("pc")).intValue(), ((Double) val.get("sc")).intValue(),
                ((Double) val.get("osc")).intValue(), ((Double) val.get("opc")).intValue(),
                System.currentTimeMillis());
    }/*from  www.j  av  a  2 s .c om*/
    ////         MapreduceResults<ServerInfo> results = datastore.mapReduce(MapreduceType.INLINE, getFilterQuery(filter), ServerInfo.class, mapReduceCommand);
    ////        if(results.getCounts().getOutputCount() == 0) {
    ////            System.out.println("output 0 :*");
    //            return null;
    //        }
    //        System.out.println("ms: " + results.getElapsedMillis());
    //        results.forEach(info -> System.out.println(info.getOpenSlotCount()));
    return null;
}

From source file:fr.gouv.vitam.mdbes.MainMapReduce.java

License:Open Source License

protected static void oneShot(MongoDbAccess dbvitam) throws InvalidParseOperationException,
        InvalidExecOperationException, InstantiationException, IllegalAccessException {
    // Requesting
    if (map == null) {
        map = "function() {" + "flattened = serializeDocFiltered(this, MAXDEPTH, FILTER);"
                + "for (var key in flattened) {" + "var value = flattened[key];"
                + "var valueType = varietyTypeOf(value);"
                + "var finalvalue = { types : [ valueType ], occurences : 1};"
                //                + "var finalvalue = { occurences : 1};"
                + "emit(key, finalvalue); } }";
    }/*w  ww . ja v a 2s.  c o m*/
    if (reduce == null) {
        reduce = "function(key,values) {" + "var typeset = new Set();" + "var occur = 0;"
                + "for (var idx = 0; idx < values.length; idx++) {" + "occur += values[idx].occurences;"
                + "typeset.add(values[idx].types);"
                + "} return { types : typeset.asArray(), occurences : occur }; }";
        //                + "} return { occurences : occur }; }";

    }
    if (output == null) {
        output = "AttributeUsage";
    }
    MapReduceCommand mapReduceCommand = new MapReduceCommand(dbvitam.daips.collection, map, reduce, output,
            OutputType.REDUCE, new BasicDBObject());
    if (options != null) {
        String[] optionsArray = options.split(",");
        Map<String, Object> mapScope = new HashMap<String, Object>();
        for (String string : optionsArray) {
            String[] kv = string.split("=");
            mapScope.put(kv[0], getParsedString(kv[1]));
        }
        mapReduceCommand.setScope(mapScope);
    } else {
        Map<String, Object> mapScope = new HashMap<String, Object>();
        mapScope.put("MAXDEPTH", 5);
        mapScope.put("FILTER", "_dds");
        mapReduceCommand.setScope(mapScope);
    }
    mapReduceCommand.addExtraOption("nonAtomic", true);
    mapReduceCommand.addExtraOption("jsMode", true);
    MapReduceOutput output = dbvitam.daips.collection.mapReduce(mapReduceCommand);
    System.out.println("Duration: " + output.getDuration() + " Saved into: " + output.getCollectionName()
            + " Input: " + output.getInputCount() + " Emit: " + output.getEmitCount() + " Output: "
            + output.getOutputCount() + "\nCmd: " + output.getCommand());
    Iterable<DBObject> iterable = output.results();
    for (DBObject dbObject : iterable) {
        System.out.println(dbObject.toString());
    }
}

From source file:org.springframework.data.mongodb.core.mapreduce.MapReduceResults.java

License:Apache License

private static MapReduceCounts parseCounts(final MapReduceOutput mapReduceOutput) {
    return new MapReduceCounts(mapReduceOutput.getInputCount(), mapReduceOutput.getEmitCount(),
            mapReduceOutput.getOutputCount());
}