0

I had data structure in MongoDB as below

{
    "_id" : ObjectId("523aab00045624a385e5f549"),
    "name" : "English Book 29",
    "SKU" : 1000549081,
    "price" : 249000,
    "image" : null,
    "category_id" : ObjectId("523a7802b50418baf38b4575"),
    "category_name" : "English Book",
    "details" : {
        "Title" : "Title 549081",
        "Binding" : 1,
        "Author" : "Author 0",
        "Publication data" : 0.5263832447608386,
        "Publisher name" : "Publisher name 14",
        "Number of page" : 90
    }
}

Binding of book has 2 values: 0 that means soft binding, and 1 that means hard binding. I write Map Reduce to statistics for each values.

var map = function()
{
  for(var key in this.details)
  {
    if(key == 'Binding')
    { 
        emit({name: key}, {
          'data':
          [
            {
              name: this.details[key],
              count: 1
            }
          ]
        });      
    }
  }
};

var reduce = function (key, values) {
  var reduced = {};
  for(var i in values)
  {
    var inter = values[i];
    for(var j in inter.data)
    {
      if(typeof(reduced[inter.data[j].name]) != "undefined")
      {
        reduced[inter.data[j].name] += inter.data[j].count;
      }
      else
      {
        reduced[inter.data[j].name] = 1;
      }
    }
  }
  return reduced;
};

When I run with small data (50 records) result return exactly. But when I run it with real data (192000 records) result return Not exactly. The result as below

{
    "_id" : {
        "name" : "Binding"
    },
    "value" : {
        "0" : 50,
        "1" : 50
    }
}

I checked return data when Map/Reduce done, result as below

"counts" : {
        "input" : 192000,
        "emit" : 192000,
        "reduce" : 1920,
        "output" : 1
    },

What wrong with it. Welcome any suggestion, explanation. Thanks and best regards,

4

1 回答 1

0

昨天研究了 Map/Reduce 后,我意识到,“Emit”一次发送 100 个元素,“Reduce”在这个数据集上执行。所以我上面的代码是错误的,因为它只在小数据集上“求和”。下面是我的 Map-Reduce 新代码

var map = function ()
{
    for(var key in this.details)
    {
        if(key == 'Binding')
        {
            var value = {};
            value[this.details[key]] = 1;
            emit(key, value);
        }
    }
}

var reduce = function (key, values)
{
    var reduced = {};
    for(var idx = 0; idx < values.length; idx++)
    {
        var inner = values[idx];
        for (var j in inner)
        {
            if (typeof (reduced[j]) == 'undefined')
            {
                reduced[j] = 0;
            }
            reduced[j] += inner[j];
        }
    }
    return reduced;
}

我在这里为遇到类似情况的任何人发帖。谢谢阅读。

于 2013-09-23T03:55:03.470 回答