从mapreduce访问json数组的内部元素

时间:2012-04-12 09:26:31

标签: arrays json mongodb mapreduce

我的示例Json对象如下

{
    "numRecommenders": 0,
    "publicProfileUrl": "http://www.linkedin.com/pub/heena-vyas/16/786/826",
    "positions": {
        "total": 1,
        "positionList": [
            {
                "id": "91286566",
                "title": "senior executive",
                "company": {
                    "name": "Reliance",
                    "industry": "Oil & Energy",
                    "type": "Public Company",
                    "size": "10,001+ employees"
                },
                "isCurrent": true
            }
        ]
    },

我的mapreduce函数如下:

String map = "function() {"
                 +"for (index in this.positions.positionList) {"
                        +"emit(this.positions.positionList[index].company.name, {count: 1});"
                   +"}}";

        String reduce = "function(key, values) {" + "var sum = 0;"
                + "values.forEach(function(value) {" + "sum += value['count'];"
                + "});" + "return {count: sum};" + "};";

        MapReduceCommand mapReduceCommand = new MapReduceCommand(
                mongoCollection, map, reduce.toString(), null,
                MapReduceCommand.OutputType.INLINE, null);

        MapReduceOutput out = mongoCollection.mapReduce(mapReduceCommand);

但是目前我正在使用来自mongoDb的150万个对象。

我收到以下异常..

线程“main”中的异常com.mongodb.CommandResult $ CommandFailure:命令失败[命令失败[mapreduce] {“断言”:“内存映射/减少的数据太多”,“assertionCode”:13604,“errmsg “:”db断言失败“,”ok“:0.0}     at com.mongodb.CommandResult.getException(CommandResult.java:70)     在com.mongodb.CommandResult.throwOnError(CommandResult.java:116)     在com.mongodb.DBCollection.mapReduce(DBCollection.java:961)     在com.mongo.dboperations.MongoStorage.runGroupCommand(MongoStorage.java:126)

at com.mongo.dboperations.MongoStorage.main(MongoStorage.java:218)<< h2>

如何解决此异常?

1 个答案:

答案 0 :(得分:1)

MongoDB中map / reduce的内联输出是最快的选项,前提是输出符合16 MB的限制。除此之外,您必须输出到集合。