List of usage examples for com.mongodb MapReduceOutput getOutputCollection
public DBCollection getOutputCollection()
From source file:com.bugull.mongo.AdvancedDao.java
License:Apache License
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, DBObject query) throws MapReduceException { MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query); CommandResult cr = output.getCommandResult(); if (!cr.ok()) { throw new MapReduceException(cr.getErrorMessage()); }// www . ja v a 2s . c o m DBCollection c = output.getOutputCollection(); DBCursor cursor = null; if (orderBy != null) { cursor = c.find().sort(MapperUtil.getSort(orderBy)); } else { cursor = c.find(); } List<DBObject> list = new ArrayList<DBObject>(); for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) { list.add(it.next()); } return list; }
From source file:com.bugull.mongo.AdvancedDao.java
License:Apache License
private synchronized Iterable<DBObject> mapReduce(String map, String reduce, String outputTarget, MapReduceCommand.OutputType outputType, String orderBy, int pageNum, int pageSize, DBObject query) throws MapReduceException { MapReduceOutput output = coll.mapReduce(map, reduce, outputTarget, outputType, query); CommandResult cr = output.getCommandResult(); if (!cr.ok()) { throw new MapReduceException(cr.getErrorMessage()); }/*from w w w.j ava 2s . com*/ DBCollection c = output.getOutputCollection(); DBCursor cursor = null; if (orderBy != null) { cursor = c.find().sort(MapperUtil.getSort(orderBy)).skip((pageNum - 1) * pageSize).limit(pageSize); } else { cursor = c.find().skip((pageNum - 1) * pageSize).limit(pageSize); } List<DBObject> list = new ArrayList<DBObject>(); for (Iterator<DBObject> it = cursor.iterator(); it.hasNext();) { list.add(it.next()); } return list; }
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"); }// w w w . j ava2s .c om 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:implementations.mongoDB.java
License:GNU General Public License
public void searchDB(String keyword) { ArrayList<String> res = new ArrayList<String>(); String map_index = "function() {" + "var words = this.artValue.split(' ');" + "var keyword = \"" + keyword + "\";" + "for ( var i=0; i<words.length; i++ ) {" + "if(words[i].toLowerCase() == keyword.toLowerCase() ){" + "emit(words[i], { docs: [this._id] });" + "}}}"; String reduce_index = "function(key, values) {" + "var docs = [];" + "values.forEach ( function(val) { docs = docs.concat(val.docs); });" + "return { docs: docs };}"; String map_relevance = "function() {" + "for ( var i=0; i< this.value.docs.length; i++ ) {" + "emit(this.value.docs[i], { count: 1 });}}"; String reduce_relevance = "function(key, values) {" + "var sum = 0;" + "values.forEach ( function(val) { sum += val.count; });" + "return { count: sum };}"; //First MapReduce phase MapReduceOutput tempRes = dbCol.mapReduce(map_index, reduce_index, "tempRes1", null); DBCollection outCol = tempRes.getOutputCollection(); //Second MapReduce phase MapReduceOutput tempRes2 = outCol.mapReduce(map_relevance, reduce_relevance, "tempRes2", null); // DBCollection outCol2 = tempRes2.getOutputCollection(); // DBCursor cur = outCol2.find(); // while(cur.hasNext()){ // res.add(cur.next().toString()); // }//from www. j a va2s.com // System.out.println(res); }
From source file:net.ymate.platform.persistence.mongodb.support.MongoDBHelper.java
License:Apache License
public Iterable<DBObject> mapReduce(String collectionName, String map, String reduce, String outputTarget, OutputType type, OrderBy order, int pageNumber, int pageSize, DBObject query) throws OperatorException { MapReduceOutput _output = getCollection(collectionName).mapReduce(map, reduce, outputTarget, type, query); CommandResult _result = _output.getCommandResult(); if (!_result.ok()) { throw new OperatorException(_result.getErrorMessage()); }/*from ww w.j a va2 s .com*/ DBCollection _collection = _output.getOutputCollection(); DBCursor _cursor = null; if (order != null) { _cursor = _collection.find().sort(order.toDBObject()); } else { _cursor = _collection.find(); } if (pageNumber > 0 && pageSize > 0) { _cursor.skip((pageNumber - 1) * pageSize).limit(pageSize); } List<DBObject> _results = new ArrayList<DBObject>(); for (Iterator<DBObject> _it = _cursor.iterator(); _it.hasNext();) { _results.add(_it.next()); } return _results; }
From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java
License:Open Source License
@SuppressWarnings("deprecation") @Override// w ww.j av a 2 s . co 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()); }