English 中文(简体)
2. 利用儿童dict价值观 Mongo Map/Reduce
原标题:Get child dict values use Mongo Map/Reduce

我有一只红树收集,一只希望获得特定地点名称、时装和变式补贴的总数。 由于我们拥有大量数据,因此它将更好地利用地图/uce。 难道有人会向我提出任何建议吗?

这里我收集的json格式

{ "_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,
        "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.

如果您将<代码>key作为GROUP BY的<>作为<编码>SUM()、AVG(()等。

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);
  }
}

降低功能将获得各种价值,如<代码>{点击:41 }。 该功能必须归还一个看上去的物体。

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.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签