Example usage for com.mongodb MapReduceCommand MapReduceCommand

List of usage examples for com.mongodb MapReduceCommand MapReduceCommand

Introduction

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

Prototype

public MapReduceCommand(final DBCollection inputCollection, final String map, final String reduce,
        @Nullable final String outputCollection, final OutputType type, final DBObject query) 

Source Link

Document

Represents the command for a map reduce operation Runs the command in REPLACE output type to a named collection

Usage

From source file:com.Aleksandar.Zoric.MongoMain.java

public String mapReduceFunction() {
    MongoClient mongoClient = new MongoClient(new ServerAddress("localhost", 27017));
    DB db = mongoClient.getDB("amarokforumdb");
    DBCollection collection = db.getCollection("comments");
    long count = db.getCollection("comments").count();

    System.out.println("Current amount of documents: " + count);

    String map = "function() { " + "var category; " + "var numOfDocuments = " + count + ";"
            + "for(i = 0; i < numOfDocuments; i++){ " + "if (numOfDocuments <= 100) {"
            + "category = 'New Comments'; }" + "else if(numOfDocuments > 100){"
            + "category = 'Old Comments'; }}" + "emit(category,1);};";

    String reduce = "function(key, values) { " + "var sum = 0; " + "values.forEach(function(doc) { "
            + "sum += 1; " + "}); " + "return {comments: sum};} ";

    MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, null,
            MapReduceCommand.OutputType.INLINE, null);

    MapReduceOutput out = collection.mapReduce(cmd);

    System.out.println("Mapreduce results");

    String result = null;/*from  w  w w.j a  va  2  s.  co  m*/

    for (DBObject o : out.results()) {
        result += o;

    }
    return result;
}

From source file:com.dilmus.dilshad.scabi.deprecated.DDBOld.java

License:Open Source License

public ArrayList<String> fieldNames(DTableOld table) throws DScabiException {
    if (null == table) {
        throw new DScabiException("Table is null", "DBD.FNS.1");
    }//from   ww  w.  java2 s.  co  m
    log.debug("fieldNamesUsingFindOne() table.count() is {}", table.count());
    if (table.count() <= 0) {
        return null;
    }
    String map = "function() { for (var key in this) { emit(key, null); } }";
    String reduce = "function(key, stuff) { return null; }";

    MapReduceCommand cmd = new MapReduceCommand(table.getCollection(), map, reduce, null,
            MapReduceCommand.OutputType.INLINE, null);
    MapReduceOutput out = table.getCollection().mapReduce(cmd);
    //if 4th param output collection name is used above, String s = out.getOutputCollection().distinct("_id").toString();
    //if 4th param output collection name is used above, System.out.println("out.getOutputCollection().distinct " + s);
    ArrayList<String> fieldNames = new ArrayList<String>();
    for (DBObject o : out.results()) {
        log.debug("fieldNames() Key, value is : {}", o.toString());
        log.debug("fieldNames() Key name is : {}", o.get("_id").toString());
        if (false == o.get("_id").toString().equals("_id"))
            fieldNames.add(o.get("_id").toString());
    }
    return fieldNames;

}

From source file:com.dilmus.dilshad.scabi.deprecated.DTableOld.java

License:Open Source License

public ArrayList<String> fieldNames() throws DScabiException {
    if (null == m_tableName) {
        throw new DScabiException("Table name is null", "DBT.FNS.1");
    }//from w ww.j  a v a  2 s .c om
    if (null == m_table) {
        throw new DScabiException("Table is null", "DBT.FNS.2");
    }
    log.debug("fieldNames() firstTime is {}", m_firstTime);
    log.debug("fieldNames() table.count() is {}", m_table.count());
    if (m_table.count() <= 0) {
        return null;
    }
    if (m_firstTime) {
        String map = "function() { for (var key in this) { emit(key, null); } }";
        String reduce = "function(key, s) { return null; }";

        MapReduceCommand cmd = new MapReduceCommand(m_table, map, reduce, null,
                MapReduceCommand.OutputType.INLINE, null);
        MapReduceOutput out = m_table.mapReduce(cmd);
        //if 4th param output collection name is used above, String s = out.getOutputCollection().distinct("_id").toString();
        //if 4th param output collection name is used above, System.out.println("out.getOutputCollection().distinct " + s);
        m_fieldNames = new ArrayList<String>();
        for (DBObject o : out.results()) {
            log.debug("fieldNames() Key, value is : {}", o.toString());
            log.debug("fieldNames() Key name is : {}", o.get("_id").toString());
            if (false == o.get("_id").toString().equals("_id"))
                m_fieldNames.add(o.get("_id").toString());
        }
        m_firstTime = false;
        return m_fieldNames;
    }
    return m_fieldNames;
}

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 .java2s  .  co  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.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 a  v  a  2  s .c o  m*/
    ////         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:com.hangum.tadpole.mongodb.core.editors.mapreduce.MapReduceEditor.java

License:Open Source License

