Example usage for com.mongodb MapReduceCommand setVerbose

List of usage examples for com.mongodb MapReduceCommand setVerbose

Introduction

In this page you can find the example usage for com.mongodb MapReduceCommand setVerbose.

Prototype

public void setVerbose(final Boolean verbose) 

Source Link

Document

Sets the verbosity of the MapReduce job, defaults to 'true'

Usage

From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java

License:Open Source License

@SuppressWarnings("deprecation")
@Override/*from  w ww .  ja  va 2  s .  c o m*/
public void execute() throws Exception {
    MongoClient mdb = MongoFactory.getInst().getMongo(sName);

    if (mdb == null)
        throw new Exception("no server selected");

    if (sDb == null)
        throw new Exception("no database selected");

    MongoFactory.getInst().setActiveDB(sDb);

    DB db = mdb.getDB(sDb);
    BasicDBObject cmdMap = parseMongoCommandString(db, cmd);

    if (!cmdMap.containsField("mapreduceArgs"))
        throw new Exception("no mapReduce document");

    DBCollection collection = db.getCollection(sColl);

    // Build the Map
    BasicDBObject options = (BasicDBObject) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(2);

    String outputCollection = null;
    String outputDB = null;
    MapReduceCommand.OutputType outputType = MapReduceCommand.OutputType.INLINE;

    if (options.get("out") instanceof String) {
        outputCollection = (String) options.get("out");
        outputType = MapReduceCommand.OutputType.REPLACE;
    } else if (options.get("out") instanceof BasicDBObject) {
        BasicDBObject out = (BasicDBObject) options.get("out");

        if (out.containsField("inline")) {
            outputCollection = null;
        } else if (out.containsField("replace")) {
            outputCollection = (String) out.get("replace");
            outputType = MapReduceCommand.OutputType.REPLACE;
        } else if (out.containsField("merge")) {
            outputCollection = (String) out.get("merge");
            outputType = MapReduceCommand.OutputType.MERGE;
        } else if (out.containsField("reduce")) {
            outputCollection = (String) out.get("reduce");
            outputType = MapReduceCommand.OutputType.REDUCE;
        }

        if (out.containsField("db"))
            outputDB = (String) out.get("db");
    }

    MapReduceCommand mrc = new MapReduceCommand(collection,
            ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(0)).getCode(),
            ((Code) ((BasicDBList) cmdMap.get("mapreduceArgs")).get(1)).getCode(), outputCollection, outputType,
            (BasicDBObject) options.get("query"));

    if (outputDB != null)
        mrc.setOutputDB(outputDB);

    if (options.containsField("sort") && options.get("sort") instanceof DBObject)
        mrc.setSort((DBObject) options.get("sort"));

    if (options.containsField("scope") && options.get("scope") instanceof DBObject)
        mrc.setScope(((DBObject) options.get("scope")).toMap());

    if (options.containsField("finalize") && options.get("scope") instanceof Code)
        mrc.setFinalize(((Code) options.get("scope")).getCode());

    if (options.containsField("limit"))
        mrc.setLimit(StringUtil.toInteger(options.get("limit"), -1));

    mrc.addExtraOption("jsMode", StringUtil.toBoolean(options.get("jsMode"), false));
    mrc.setVerbose(StringUtil.toBoolean(options.get("verbose"), false));

    // Run the actual mapreduce function
    MapReduceOutput mro = collection.mapReduce(mrc);

    // Pull the inline results
    if (mro.getOutputCollection() == null) {
        dbListResult = new BasicDBList();
        Iterable<DBObject> it = mro.results();
        for (DBObject dbo : it) {
            dbListResult.add(dbo);
        }
    }

    BasicDBObject dbo = mro.getRaw();
    StringBuilder sb = new StringBuilder();

    if (dbo.containsField("timeMillis"))
        sb.append("Time=").append(dbo.get("timeMillis")).append("ms; ");

    if (dbo.containsField("counts")) {
        BasicDBObject counts = (BasicDBObject) dbo.get("counts");
        sb.append("Counts: input=" + counts.get("input"));
        sb.append("; emit=" + counts.get("emit"));
        sb.append("; reduce=" + counts.get("reduce"));
        sb.append("; output=" + counts.get("output"));
    }

    setMessage(sb.toString());
}

From source file:org.mongodb.morphia.MapReduceOptions.java

License:Apache License

@SuppressWarnings("deprecation")
MapReduceCommand toCommand(final Mapper mapper) {
    if (query.getOffset() != 0 || query.getFieldsObject() != null) {
        throw new QueryException("mapReduce does not allow the offset/retrievedFields query ");
    }/*from w  w  w  . jav a2 s  .c  om*/

    final DBCollection dbColl = inputCollection != null
            ? getQuery().getCollection().getDB().getCollection(inputCollection)
            : query.getCollection();
    final String target = outputCollection != null ? outputCollection
            : mapper.getMappedClass(resultType).getCollectionName();

    final MapReduceCommand command = new MapReduceCommand(dbColl, map, reduce, target, outputType,
            query.getQueryObject());
    command.setBypassDocumentValidation(bypassDocumentValidation);
    command.setCollation(collation);
    command.setFinalize(finalize);
    command.setJsMode(jsMode);
    command.setLimit(limit);
    command.setMaxTime(maxTimeMS, TimeUnit.MILLISECONDS);
    command.setOutputDB(outputDB);
    command.setReadPreference(readPreference);
    command.setScope(scope);
    command.setSort(query.getSortObject());
    command.setVerbose(verbose);

    return command;
}