List of usage examples for com.mongodb MapReduceCommand toDBObject
public DBObject toDBObject()
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.java 2s . 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.hangum.tadpole.mongodb.core.editors.mapreduce.MapReduceEditor.java
License:Open Source License
/** * execute map reduce/*from w w w. j a va2 s. co 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.stratio.connector.mongodb.core.engine.metadata.DiscoverMetadataUtils.java
License:Apache License
/** * Discover the existing fields stored in the collection. * * @param collection/*from www .ja v a 2 s. co m*/ * the collection * @return the list of fields including the _id */ public static List<String> discoverField(DBCollection collection) { String map = "function() { for (var field in this) { emit(field, null); }}"; String reduce = "function(field, stuff) { return null; }"; MapReduceCommand mapReduceCommand = new MapReduceCommand(collection, map, reduce, null, OutputType.INLINE, null); DBObject getFieldsCommand = mapReduceCommand.toDBObject(); CommandResult command = collection.getDB().command(getFieldsCommand); BasicDBList results = (BasicDBList) command.get("results"); Set<String> fields = new HashSet<>(); if (results != null) { for (Object object : results) { DBObject bson = (DBObject) object; fields.add((String) bson.get("_id")); } } return new ArrayList<String>(fields); }
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//from w ww. j a v a 2 s. 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; }