Example usage for com.mongodb MapReduceCommand setOutputDB

List of usage examples for com.mongodb MapReduceCommand setOutputDB

Introduction

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

Prototype

public void setOutputDB(@Nullable final String outputDB) 

Source Link

Document

Sets the (optional) database name where the output collection should reside

Usage

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void mapReduce(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    String map = getStringFieldValue(Item.mrMap);
    String reduce = getStringFieldValue(Item.mrReduce);
    String finalize = getStringFieldValue(Item.mrFinalize);
    String stype = getStringFieldValue(Item.mrType);
    final OutputType type = OutputType.valueOf(stype.toUpperCase());
    String out = getStringFieldValue(Item.mrOut);
    if (type != OutputType.INLINE && (out.isEmpty())) {
        new InfoDialog(id, null, null, "Output collection cannot be empty if type is not inline.").show();
        return;/*from   w  ww  .ja v  a  2 s.c o  m*/
    }

    String outDB = getStringFieldValue(Item.mrOutDB);
    DBObject query = ((DocBuilderField) getBoundUnit(Item.mrQuery)).getDBObject();
    int limit = getIntFieldValue(Item.mrLimit);
    final MapReduceCommand cmd = new MapReduceCommand(col, map, reduce, out, type, query);
    DBObject sort = ((DocBuilderField) getBoundUnit(Item.mrSort)).getDBObject();
    if (sort != null) {
        cmd.setSort(sort);
    }
    if (!outDB.isEmpty()) {
        cmd.setOutputDB(outDB);
    }
    if (!finalize.isEmpty()) {
        cmd.setFinalize(finalize);
    }
    if (limit > 0) {
        cmd.setLimit(limit);
    }

    if (getBooleanFieldValue(Item.mrJSMode)) {
        cmd.addExtraOption("jsMode", true);
    }

    final BasicDBObject cmdobj = (BasicDBObject) cmd.toDBObject();
    if (getBooleanFieldValue(Item.mrOutSharded)) {
        ((BasicDBObject) cmdobj.get("out")).put("sharded", true);
    }
    if (getBooleanFieldValue(Item.mrNonAtomic)) {
        ((BasicDBObject) cmdobj.get("out")).put("nonAtomic", true);
    }

    new DbJob() {
        MapReduceOutput output;

        @Override
        public Object doRun() {
            //                output = col.mapReduce(cmd);

            // if type in inline, then query options like slaveOk is fine
            CommandResult res = null;
            if (type == MapReduceCommand.OutputType.INLINE) {
                res = col.getDB().command(cmdobj, col.getOptions());
                return res;
            }

            res = col.getDB().command(cmdobj);
            res.throwOnError();
            output = new MapReduceOutput(col, cmdobj, res);
            return output;
        }

        @Override
        public void wrapUp(Object res) {
            if (output != null) {
                if (cmd.getOutputType() == OutputType.INLINE) {
                    res = output.results();
                } else {
                    // spawn a find
                    doFind(output.getOutputCollection(), null);
                    res = output.getRaw();
                }
            }
            super.wrapUp(res);
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "MR";
        }

        @Override
        public DBObject getRoot(Object result) {
            return cmdobj;
        }

        @Override
        public ButtonBase getButton() {
            return button;
        }

        @Override
        DBObject getCommand() {
            return cmdobj;
        }

        @Override
        DB getDB() {
            return col.getDB();
        }
    }.addJob();
}

From source file:com.jaspersoft.mongodb.query.MongoDbQueryWrapper.java

License:Open Source License

