【问题标题】:MongoDB MapReduce Emit StrangenessMongoDB MapReduce 发出异常
【发布时间】:2012-03-09 06:51:42
【问题描述】:

在 MapReduce 方面,我是个菜鸟,我一直在为这个问题而烦恼。希望有人可以帮我一把。

我的目标:获取产品收入和售出数量。

我从中查询的事务集合示例文档:

 { "_id" : ObjectId( "xxxxxxxxxx" ),
  "MerchantID" : { "$ref" : "merchants",
    "$id" : ObjectId( "xxxxxxxx" ) },
  "TransactionSocialKey" : "xxxxxxxx",
  "PurchaseComplete: true,
  "Products" : [ 
    { "ProductID" : { "$ref" : "products",
        "$id" : ObjectId( "4ecae2b9cf72ab1f6900xxx1" ) },
      "ProductPrice" : 14.99,
      "ProductQuantity" : "1" }, 
    { "ProductID" : { "$ref" : "products",
        "$id" : ObjectId( "4ecae2b9cf72ab1f690xxx2" ) },
      "ProductPrice" : 14.99,
      "ProductQuantity" : "1" } ],
  "DateTimeCreated" : Date( 1321919161000 ) }

如您所见,我有一个名为 Products 的嵌入式数组,其中包含 ProductID、Product Price 和 Product Quantity。

我的地图功能

map = function(){

  if(this.PurchaseComplete === true){

        this.Products.forEach(function(Product){

            if(Product.ProductID.$id.toString() == Product_ID.toString()){

                emit(Product_ID, {
                    "ProductQuantity" : Product.ProductQuantity, 
                    "ProductPrice" : Product.ProductPrice,
                    "ProductID" : Product.ProductID.$id.toString()
                }); 

            }

        }); 
    }
}

因此,我只会发出已完成的事务。如果事务已完成,我将遍历 Products 数组,并且如果 Product.ProductID.$id 等于我在 MapReduce Scope 中设置的 Product_ID,那么我将从集合中发出 Product。

为了测试,我将 Reduce 函数设置为:

reduce = function(key, Product_Transactions){

    return {"Transactions" : Product_Transactions}; 

}

出于某种奇怪的原因,我得到了这种结果:

[results] => Array
        (
            [0] => Array
                (
                    [_id] => MongoId Object
                        (
                            [$id] => 4ecae2b9cf72ab1f6900xxx1
                        )

                    [value] => Array
                        (
                            [Transactions] => Array
                                (
                                    [0] => Array
                                        (
                                            [Transactions] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [ProductQuantity] => 1
                                                            [ProductPrice] => 14.99
                                                            [ProductID] => 4ecae2b9cf72ab1f6900xxx1
                                                        )

                                                    [1] => Array
                                                        (
                                                            [ProductQuantity] => 1
                                                            [ProductPrice] => 14.99
                                                            [ProductID] => 4ecae2b9cf72ab1f6900xxx1
                                                        )

                                                       It Continues… 
                                                 )
                                         )
                                   [1] => Array
                                        (
                                            [ProductQuantity] => 1
                                            [ProductPrice] => 12.74
                                            [ProductID] => 4ecae2b9cf72ab1f6900xxx1
                                        )

                                    [2] => Array
                                        (
                                            [ProductQuantity] => 1
                                            [ProductPrice] => 12.74
                                            [ProductID] => 4ecae2b9cf72ab1f6900xxx1
                                        )

                            )
                       )
                   )
             )

我不确定为什么会得到这个奇怪的嵌入式数组。发射键始终相同且永不改变。我真的很想知道从哪里开始解决问题。任何帮助或指导将不胜感激。

【问题讨论】:

    标签: mongodb mapreduce


    【解决方案1】:

    map 的输出应该与reduce 消费和生产的格式相同。这个想法是reduce 可以并行运行和/或针对部分减少的结果。

    你的代码应该是这样的(伪代码)

    var map = function() {
      if(some condition) {
        emit(product_id, {Transactions: [{ // <= note the array here!
                            "ProductQuantity" : Product.ProductQuantity, 
                            "ProductPrice" : Product.ProductPrice,
                            "ProductID" : ID
                        }]})
      }
    };
    
    var reduce = function(key, vals) {
      var result = {Transactions: []};
    
      vals.forEach(function(v) {
        v.Transactions.forEach(t) {
          result.Transactions.push(t);
        }
      });
    
      return result;
    }
    

    【讨论】:

    • 非常感谢您的快速回复。我会考虑你的建议,看看这是否能帮到我。
    • 我想我对此的一个大问题是,为什么发射会产生一个多维数组?我真的只是在寻找一个一维关联数组。我不明白为什么有两个 Transactions 键。
    • Emit 工作正常。是你的减少把事情搞砸了。你听过我的建议吗?
    • @SergioTulentsev 我有类似的问题,我尝试了你的建议,但没有任何反应。你能看看我的问题吗? stackoverflow.com/questions/24283502/…
    • @NHG:我的建议不适用于那里。
    猜你喜欢
    • 2014-02-19
    • 2019-02-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2021-04-07
    • 2014-02-24
    • 2013-06-03
    相关资源
    最近更新 更多