Search in sources :

Example 1 with MapReduceStatistics

use of com.mongodb.operation.MapReduceStatistics in project mongo-java-driver by mongodb.

the class DBCollection method mapReduce.

/**
     * Allows you to run map-reduce aggregation operations over a collection.
     *
     * @param command specifies the details of the Map Reduce operation to perform
     * @return a MapReduceOutput containing the results of the map reduce operation
     * @mongodb.driver.manual core/map-reduce/ Map-Reduce
     */
public MapReduceOutput mapReduce(final MapReduceCommand command) {
    ReadPreference readPreference = command.getReadPreference() == null ? getReadPreference() : command.getReadPreference();
    if (command.getOutputType() == MapReduceCommand.OutputType.INLINE) {
        MapReduceWithInlineResultsOperation<DBObject> operation = new MapReduceWithInlineResultsOperation<DBObject>(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), getDefaultDBObjectCodec()).readConcern(getReadConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(command.getJsMode() == null ? false : command.getJsMode()).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).collation(command.getCollation());
        if (command.getScope() != null) {
            operation.scope(wrap(new BasicDBObject(command.getScope())));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        MapReduceBatchCursor<DBObject> executionResult = executor.execute(operation, readPreference);
        return new MapReduceOutput(command.toDBObject(), executionResult);
    } else {
        String action;
        switch(command.getOutputType()) {
            case REPLACE:
                action = "replace";
                break;
            case MERGE:
                action = "merge";
                break;
            case REDUCE:
                action = "reduce";
                break;
            default:
                throw new IllegalArgumentException("Unexpected output type");
        }
        MapReduceToCollectionOperation operation = new MapReduceToCollectionOperation(getNamespace(), new BsonJavaScript(command.getMap()), new BsonJavaScript(command.getReduce()), command.getOutputTarget(), getWriteConcern()).filter(wrapAllowNull(command.getQuery())).limit(command.getLimit()).maxTime(command.getMaxTime(MILLISECONDS), MILLISECONDS).jsMode(command.getJsMode() == null ? false : command.getJsMode()).sort(wrapAllowNull(command.getSort())).verbose(command.isVerbose()).action(action).databaseName(command.getOutputDB()).bypassDocumentValidation(command.getBypassDocumentValidation()).collation(command.getCollation());
        if (command.getScope() != null) {
            operation.scope(wrap(new BasicDBObject(command.getScope())));
        }
        if (command.getFinalize() != null) {
            operation.finalizeFunction(new BsonJavaScript(command.getFinalize()));
        }
        try {
            MapReduceStatistics mapReduceStatistics = executor.execute(operation);
            DBCollection mapReduceOutputCollection = getMapReduceOutputCollection(command);
            DBCursor executionResult = mapReduceOutputCollection.find();
            return new MapReduceOutput(command.toDBObject(), executionResult, mapReduceStatistics, mapReduceOutputCollection);
        } catch (MongoWriteConcernException e) {
            throw createWriteConcernException(e);
        }
    }
}
Also used : MapReduceStatistics(com.mongodb.operation.MapReduceStatistics) MapReduceWithInlineResultsOperation(com.mongodb.operation.MapReduceWithInlineResultsOperation) BsonString(org.bson.BsonString) BsonJavaScript(org.bson.BsonJavaScript) MapReduceToCollectionOperation(com.mongodb.operation.MapReduceToCollectionOperation)

Aggregations

MapReduceStatistics (com.mongodb.operation.MapReduceStatistics)1 MapReduceToCollectionOperation (com.mongodb.operation.MapReduceToCollectionOperation)1 MapReduceWithInlineResultsOperation (com.mongodb.operation.MapReduceWithInlineResultsOperation)1 BsonJavaScript (org.bson.BsonJavaScript)1 BsonString (org.bson.BsonString)1