Example usage for com.mongodb DBCollection mapReduce

List of usage examples for com.mongodb DBCollection mapReduce

Introduction

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

Prototype

public MapReduceOutput mapReduce(final String map, final String reduce, final String outputTarget,
        final MapReduceCommand.OutputType outputType, final DBObject query) 

Source Link

Document

Allows you to run map-reduce aggregation operations over a collection and saves to a named collection.

Usage

From source file:com.hangum.tadpole.mongodb.core.test.MongoTestMapReduce.java

License:Open Source License

/**
 * @param args/* w w  w.  ja v a  2s .c  om*/
 */
public static void main(String[] args) throws Exception {

    ConAndAuthentication testMongoCls = new ConAndAuthentication();
    Mongo mongo = testMongoCls.connection(ConAndAuthentication.serverurl, ConAndAuthentication.port);
    DB db = mongo.getDB("test");
    DBCollection coll = db.getCollection("person");

    MapReduceOutput out = coll.mapReduce(map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
    for (DBObject obj : out.results()) {
        System.out.println("======================================================");
        System.out.println("\t[_id]\t" + obj.get("_id"));
        Map objResult = (Map) obj.get("value");
        System.out.println("\t[count]\t" + objResult.get("count"));
        System.out.println("\t[sum]\t" + objResult.get("sum"));

        System.out.println(obj);
    }
    out.getRaw();
    out.getRaw();

    mongo.close();

    try {
        Thread.sleep(1);
    } catch (Exception e) {
    }
}

From source file:de.otto.mongodb.profiler.collection.ListSizeDistribution.java

License:Apache License

/**
 * Calculates the distribution sizes of arrays / lists inside documents of a collection. Executes a map-reduce
 * operation to aggregate the values. Returns a map where the key determines the length of the array / list
 * and the value the amount of documents with that length.
 *
 * @param collection           the collection
 * @param attribute            the name of the attribute that is an array inside documents of the collection
 * @param resultCollectionName the name of the result collection of the map-reduce
 * @return the map of array lengths to the amount of documents
 *//*  w  w w.ja v a  2s .c o  m*/
public static Map<Long, Long> calculate(final DBCollection collection, final String attribute,
        final String resultCollectionName) {

    checkNotNull(collection);
    checkNotNull(attribute);

    final String mapFunction = String.format(MAP_FUNCTION, attribute);

    final MapReduceCommand.OutputType outputType = resultCollectionName != null
            ? MapReduceCommand.OutputType.REPLACE
            : MapReduceCommand.OutputType.INLINE;

    final MapReduceOutput output = collection.mapReduce(mapFunction, REDUCE_FUNCTION, resultCollectionName,
            outputType, QUERY_ALL);

    final Map<Long, Long> result = new TreeMap<>(KEYS_BY_VALUE);
    for (DBObject dbo : output.results()) {
        final Number id = ((Number) dbo.get("_id"));
        final Number count = ((Number) ((DBObject) dbo.get("value")).get("count"));
        result.put(Long.valueOf(id.longValue()), Long.valueOf(count.longValue()));
    }

    return result;
}

From source file:org.exist.mongodb.xquery.mongodb.collection.MapReduce.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*w w  w .  j  a v  a 2  s  .c o  m*/
        // Verify clientid and get client
        String mongodbClientId = args[0].itemAt(0).getStringValue();
        MongodbClientStore.getInstance().validate(mongodbClientId);
        MongoClient client = MongodbClientStore.getInstance().get(mongodbClientId);

        // Get parameters
        String dbname = args[1].itemAt(0).getStringValue();
        String collection = args[2].itemAt(0).getStringValue();

        String map = args[3].itemAt(0).getStringValue();
        String reduce = args[4].itemAt(0).getStringValue();

        // output-target can have value null
        String outputTarget = args[5].isEmpty() ? null : args[5].itemAt(0).getStringValue();

        OutputType outputType = args[6].isEmpty() ? OutputType.INLINE
                : OutputType.valueOf(args[6].itemAt(0).getStringValue().toUpperCase(Locale.US));

        DBObject query = (BasicDBObject) JSON.parse(args[7].itemAt(0).getStringValue());

        // Get collection in database
        DB db = client.getDB(dbname);
        DBCollection dbcol = db.getCollection(collection);

        // Execute query      
        MapReduceOutput output = dbcol.mapReduce(map, reduce, outputTarget, outputType, query);

        // Parse results
        Sequence retVal = new ValueSequence();

        for (DBObject result : output.results()) {
            retVal.add(new StringValue(result.toString()));
        }

        return retVal;

    } catch (JSONParseException ex) {
        LOG.error(ex.getMessage());
        throw new XPathException(this, MongodbModule.MONG0004, ex.getMessage());

    } catch (XPathException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, ex.getMessage(), ex);

    } catch (MongoCommandException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0005, ex.getMessage());

    } catch (MongoException ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0002, ex.getMessage());

    } catch (Throwable ex) {
        LOG.error(ex.getMessage(), ex);
        throw new XPathException(this, MongodbModule.MONG0003, ex.getMessage());
    }

}