【问题标题】:Acessing inner element of json array from mapreduce从 mapreduce 访问 json 数组的内部元素
【发布时间】:2012-04-12 09:26:30
【问题描述】:

我的示例 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 中的异常:命令失败 [command failed [mapreduce] {“assertion”:“内存映射/减少中的数据过多”,“assertionCode”:13604,“errmsg " : "db 断言失败" , "ok" : 0.0} 在 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)

在 com.mongo.dboperations.MongoStorage.main(MongoStorage.java:218)

如何解决这个异常?

【问题讨论】:

  • 这段代码有效:String map = "function() {" +"for (index in this.positions.positionList) {" +"emit(this.positions.positionList[index].company.姓名,{count: 1});" +"}}";
  • 使用 for ... in 遍历数组是行不通的 - 这仅适用于对象属性

标签: arrays json mongodb mapreduce


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2019-09-30
    • 2017-02-05
    • 2017-11-29
    相关资源
    最近更新 更多