List of usage examples for com.mongodb MapReduceCommand setScope
public void setScope(@Nullable final Map<String, Object> scope)
From source file:com.stratio.connector.mongodb.core.engine.metadata.DiscoverMetadataUtils.java
License:Apache License
/** * Discover the existing fields stored in the collection and their data types. * * @param collection/*ww w . j a v a 2s. c o m*/ * the collection * @return the list of fields including the _id */ public static HashMap<String, String> discoverFieldsWithType(DBCollection collection, String sample_probability) { String map = "function() { if(Math.random() <= sample_number) {for (var key in this) {var type = typeof(this[key]); if(type == \"object\"){type = \"string\";};emit(key, type);}} } "; String reduce = "function(key, values) { var result = \"\"; for (var i = 0; i < values.length; i++){ var v = values[i];if(v == \"string\"){result = \"string\"; break;} if(v == \"number\"){result = \"number\"} if(v == \"boolean\" && result == \"number\"){result = \"string\"; break;}if(v == \"number\" && result == \"boolean\"){result = \"string\"; break;} if(v==\"boolean\"){result = \"boolean\"}};return result; }"; MapReduceCommand mapReduceCommand = new MapReduceCommand(collection, map, reduce, null, OutputType.INLINE, null); HashMap<String, Object> scope = new HashMap<>(); // connection scope.put("sample_number", sample_probability); mapReduceCommand.setScope(scope); DBObject getFieldsCommand = mapReduceCommand.toDBObject(); CommandResult command = collection.getDB().command(getFieldsCommand); BasicDBList results = (BasicDBList) command.get("results"); HashMap<String, String> fields = new HashMap<>(); if (results != null) { for (Object object : results) { DBObject bson = (DBObject) object; String nameField = (String) bson.get("_id"); String type = (String) bson.get("value"); fields.put(nameField, type); } } return fields; }
From source file:de.taimos.dvalin.mongo.AbstractMongoDAO.java
License:Apache License
/** * runs a map-reduce-job on the collection. The functions are read from the classpath in the folder mongodb. The systems reads them from * files called <name>.map.js, <name>.reduce.js and optionally <name>.finalize.js. After this the result is converted * using the given {@link MapReduceResultHandler} * * @param <R> the type of the result class * @param name the name of the map-reduce functions * @param query the query to filter the elements used for the map-reduce * @param sort sort query to sort elements before running map-reduce * @param scope the global scope for the JavaScript run * @param conv the converter to convert the result * @return an {@link Iterable} with the result entries * @throws RuntimeException if resources cannot be read */// w w w. j ava 2 s .c om protected final <R> Iterable<R> mapReduce(String name, DBObject query, DBObject sort, Map<String, Object> scope, final MapReduceResultHandler<R> conv) { String map = this.getMRFunction(name, "map"); String reduce = this.getMRFunction(name, "reduce"); MapReduceCommand mrc = new MapReduceCommand(this.collection.getDBCollection(), map, reduce, null, OutputType.INLINE, query); String finalizeFunction = this.getMRFunction(name, "finalize"); if (finalizeFunction != null) { mrc.setFinalize(finalizeFunction); } if (sort != null) { mrc.setSort(sort); } if (scope != null) { mrc.setScope(scope); } MapReduceOutput mr = this.collection.getDBCollection().mapReduce(mrc); return new ConverterIterable<R>(mr.results().iterator(), conv); }
From source file:fr.gouv.vitam.mdbes.MainMapReduce.java
License:Open Source License
protected static void oneShot(MongoDbAccess dbvitam) throws InvalidParseOperationException, InvalidExecOperationException, InstantiationException, IllegalAccessException { // Requesting if (map == null) { map = "function() {" + "flattened = serializeDocFiltered(this, MAXDEPTH, FILTER);" + "for (var key in flattened) {" + "var value = flattened[key];" + "var valueType = varietyTypeOf(value);" + "var finalvalue = { types : [ valueType ], occurences : 1};" // + "var finalvalue = { occurences : 1};" + "emit(key, finalvalue); } }"; }/*ww w . j a va 2s. c o m*/ if (reduce == null) { reduce = "function(key,values) {" + "var typeset = new Set();" + "var occur = 0;" + "for (var idx = 0; idx < values.length; idx++) {" + "occur += values[idx].occurences;" + "typeset.add(values[idx].types);" + "} return { types : typeset.asArray(), occurences : occur }; }"; // + "} return { occurences : occur }; }"; } if (output == null) { output = "AttributeUsage"; } MapReduceCommand mapReduceCommand = new MapReduceCommand(dbvitam.daips.collection, map, reduce, output, OutputType.REDUCE, new BasicDBObject()); if (options != null) { String[] optionsArray = options.split(","); Map<String, Object> mapScope = new HashMap<String, Object>(); for (String string : optionsArray) { String[] kv = string.split("="); mapScope.put(kv[0], getParsedString(kv[1])); } mapReduceCommand.setScope(mapScope); } else { Map<String, Object> mapScope = new HashMap<String, Object>(); mapScope.put("MAXDEPTH", 5); mapScope.put("FILTER", "_dds"); mapReduceCommand.setScope(mapScope); } mapReduceCommand.addExtraOption("nonAtomic", true); mapReduceCommand.addExtraOption("jsMode", true); MapReduceOutput output = dbvitam.daips.collection.mapReduce(mapReduceCommand); System.out.println("Duration: " + output.getDuration() + " Saved into: " + output.getCollectionName() + " Input: " + output.getInputCount() + " Emit: " + output.getEmitCount() + " Output: " + output.getOutputCount() + "\nCmd: " + output.getCommand()); Iterable<DBObject> iterable = output.results(); for (DBObject dbObject : iterable) { System.out.println(dbObject.toString()); } }
From source file:org.aw20.mongoworkbench.command.MapReduceMongoCommand.java
License:Open Source License
@SuppressWarnings("deprecation") @Override// w w w . j a v a 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 "); }//from w ww . ja va 2s .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; }
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 www. j a va 2 s . c o m*/ 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()); } }