List of usage examples for com.mongodb MapReduceOutput results
@SuppressWarnings("unchecked") public Iterable<DBObject> results()
From source file:govt_import_export.export.java
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed // TODO add your handling code here: JLabel p = new JLabel(); MongoClient mongo = null;/*from www . j a v a 2 s . c om*/ try { mongo = new MongoClient("localhost", 27017); //get database } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } DB db = mongo.getDB("AUTOMOBILEXPO"); DBCollection table = db.getCollection(manufac); String map = "function () {" + "emit(this.model,this.Units);" + "}"; String reduce = "function (key, values) { " + "return Array.sum(values)}"; MapReduceCommand cmd = new MapReduceCommand(table, map, reduce, null, MapReduceCommand.OutputType.INLINE, null); MapReduceOutput out = table.mapReduce(cmd); for (DBObject o : out.results()) { System.out.println(o.toString()); } }
From source file:net.ymate.platform.persistence.mongodb.support.MongoDBHelper.java
License:Apache License
public Iterable<DBObject> mapReduce(String collectionName, String map, String reduce, DBObject query) throws OperatorException { MapReduceOutput _output = getCollection(collectionName).mapReduce(map, reduce, null, OutputType.INLINE, query);/*w w w . j av a 2s . c om*/ CommandResult _result = _output.getCommandResult(); if (!_result.ok()) { throw new OperatorException(_result.getErrorMessage()); } return _output.results(); }
From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java
License:Open Source License
@SuppressWarnings("deprecation") @Override//from ww w . j a v a2 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.basex.modules.MongoDB.java
License:BSD License
/** * Mongodb Mapreduce function with 3 parameters, Map, reduce and query Option. * @param handler Database Handler.//www . j a va 2s.com * @param col Collection name * @param map Map method * @param reduce Reduce Method * @param query Selection options. * @return Items. * @throws Exception */ public Item mapreduce(final Str handler, final Str col, final Str map, final Str reduce, final Item finalalize, final Item query, final Map options) throws Exception { final DB db = getDbHandler(handler); if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty in Mapreduce"); } final DBObject q = query != null ? getDbObjectFromStr(query) : null; final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; OutputType op = MapReduceCommand.OutputType.INLINE; if (options != null) { for (Item k : options.keys()) { String key = (String) k.toJava(); if (key.equals("outputs")) { out = (String) options.get(k, null).toJava(); } if (key.equals("outputype")) { outType = (String) options.get(k, null).toJava(); } } if (out != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map.toJava(), reduce.toJava(), out, op, q); if (finalalize != null) { cmd.setFinalize((String) finalalize.toJava()); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.MongoDB.java
License:BSD License
/** * Mapreduce all functions in xquery's Map like :{"map":"function(){..}" * , "reduce":"function(){}"}./*from w w w. ja v a 2s .co m*/ * @param handler * @param col collection name * @param options all options of Mapreduce including "map" in key. * @return * @throws Exception */ public Item mapreduce(final Str handler, final Str col, final Map options) throws Exception { if (options == null) { throw MongoDBErrors.generalExceptionError("Map optoins are empty"); } final DB db = getDbHandler(handler); final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; String map = null; String reduce = null; DBObject query = null; DBObject sort = null; int limit = 0; String finalalize = null; OutputType op = MapReduceCommand.OutputType.INLINE; for (Item k : options.keys()) { String key = (String) k.toJava(); Value val = options.get(k, null); String value = (String) val.toJava(); if (key.toLowerCase().equals("map")) { map = (String) value; } else if (key.toLowerCase().equals("reduce")) { reduce = value; } else if (key.toLowerCase().equals("outputs")) { out = value; } else if (key.toLowerCase().equals("outputype")) { outType = value; } else if (key.toLowerCase().equals("limit")) { if (val.type().instanceOf(SeqType.ITR_OM)) { long l = ((Item) val).itr(null); limit = (int) l; } else { throw MongoDBErrors.generalExceptionError(" Expected integer Value"); } } else if (key.toLowerCase().equals(SORT)) { sort = getDbObjectFromStr(Str.get(value)); } else if (key.toLowerCase().equals(QUERY)) { query = getDbObjectFromStr(Str.get(value)); } else if (key.toLowerCase().equals(FINALIZE)) { finalalize = value; } } if (out != null && outType != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } else if (out != null) { op = MapReduceCommand.OutputType.REPLACE; } if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty"); } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, out, op, query); if (finalalize != null) { cmd.setFinalize(finalalize); } if (limit != 0) { cmd.setLimit(limit); } if (sort != null) { cmd.setSort(sort); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Mongodb Mapreduce function with 5 parameters, Map, reduce and query Option. * @param handler database handler//from w w w.ja v a2 s . c om * @param col collection name * @param map Map method * @param reduce Reduce Method * @param finalalize mongodb finalize parameter * @param query Selection options. * @param options additional options * @return Item * @throws Exception exception */ public Item mapreduce(final Str handler, final Str col, final Str map, final Str reduce, final Item finalalize, final Item query, final Map options) throws Exception { final DB db = getDbHandler(handler); if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty in Mapreduce"); } final DBObject q = query != null ? getDbObjectFromItem(query) : null; final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; OutputType op = MapReduceCommand.OutputType.INLINE; if (options != null) { for (Item k : options.keys()) { String key = (String) k.toJava(); if (key.equals("outputs")) { out = (String) options.get(k, null).toJava(); } if (key.equals("outputype")) { outType = (String) options.get(k, null).toJava(); } } if (out != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map.toJava(), reduce.toJava(), out, op, q); if (finalalize != null) { cmd.setFinalize((String) finalalize.toJava()); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.basex.modules.nosql.MongoDB.java
License:BSD License
/** * Mapreduce all functions in xquery's Map like :{"map":"function(){..}" * , "reduce":"function(){}"}./*from ww w . j a v a 2s. co m*/ * @param handler database handler * @param col collection name * @param options all options of Mapreduce including "map" in key. * @return Item * @throws Exception exception */ public Item mapreduce(final Str handler, final Str col, final Map options) throws Exception { if (options == null) { throw MongoDBErrors.generalExceptionError("Map optoins are empty"); } final DB db = getDbHandler(handler); final DBCollection collection = db.getCollection(itemToString(col)); String out = null; String outType = null; String map = null; String reduce = null; DBObject query = null; DBObject sort = null; int limit = 0; String finalalize = null; OutputType op = MapReduceCommand.OutputType.INLINE; for (Item k : options.keys()) { String key = (String) k.toJava(); Value val = options.get(k, null); String value = (String) val.toJava(); if (key.toLowerCase().equals(MAP)) { map = value; } else if (key.toLowerCase().equals(REDUCE)) { reduce = value; } else if (key.toLowerCase().equals(OUTPUTS)) { out = value; } else if (key.toLowerCase().equals(OUTPUTTYPE)) { outType = value; } else if (key.toLowerCase().equals(LIMIT)) { if (val.seqType().instanceOf(SeqType.ITR_OM)) { long l = ((Item) val).itr(null); limit = (int) l; } else { throw MongoDBErrors.generalExceptionError(" Expected integer Value"); } } else if (key.toLowerCase().equals(SORT)) { sort = getDbObjectFromItem(Str.get(value)); } else if (key.toLowerCase().equals(QUERY)) { query = getDbObjectFromItem(Str.get(value)); } else if (key.toLowerCase().equals(FINALIZE)) { finalalize = value; } } if (out != null && outType != null) { if (outType.toUpperCase().equals("REPLACE")) { op = MapReduceCommand.OutputType.REPLACE; } else if (outType.toUpperCase().equals("MERGE")) { op = MapReduceCommand.OutputType.MERGE; } else if (outType.toUpperCase().equals("REDUCE")) { op = MapReduceCommand.OutputType.REDUCE; } } else if (out != null) { op = MapReduceCommand.OutputType.REPLACE; } if (map == null) { throw MongoDBErrors.generalExceptionError("Map function cannot be empty"); } db.requestStart(); try { MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce, out, op, query); if (finalalize != null) { cmd.setFinalize(finalalize); } if (limit != 0) { cmd.setLimit(limit); } if (sort != null) { cmd.setSort(sort); } final MapReduceOutput outcmd = collection.mapReduce(cmd); return returnResult(handler, Str.get(JSON.serialize(outcmd.results()))); } catch (MongoException e) { throw MongoDBErrors.generalExceptionError(e); } finally { db.requestDone(); } }
From source file:org.eclipse.birt.data.oda.mongodb.internal.impl.MDbMetaData.java
License:Open Source License
/** * Returns all fields' name and corresponding metadata found in the specified collection. * @param collectionName name of MongoDB collection (i.e. table) * @param searchLimit maximum number of documents, i.e. rows to search for available fields; * a zero or negative value would adopt the default limit * @param runtimeProps an instance of QueryProperties containing the data set runtime property values; * may be null to apply all default values in finding the available fields metadata * @return the DocumentsMetaData object that contains the list of available field names and * corresponding metadata; /* w w w . j a v a2s . c o m*/ * an empty list is returned if no available fields are found, or * if the specified collection does not exist * @throws OdaException */ public DocumentsMetaData getAvailableFields(String collectionName, int searchLimit, QueryProperties runtimeProps) throws OdaException { DBCollection collection = getCollection(collectionName); if (collection == null && !runtimeProps.hasRunCommand()) { if (runtimeProps.getOperationType() == CommandOperationType.RUN_DB_COMMAND && runtimeProps.getOperationExpression().isEmpty()) throw new OdaException(Messages.bind(Messages.mDbMetaData_missingCmdExprText, runtimeProps.getOperationType().displayName())); else throw new OdaException(Messages.bind(Messages.mDbMetaData_invalidCollectionName, collectionName)); } if (searchLimit <= 0) // no limit specified, applies meta data design-time default searchLimit = DEFAULT_META_DATA_SEARCH_LIMIT; // handle optional command operation if (runtimeProps.hasValidCommandOperation()) { QueryModel.validateCommandSyntax(runtimeProps.getOperationType(), runtimeProps.getOperationExpression()); Iterable<DBObject> commandResults = null; if (runtimeProps.hasAggregateCommand()) commandResults = MDbOperation.callAggregateCmd(collection, runtimeProps); else if (runtimeProps.hasMapReduceCommand()) { MapReduceOutput mapReduceOut = MDbOperation.callMapReduceCmd(collection, runtimeProps); commandResults = mapReduceOut.results(); // skip running $query on output collection in discovering metadata } else if (runtimeProps.hasRunCommand()) commandResults = MDbOperation.callDBCommand(m_connectedDB, runtimeProps); if (commandResults != null) return getMetaData(commandResults, searchLimit); return sm_emptyFields; } // run search query operation by default DBCursor rowsCursor = collection.find(); if (searchLimit > 0) rowsCursor.limit(searchLimit); QueryProperties mdCursorProps = runtimeProps != null ? runtimeProps : QueryProperties.defaultValues(); MDbOperation.applyPropertiesToCursor(rowsCursor, mdCursorProps, false); return getMetaData(rowsCursor); }
From source file:org.exist.mongodb.xquery.mongodb.collection.MapReduce.java
License:Open Source License
@Override public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { try {/*from www . j av a2 s . co m*/ // Verify clientid and get client String mongodbClientId = args[0].itemAt(0).getStringValue(); MongodbClientStore.getInstance().validate(mongodbClientId); MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId); // Get parameters String dbname = args[1].itemAt(0).getStringValue(); String collection = args[2].itemAt(0).getStringValue(); String map = args[3].itemAt(0).getStringValue(); String reduce = args[4].itemAt(0).getStringValue(); // output-target can have value null String outputTarget = args[5].isEmpty() ? null : args[5].itemAt(0).getStringValue(); OutputType outputType = args[6].isEmpty() ? OutputType.INLINE : OutputType.valueOf(args[6].itemAt(0).getStringValue().toUpperCase(Locale.US)); DBObject query = (BasicDBObject) JSON.parse(args[7].itemAt(0).getStringValue()); // Get collection in database DB db = client.getDB(dbname); DBCollection dbcol = db.getCollection(collection); // Execute query MapReduceOutput output = dbcol.mapReduce(map, reduce, outputTarget, outputType, query); // Parse results Sequence retVal = new ValueSequence(); for (DBObject result : output.results()) { retVal.add(new StringValue(result.toString())); } return retVal; } catch (JSONParseException ex) { LOG.error(ex.getMessage()); throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage()); } catch (XPathException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, ex.getMessage(), ex); } catch (MongoCommandException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0005, ex.getMessage()); } catch (MongoException ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage()); } catch (Throwable ex) { LOG.error(ex.getMessage(), ex); throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage()); } }
From source file:org.iternine.jeppetto.dao.mongodb.projections.MapReduceCommand.java
License:Apache License
@Override public final Object singleResult(DBCollection dbCollection) { MapReduceOutput output = dbCollection.mapReduce(createMapFunction(), createReduceFunction(), null, query); DBCursor cursor = output.results(); try {//from ww w. j a v a 2s .c o m return transformToValue(cursor); } finally { output.drop(); } }