Example usage for org.springframework.data.mongodb.core.mapreduce MapReduceOptions getOutputType

List of usage examples for org.springframework.data.mongodb.core.mapreduce MapReduceOptions getOutputType

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.mapreduce MapReduceOptions getOutputType.

Prototype

public MapReduceCommand.OutputType getOutputType() 

Source Link

Usage

From source file:org.springframework.data.mongodb.core.MongoTemplate.java

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(), null);

    DBObject commandObject = copyQuery(query, copyMapReduceOptions(mapReduceOptions, command));

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Executing MapReduce on collection [" + command.getInput() + "], mapFunction [" + mapFunc
                + "], reduceFunction [" + reduceFunc + "]");
    }/* w ww .j  a  v a 2s  .  c  o m*/

    CommandResult commandResult = command.getOutputType() == MapReduceCommand.OutputType.INLINE
            ? executeCommand(commandObject, getDb().getOptions())
            : executeCommand(commandObject);
    handleCommandError(commandResult, commandObject);

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(String.format("MapReduce command result = [%s]", serializeToJsonSafely(commandObject)));
    }

    MapReduceOutput mapReduceOutput = new MapReduceOutput(inputCollection, commandObject, commandResult);
    List<T> mappedResults = new ArrayList<T>();
    DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
    for (DBObject dbObject : mapReduceOutput.results()) {
        mappedResults.add(callback.doWith(dbObject));
    }

    MapReduceResults<T> mapReduceResult = new MapReduceResults<T>(mappedResults, commandResult);
    return mapReduceResult;
}