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 DBObject query) 

Source Link

Document

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

Usage

From source file:implementations.mongoDB.java

License:GNU General Public License

public void searchDB(String keyword) {
    ArrayList<String> res = new ArrayList<String>();
    String map_index = "function() {" + "var words = this.artValue.split(' ');" + "var keyword = \"" + keyword
            + "\";" + "for ( var i=0; i<words.length; i++ ) {"
            + "if(words[i].toLowerCase()  == keyword.toLowerCase() ){" + "emit(words[i], { docs: [this._id] });"
            + "}}}";

    String reduce_index = "function(key, values) {" + "var docs = [];"
            + "values.forEach ( function(val) { docs = docs.concat(val.docs); });" + "return { docs: docs };}";

    String map_relevance = "function() {" + "for ( var i=0; i< this.value.docs.length; i++ ) {"
            + "emit(this.value.docs[i], { count: 1 });}}";

    String reduce_relevance = "function(key, values) {" + "var sum = 0;"
            + "values.forEach ( function(val) { sum += val.count; });" + "return { count: sum };}";

    //First MapReduce phase
    MapReduceOutput tempRes = dbCol.mapReduce(map_index, reduce_index, "tempRes1", null);
    DBCollection outCol = tempRes.getOutputCollection();
    //Second MapReduce phase
    MapReduceOutput tempRes2 = outCol.mapReduce(map_relevance, reduce_relevance, "tempRes2", null);
    //      DBCollection outCol2 = tempRes2.getOutputCollection();
    //      DBCursor cur = outCol2.find();
    //      while(cur.hasNext()){
    //         res.add(cur.next().toString());
    //      }/* w w  w  .  jav  a 2 s. com*/
    //      System.out.println(res);
}

From source file:org.geotools.data.mongodb.MongoLayer.java

License:LGPL

/**
 * Generate model of collection records' data fields and types
 * //from   w  w w  .ja  v a  2  s.c  o m
 * @param coll mongo collection
 * @param buildRule which rule to apply if same named fields with different types exist
 * @return JSON object describing collection record
 */
private DBObject getCollectionModel(DBCollection coll, RecordBuilder buildRule) {
    // call map-reduce job to generate metadata
    // mongo java driver calls mapReduce with the functions rather than the name of the
    // functions
    // function prototypes from scripts/mrscripts/MetaDataCompute.js
    // (do not include comments in quoted javascript functions below-gives mongo error)
    coll.mapReduce(metaMapFunc, metaReduceFunc, metaResultsColl, new BasicDBObject());

    // get mapping of field names and types, and counts for different types
    DBCollection metaColl = coll.getDB().getCollection(metaResultsColl);
    HashMap<String, ClassCount> fieldMap = getFieldMap(metaColl);
    log.finest("fieldMap=" + fieldMap);

    // resulting collection may have dupes for fields of different types
    // use build rule to determine final type
    HashMap<String, String> finalMap = finalizeMajorityRule(fieldMap, buildRule);
    log.finest("finalMap=" + finalMap);

    // convert map of field names with types and associated counts to a JSON DBObject
    DBObject metaData = convertMapToJson(finalMap);
    log.finest("metaData=" + metaData);

    return metaData;
}

From source file:org.iternine.jeppetto.dao.mongodb.projections.MapReduceCommand.java

License:Apache License

@Override
public final Object singleResult(DBCollection dbCollection) {
    MapReduceOutput output = dbCollection.mapReduce(createMapFunction(), createReduceFunction(), null, query);
    DBCursor cursor = output.results();/* w  ww .  ja  v  a2  s  .  c  o  m*/

    try {
        return transformToValue(cursor);
    } finally {
        output.drop();
    }
}