use of com.mongodb.operation.MapReduceToCollectionOperation in project mongo-java-driver by mongodb.
the class MapReduceIterableImpl method execute.
MongoIterable<TResult> execute() {
if (inline) {
MapReduceWithInlineResultsOperation<TResult> operation = new MapReduceWithInlineResultsOperation<TResult>(namespace, new BsonJavaScript(mapFunction), new BsonJavaScript(reduceFunction), codecRegistry.get(resultClass)).filter(toBsonDocument(filter)).limit(limit).maxTime(maxTimeMS, MILLISECONDS).jsMode(jsMode).scope(toBsonDocument(scope)).sort(toBsonDocument(sort)).verbose(verbose).readConcern(readConcern).collation(collation);
if (finalizeFunction != null) {
operation.finalizeFunction(new BsonJavaScript(finalizeFunction));
}
return new OperationIterable<TResult>(operation, readPreference, executor);
} else {
MapReduceToCollectionOperation operation = createMapReduceToCollectionOperation();
String dbName = databaseName != null ? databaseName : namespace.getDatabaseName();
MongoIterable<TResult> delegated = new FindIterableImpl<TDocument, TResult>(new MongoNamespace(dbName, collectionName), documentClass, resultClass, codecRegistry, primary(), readConcern, executor, new BsonDocument(), new FindOptions().collation(collation)).batchSize(batchSize);
return new AwaitingWriteOperationIterable<TResult, MapReduceStatistics>(operation, executor, delegated);
}
}
use of com.mongodb.operation.MapReduceToCollectionOperation 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);
}
}
}
Aggregations