开发者

Get child dict values use Mongo Map/Reduce

I have a mongo collection, i want get total value of 'number_of_ad_clicks' by given sitename, timestamp and variant id. Because we have large data so it would be better use map/reduce. Could any guys give me any suggestion?

Here is my collection json format

{ "_id" : ObjectId( "4e3c280ecacbd1333b00f5ff" ),
  "timestamp" : "20110805",
  "variants" : { "94" : { "number_of_ad_clicks" : 41,
      "number_of_search_keywords" : 9,
      "total_duration" : 0,
      "os" : { "os_2" : 2,
        "os_1" : 1,
        "os_0" : 0 },
      "countries" : { "ge" : 6,
        "ca" : 1,
        "fr" : 8,
        "uk" : 4,
        "us" : 6 },
      "screen_resolutions" : { "(320, 240)" : 1,
        "(640, 480)" : 5,
        "(1024, 960)" : 5,
        "(1280, 768)" : 5 },
      "widgets" : { "widget_1" : 1,
        "widget_0" : 0 },
      "languages" : { "ua_uk" : 8,
        "ca_en" : 2,
        "ca_fr" : 2,
        "us_en" : 5 },
      "search_keywords" : { "search_keyword_8" : 8,
        "search_keyword_5" : 5,
        "search_keyword_4" : 4,
        "search_keyword_7" : 7,
        "search_keyword_6" : 6,
        "search_keyword_1" : 1,
        "search_keyword_3" : 3,
        "search_keyword_2" : 2 },
      "number_of_pageviews" : 18,
      "browsers" : { "browser_4" : 4,
  开发者_StackOverflow      "browser_0" : 0,
        "browser_1" : 1,
        "browser_2" : 2,
        "browser_3" : 3 },
      "keywords" : { "keyword_5" : 5,
        "keyword_4" : 4,
        "keyword_1" : 1,
        "keyword_0" : 0,
        "keyword_3" : 3,
        "keyword_2" : 2 },
      "number_of_keyword_clicks" : 83,
      "number_of_visits" : 96 } },
  "site_name" : "fonter.com",
  "number_of_variants" : 1 }

Here is my try. but failed. He is my try.

m = function() {
    emit(this.query, {variants: this.variants});
}

r = function(key , vals) {
    var clicks = 0 ;
    for(var i = 0; i < vals.length(); i++){
        clicks = vals[i]['number_of_ad_clicks'];
    }
    return clicks;
}
res = db.variant_daily_collection.mapReduce(m, r, {out : "myoutput", "query":{"site_name": 'fonter.com', 'timestamp': '20110805'}})
db.myoutput.find()

could somebody any suggestion?

Thank you very much, i try you solution but nothing return. I invoke the mapreduce in the following, is there any thing wrong?

res = db.variant_daily_collection.mapReduce(map, reduce, {out : "myoutput", "query":{"site_name": 'facee.com', 'timestamp': '20110809', 'variant_id': '305'}})
db.myoutput.find()


The emit function emits both a key and a value.

If you are used to SQL think of key as your GROUP BY and value as your SUM(), AVG(), etc..

In your case you want to "group by": site_name, timestamp and variant id. It looks like you may have more than one variant, so you will need to loop through the variants, like this:

map = function() {
  for(var i in variants){
    var key = {};
    key.timestamp = this.timestamp;
    key.site_name = this.site_name;
    key.variant_id = i; // that's the "94" string.

    var value = {};
    value.clicks = this.variants[i].number_of_ad_clicks;

    emit(key, value);
  }
}

The reduce function will get an array of values each one like this { clicks: 41 }. The function needs to return one object that looks the same.

So if you get values = [ {clicks:21}, {clicks:10}, {clicks:5} ] you must output {clicks:36}.

So you do something like this:

reduce = function(key , vals) {
    var returnValue = { clicks: 0 }; // initializing to zero
    for(var i = 0; i < vals.length(); i++){
        returnValue.clicks += vals[i].clicks;
    }
    return returnValue;
}

Note that the value from map has the same shape as the return from reduce.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