private void createIterator() throws JRException {
    if (!queryObject.containsField(COLLECTION_NAME_KEY)) {
        throw new JRException("\"" + COLLECTION_NAME_KEY + "\" must be part of the query object");
    }//from  w w w  . j  ava  2  s  . c o m
    DBObject findQueryObject = (DBObject) queryObject.get(FIND_QUERY_KEY);
    if (findQueryObject == null) {
        findQueryObject = new BasicDBObject();
    }
    if (queryObject.containsField(FIND_QUERY_REGEXP_KEY)) {
        DBObject regExpObject = (DBObject) queryObject.get(FIND_QUERY_REGEXP_KEY);
        String value, flags;
        int index;
        for (String key : regExpObject.keySet()) {
            value = (String) regExpObject.get(key);
            if (value.startsWith("/")) {
                value = value.substring(1, value.length());
            } else {
                throw new JRException("Regular expressions must start with: /");
            }
            if (!value.contains("/")) {
                throw new JRException("No ending symbol found: /");
            }
            index = value.lastIndexOf("/");
            flags = null;
            if (index == value.length() - 1) {
                value = value.substring(0, index);
            } else {
                flags = value.substring(index + 1, value.length());
                value = value.substring(0, index);
            }
            findQueryObject.put(key, Pattern.compile((flags != null ? "(?" + flags + ")" : "") + value));
        }
    }

    DBCollection collection = connection.getMongoDatabase()
            .getCollectionFromString((String) queryObject.removeField(COLLECTION_NAME_KEY));
    if (queryObject.containsField(MAP_REDUCE_KEY)) {
        Object value = queryObject.removeField(MAP_REDUCE_KEY);
        if (!(value instanceof DBObject)) {
            logger.error("MapReduce value must be a valid JSON object");
        } else {
            DBObject mapReduceObject = (DBObject) value;
            String map = validateProperty(mapReduceObject, MAP_KEY);
            String reduce = validateProperty(mapReduceObject, REDUCE_KEY);
            Object outObject = mapReduceObject.get(OUT_KEY);
            if (outObject == null) {
                throw new JRException("\"out\" cannot be null");
            }
            String collectionName = null;
            Object outDb = null;
            OutputType outputType = null;
            boolean hasOutputType = false;
            if (logger.isDebugEnabled()) {
                logger.debug("Out object: " + outObject + ". Type: " + outObject.getClass().getName());
            }
            if (outObject instanceof String) {
                collectionName = String.valueOf(outObject);
            } else if (outObject instanceof DBObject) {
                DBObject outDbObject = (DBObject) outObject;
                outDb = outDbObject.removeField(OUT_DB_KEY);
                Iterator<String> keysIterator = outDbObject.keySet().iterator();
                String type = null;
                if (keysIterator.hasNext()) {
                    type = keysIterator.next();
                    collectionName = String.valueOf(outDbObject.get(type));
                } else {
                    throw new JRException("\"out\" object cannot be empty");
                }
                type = type.toUpperCase();
                outputType = OutputType.valueOf(type);
                if (outputType == null) {
                    throw new JRException("Unknow output type: " + type);
                }
                hasOutputType = true;
                if (logger.isDebugEnabled()) {
                    logger.debug("outobject: " + outDbObject);
                    logger.debug("collectionName: " + collectionName);
                    logger.debug("outputType: " + outputType);
                }
            } else {
                throw new JRException("Unsupported type for \"out\": " + outObject.getClass().getName());
            }
            MapReduceCommand mapReduceCommand = new MapReduceCommand(collection, map, reduce, collectionName,
                    hasOutputType ? outputType : OutputType.REPLACE, null);
            if (outDb != null) {
                mapReduceCommand.setOutputDB(String.valueOf(outDb));
            }
            Object finalizeObject = mapReduceObject.removeField(FINALIZE_KEY);
            if (finalizeObject != null) {
                mapReduceCommand.setFinalize(String.valueOf(finalizeObject));
            }
            MapReduceOutput mapReduceOutput = collection.mapReduce(mapReduceCommand);
            DBCollection mapReduceCollection = mapReduceOutput.getOutputCollection();
            if (mapReduceCollection != null) {
                collection = mapReduceCollection;
            }
        }
    }

    iterator = collection.find(findQueryObject, (DBObject) queryObject.get(FIND_FIELDS_KEY));
    if (queryObject.containsField(SORT_KEY)) {
        iterator = iterator.sort((DBObject) queryObject.get(SORT_KEY));
    }
    if (queryObject.containsField(LIMIT_KEY)) {
        Integer value = processInteger(queryObject.get(LIMIT_KEY));
        if (value != null) {
            iterator = iterator.limit(value.intValue());
        }
    }
}

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

License:Open Source License

@SuppressWarnings("deprecation")
@Override/*  w  w  w .  j ava  2 s  .c om*/
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 ");
    }// w w  w .java 2 s  . c o m

    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;
}

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

License:Apache License

private void copyMapReduceOptionsToCommand(Query query, MapReduceOptions mapReduceOptions,
        MapReduceCommand mapReduceCommand) {

    if (query != null) {
        if (query.getSkip() != 0 || query.getFieldsObject() != null) {
            throw new InvalidDataAccessApiUsageException(
                    "Can not use skip or field specification with map reduce operations");
        }/*from w ww  . j  a  v a  2s  .com*/
        if (query.getLimit() > 0 && mapReduceOptions.getLimit() == null) {
            mapReduceCommand.setLimit(query.getLimit());
        }
        if (query.getSortObject() != null) {
            mapReduceCommand.setSort(queryMapper.getMappedObject(query.getSortObject(), null));
        }
    }

    if (mapReduceOptions.getLimit() != null && mapReduceOptions.getLimit().intValue() > 0) {
        mapReduceCommand.setLimit(mapReduceOptions.getLimit());
    }

    if (mapReduceOptions.getJavaScriptMode() != null) {
        mapReduceCommand.setJsMode(true);
    }
    if (!mapReduceOptions.getExtraOptions().isEmpty()) {
        for (Map.Entry<String, Object> entry : mapReduceOptions.getExtraOptions().entrySet()) {
            ReflectiveMapReduceInvoker.addExtraOption(mapReduceCommand, entry.getKey(), entry.getValue());
        }
    }
    if (mapReduceOptions.getFinalizeFunction() != null) {
        mapReduceCommand
                .setFinalize(this.replaceWithResourceIfNecessary(mapReduceOptions.getFinalizeFunction()));
    }
    if (mapReduceOptions.getOutputDatabase() != null) {
        mapReduceCommand.setOutputDB(mapReduceOptions.getOutputDatabase());
    }
    if (!mapReduceOptions.getScopeVariables().isEmpty()) {
        mapReduceCommand.setScope(mapReduceOptions.getScopeVariables());
    }
}