List of usage examples for com.mongodb MapReduceCommand getInput
public String getInput()
From source file:com.impetus.client.mongodb.MongoDBClient.java
License:Apache License
/** * Execute native query.//from w ww . j a v a 2 s . c om * * @param jsonClause * the json clause * @param entityMetadata * the entity metadata * @return the list */ public List executeNativeQuery(String jsonClause, EntityMetadata entityMetadata) { List entities = new ArrayList(); String[] tempArray = jsonClause.split("\\."); String tempClause = tempArray[tempArray.length - 1]; if (tempClause.contains("findOne(") || tempClause.contains("findAndModify(")) { DBObject obj = (BasicDBObject) executeScript(jsonClause); populateEntity(entityMetadata, entities, obj); return entities; } else if (tempClause.contains("find(") || jsonClause.contains("aggregate(")) { jsonClause = jsonClause.concat(".toArray()"); BasicDBList list = (BasicDBList) executeScript(jsonClause); for (Object obj : list) { populateEntity(entityMetadata, entities, (DBObject) obj); } return entities; } else if (tempClause.contains("count(") || tempClause.contains("dataSize(") || tempClause.contains("storageSize(") || tempClause.contains("totalIndexSize(") || tempClause.contains("totalSize(")) { Long count = ((Double) executeScript(jsonClause)).longValue(); entities.add(count); return entities; } else if (tempClause.contains("distinct(")) { BasicDBList list = (BasicDBList) executeScript(jsonClause); for (Object obj : list) { entities.add(obj); } return entities; } else if (jsonClause.contains("mapReduce(")) { final MapReduceCommand command = parseMapReduceCommand(jsonClause); final MapReduceOutput output = mongoDb.getCollection(command.getInput()).mapReduce(command); final BasicDBList list = new BasicDBList(); for (final DBObject item : output.results()) { list.add(item); } return list; } else { BasicDBList list = (BasicDBList) executeScript(jsonClause); for (Object obj : list) { entities.add(obj); } return entities; } }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
License:Apache License
public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass) { String mapFunc = replaceWithResourceIfNecessary(mapFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); DBCollection inputCollection = getCollection(inputCollectionName); MapReduceCommand command = new MapReduceCommand(inputCollection, mapFunc, reduceFunc, mapReduceOptions.getOutputCollection(), mapReduceOptions.getOutputType(), query == null || query.getQueryObject() == null ? null : queryMapper.getMappedObject(query.getQueryObject(), null)); copyMapReduceOptionsToCommand(query, mapReduceOptions, command); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Executing MapReduce on collection [{}], mapFunction [{}], reduceFunction [{}]", command.getInput(), mapFunc, reduceFunc); }/*from ww w. j av a 2 s. co m*/ MapReduceOutput mapReduceOutput = inputCollection.mapReduce(command); if (LOGGER.isDebugEnabled()) { LOGGER.debug("MapReduce command result = [{}]", serializeToJsonSafely(mapReduceOutput.results())); } List<T> mappedResults = new ArrayList<T>(); DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass, inputCollectionName); for (DBObject dbObject : mapReduceOutput.results()) { mappedResults.add(callback.doWith(dbObject)); } return new MapReduceResults<T>(mappedResults, mapReduceOutput); }