/**
 * execute map reduce//w w w.j  a  va2  s  . c  o  m
 */
private void executeMapReduce() throws Exception {
    String strMap = textMap.getText();
    String strReduce = textReduce.getText();
    String strFinilize = textFinalize.getText();
    String strOutputTarget = textOutputTarget.getText();
    MapReduceCommand.OutputType outputType = (MapReduceCommand.OutputType) comboOutputType
            .getData(comboOutputType.getText());

    DBObject dbQuery = null;
    if (!"".equals(textQuery.getText()))
        dbQuery = (DBObject) JSON.parse(textQuery.getText());

    DBObject dbSort = null;
    if (!"".equals(textSort.getText()))
        dbSort = (DBObject) JSON.parse(textSort.getText());

    //  .
    DBCollection dbCol = MongoDBQuery.findCollection(userDB, initColName);
    MapReduceCommand mrCmd = new MapReduceCommand(dbCol, strMap, strReduce, strOutputTarget, outputType,
            dbQuery);
    if (!"".equals(strFinilize))
        mrCmd.setFinalize(strFinilize);
    if (dbSort != null)
        mrCmd.setSort(dbSort);
    if (getLimit() > 0)
        mrCmd.setLimit(getLimit());
    if (btnJsMode.getSelection())
        mrCmd.addExtraOption("jsMode", true);

    final BasicDBObject searchObj = (BasicDBObject) mrCmd.toDBObject();
    if (btnSharded.getSelection())
        ((BasicDBObject) searchObj.get("out")).put("sharded", true);
    if (btnNoneAtomic.getSelection())
        ((BasicDBObject) searchObj.get("out")).put("nonAtomic", true);

    goMapReduce(dbCol, searchObj, outputType);
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

/**
 * Parses the map reduce command.// w ww  .ja  v  a 2s . com
 * 
 * @param jsonClause
 *            the json clause
 * @return the map reduce command
 */
private MapReduceCommand parseMapReduceCommand(String jsonClause) {
    String collectionName = jsonClause.replaceFirst("(?ms).*?\\.\\s*(.+?)\\s*\\.\\s*mapReduce\\s*\\(.*", "$1");
    if (collectionName.contains("getCollection")) {
        collectionName = collectionName.replaceFirst(".*getCollection\\s*\\(\\s*(['\"])([^'\"]+)\\1\\s*\\).*",
                "$2");
    }

    DBCollection collection = mongoDb.getCollection(collectionName);

    String body = jsonClause.replaceFirst("^(?ms).*?mapReduce\\s*\\(\\s*(.*)\\s*\\)\\s*;?\\s*$", "$1");
    String mapFunction = findCommaSeparatedArgument(body, 0).trim();
    String reduceFunction = findCommaSeparatedArgument(body, 1).trim();

    String query = findCommaSeparatedArgument(body, 2).trim();
    DBObject parameters = (DBObject) JSON.parse(query);
    DBObject mongoQuery;
    if (parameters.containsField("query")) {
        mongoQuery = (DBObject) parameters.get("query");
    } else {
        mongoQuery = new BasicDBObject();
    }

    return new MapReduceCommand(collection, mapFunction, reduceFunction, null,
            MapReduceCommand.OutputType.INLINE, mongoQuery);
}

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   ww w .j a v  a 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:com.machinelinking.storage.mongodb.MongoJSONStorageConnection.java

License:Apache License

@Override
public JsonNode processMapReduce(DBObject query, String map, String reduce, int limit) {
    final MapReduceCommand command = new MapReduceCommand(this.collection, map, reduce, null,
            MapReduceCommand.OutputType.INLINE, query);
    command.setLimit(limit);/*from w w w. ja  v  a2  s .  c om*/
    final MapReduceOutput out = this.collection.mapReduce(command);
    final DBObject results = (DBObject) out.getCommandResult().get("results");
    return MongoUtils.convertToJsonNode(results);
}

From source file:com.petpet.c3po.dao.mongo.MongoPersistenceLayer.java

License:Apache License

public List<BasicDBObject> mapReduceRaw(String map, String reduce, Filter filter) {
    long start = System.currentTimeMillis();
    DBObject query = this.getCachedFilter(filter);
    DBCollection elmnts = getCollection(Element.class);
    MapReduceCommand cmd = new MapReduceCommand(elmnts, map, reduce, null, INLINE, query);

    MapReduceOutput output = elmnts.mapReduce(cmd);
    Iterator<DBObject> iterator = output.results().iterator();
    List<BasicDBObject> results = new ArrayList<BasicDBObject>();
    while (iterator.hasNext()) {
        results.add((BasicDBObject) iterator.next());

    }/*from   ww  w. j  a v  a 2  s . co  m*/
    //List<BasicDBObject> results = ArrayList<BasicDBObject> () ( output.results());getCommandResult().get( "results" );
    long end = System.currentTimeMillis();
    LOG.debug("The map-reduce job took {} seconds", (end - start) / 1000);
    return results;

}