Example usage for com.mongodb MapReduceCommand MapReduceCommand

List of usage examples for com.mongodb MapReduceCommand MapReduceCommand

Introduction

In this page you can find the example usage for com.mongodb MapReduceCommand MapReduceCommand.

Prototype

public MapReduceCommand(final DBCollection inputCollection, final String map, final String reduce,
        @Nullable final String outputCollection, final OutputType type, final DBObject query) 

Source Link

Document

Represents the command for a map reduce operation Runs the command in REPLACE output type to a named collection

Usage

From source file:org.jeo.mongo.DefaultMapper.java

License:Open Source License

@Override
public Envelope bbox(DBCollection dbcol, MongoDataset data) {
    MapReduceCommand mr = new MapReduceCommand(dbcol, bboxMap, bboxReduce, null, OutputType.INLINE,
            new BasicDBObject());

    BasicDBList list = (BasicDBList) dbcol.mapReduce(mr).getCommandResult().get("results");
    DBObject bbox = (DBObject) ((DBObject) list.get(0)).get("value");

    return new Envelope((Double) bbox.get("x1"), (Double) bbox.get("x2"), (Double) bbox.get("y1"),
            (Double) bbox.get("y2"));
}

From source file:org.mongodb.morphia.MapReduceOptions.java

License:Apache License

@SuppressWarnings("deprecation")
MapReduceCommand toCommand(final Mapper mapper) {
    if (query.getOffset() != 0 || query.getFieldsObject() != null) {
        throw new QueryException("mapReduce does not allow the offset/retrievedFields query ");
    }/*www . ja v  a2  s .  co  m*/

    final DBCollection dbColl = inputCollection != null
            ? getQuery().getCollection().getDB().getCollection(inputCollection)
            : query.getCollection();
    final String target = outputCollection != null ? outputCollection
            : mapper.getMappedClass(resultType).getCollectionName();

    final MapReduceCommand command = new MapReduceCommand(dbColl, map, reduce, target, outputType,
            query.getQueryObject());
    command.setBypassDocumentValidation(bypassDocumentValidation);
    command.setCollation(collation);
    command.setFinalize(finalize);
    command.setJsMode(jsMode);
    command.setLimit(limit);
    command.setMaxTime(maxTimeMS, TimeUnit.MILLISECONDS);
    command.setOutputDB(outputDB);
    command.setReadPreference(readPreference);
    command.setScope(scope);
    command.setSort(query.getSortObject());
    command.setVerbose(verbose);

    return command;
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

/**
 * performs a map reduce operation/*from w  w  w  .ja v  a  2s. c o m*/
 * Runs the command in REPLACE output mode (saves to named collection)
 *
 * @param map          map function in javascript code
 * @param outputTarget optional - leave null if want to use temp collection
 * @param reduce       reduce function in javascript code
 * @param query        to match
 * @return The output
 * @throws MongoException If an error occurred
 */
@Deprecated
public com.mongodb.MapReduceOutput mapReduce(String map, String reduce, String outputTarget, DBObject query)
        throws MongoException {
    return mapReduce(new MapReduceCommand(dbCollection, map, reduce, outputTarget,
            MapReduceCommand.OutputType.REPLACE, serializeFields(query)));
}

From source file:org.mongojack.JacksonDBCollection.java

License:Apache License

/**
 * performs a map reduce operation/*w ww .j av a 2s.c o m*/
 * Specify an outputType to control job execution
 * * INLINE - Return results inline
 * * REPLACE - Replace the output collection with the job output
 * * MERGE - Merge the job output with the existing contents of outputTarget
 * * REDUCE - Reduce the job output with the existing contents of
 * outputTarget
 *
 * @param map          map function in javascript code
 * @param outputTarget optional - leave null if want to use temp collection
 * @param outputType   set the type of job output
 * @param reduce       reduce function in javascript code
 * @param query        to match
 * @return The output
 * @throws MongoException If an error occurred
 */
@Deprecated
public com.mongodb.MapReduceOutput mapReduce(String map, String reduce, String outputTarget,
        MapReduceCommand.OutputType outputType, DBObject query) throws MongoException {
    return mapReduce(
            new MapReduceCommand(dbCollection, map, reduce, outputTarget, outputType, serializeFields(query)));
}

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 w w w  . ja va2  s  .  c  om*/

    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);
}