[Spring Data MongoDB]学习笔记--MapReduce

mongodb的MapReduce主要包含两个方法:map和reduce。

举个例子,假设现在有下面3条记录

{ "_id" : ObjectId("4e5ff893c0277826074ec533"), "x" : [ "a", "b" ] }
{ "_id" : ObjectId("4e5ff893c0277826074ec534"), "x" : [ "b", "c" ] }
{ "_id" : ObjectId("4e5ff893c0277826074ec535"), "x" : [ "c", "d" ] }

map方法调用emit方法,返回键值对,键是x[i]的值,比如a; 值是1。

function () {
    for (var i = 0; i < this.x.length; i++) {
        emit(this.x[i], 1);
    }
}

reduce方法负责把键值对进行统计。

function (key, values) {
    var sum = 0;
    for (var i = 0; i < values.length; i++)
        sum += values[i];
    return sum;
}

执行结果如下

{ "_id" : "a", "value" : 1 }
{ "_id" : "b", "value" : 2 }
{ "_id" : "c", "value" : 2 }
{ "_id" : "d", "value" : 1 }

假设map和reduce方法分别存放在map.js和reduce.js中,可以通过下面的代码获得mapreduce结果。

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", ValueObject.class);
for (ValueObject valueObject : results) {
  System.out.println(valueObject);
}

输出是

ValueObject [id=a, value=1.0]
ValueObject [id=b, value=2.0]
ValueObject [id=c, value=2.0]
ValueObject [id=d, value=1.0]

ValueObject代码

public class ValueObject {

  private String id;
  private float value;

  public String getId() {
    return id;
  }

  public float getValue() {
    return value;
  }

  public void setValue(float value) {
    this.value = value;
  }

  @Override
  public String toString() {
    return "ValueObject [id=" + id + ", value=" + value + "]";
  }
}

可以增加一个MapReduceOptions来增加一些map-reduce的options,下面把结果存入collection。

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", 
                                                                     new MapReduceOptions().outputCollection("jmr1_out"), ValueObject.class);

MapReduceOptions也可以通过静态方法options()来实例化。

MapReduceResults<ValueObject> results = mongoOperations.mapReduce("jmr1", "classpath:map.js", "classpath:reduce.js", 
                                                                     options().outputCollection("jmr1_out"), ValueObject.class);

还可以加入一个query来对统计进行过滤,下面是把包含a和b的document排除在外。

Query query = new Query(where("x").ne(new String[] { "a", "b" }));
MapReduceResults<ValueObject> results = mongoOperations.mapReduce(query, "jmr1", "classpath:map.js", "classpath:reduce.js", 
                                                                     options().outputCollection("jmr1_out"), ValueObject.class);

 

posted @ 2014-08-06 16:07  lemon_bar  阅读(2583)  评论(0编辑  收藏  举报