You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Kevin Wang <ja...@gmail.com> on 2009/04/28 04:32:44 UTC
map/reduce function question
Hi All,
While I am trying to switch my mind to map/reduce model while accessing
couchdb data, I found myself struggling. Here is a simple question that you
guys might be able to answer:
If I have the following set of data in a doc:
{"name": "foo", "prop": "value1"}
{"name": "bar", "prop": "value2"}
{"name": "foo", "prop": "value3"}
{"name": "bob", "prop": "value4"}
How to write the map/reduce functions so that I can get a view which returns
the total number of unique name's? In this case, the result should be 3.
Thanks!
-- Kevin
Re: map/reduce function question
Posted by Brian Candler <B....@pobox.com>.
On Mon, Apr 27, 2009 at 07:32:44PM -0700, Kevin Wang wrote:
> Hi All,
>
> While I am trying to switch my mind to map/reduce model while accessing
> couchdb data, I found myself struggling. Here is a simple question that you
> guys might be able to answer:
>
> If I have the following set of data in a doc:
>
> {"name": "foo", "prop": "value1"}
> {"name": "bar", "prop": "value2"}
> {"name": "foo", "prop": "value3"}
> {"name": "bob", "prop": "value4"}
>
> How to write the map/reduce functions so that I can get a view which returns
> the total number of unique name's? In this case, the result should be 3.
Simple answer:
//MAP
function(doc) {
if (doc.name) { emit(doc.name, null); }
}
//REDUCE
function(ks, vs, co) {
if (co) {
return sum(vs);
} else {
return vs.length;
}
}
Then query the view with group=true and count the number of rows returned.
You also get counts of the objects with each name for free (and you also get
an index by name for free, because you can query the same view with
reduce=false)
Alternatively, you can replace the above reduce function with this one,
which builds a single object that maps names to counts:
//REDUCE
function(ks, vs, co) {
if (co) {
var result = vs.shift();
for (var i in vs) {
for (var j in vs[i]) {
result[j] = (result[j] || 0) + vs[i][j];
}
}
return result;
} else {
var result = {};
for (var i in ks) {
var key = ks[i];
result[key[0]] = (result[key[0]] || 0) + 1;
}
return result;
}
}
Then the client counts the number of keys in the returned object.
This approach gets the results immediately from the root node without
walking the view index, but becomes inefficient if there are large numbers
of distinct names (say hundreds or thousands) as it builds a very large
reduced object.
HTH,
Brian.
Re: map/reduce function question
Posted by Wout Mertens <wo...@gmail.com>.
Well I was wrong, a reduce query doesn't show the amount of rows. So
you'll have to count the number of rows that the view Paul gave
returns in your application, or you'll have to wait until CouchDB
gains multiple map+reduce passes.
Wout.
On Apr 28, 2009, at 11:22 AM, Wout Mertens wrote:
> You can cheat: simply get the MR view that Paul gave you with
> group=true and it will tell you the total number of results, which is
> the answer you're looking for. At least, I think so, I can't test now.
>
> If it doesn't, this is yet another case for multi-MR views :)
>
> Wout.
>
> On Tuesday, April 28, 2009, Kevin Wang <ja...@gmail.com> wrote:
>> Sorry I think I made a mistake stating the problem.
>>
>> I have the following 4 docs:
>>
>> {"name": "foo", "prop": "value1"}
>> {"name": "bar", "prop": "value2"}
>> {"name": "foo", "prop": "value3"}
>> {"name": "bob", "prop": "value4"}
>>
>> How to write the map/reduce functions so that I can get a view
>> which returns
>> the total number of unique name's? In this case, the result should
>> be 3.
>>
>> -- Kevin
>>
>> On Mon, Apr 27, 2009 at 7:32 PM, Kevin Wang <ja...@gmail.com>
>> wrote:
>>
>>> Hi All,
>>>
>>> While I am trying to switch my mind to map/reduce model while
>>> accessing
>>> couchdb data, I found myself struggling. Here is a simple
>>> question that you
>>> guys might be able to answer:
>>>
>>> If I have the following set of data in a doc:
>>>
>>> {"name": "foo", "prop": "value1"}
>>> {"name": "bar", "prop": "value2"}
>>> {"name": "foo", "prop": "value3"}
>>> {"name": "bob", "prop": "value4"}
>>>
>>> How to write the map/reduce functions so that I can get a view which
>>> returns the total number of unique name's? In this case, the
>>> result should
>>> be 3.
>>>
>>> Thanks!
>>>
>>> -- Kevin
>>>
>>
Re: map/reduce function question
Posted by Wout Mertens <wo...@gmail.com>.
You can cheat: simply get the MR view that Paul gave you with
group=true and it will tell you the total number of results, which is
the answer you're looking for. At least, I think so, I can't test now.
If it doesn't, this is yet another case for multi-MR views :)
Wout.
On Tuesday, April 28, 2009, Kevin Wang <ja...@gmail.com> wrote:
> Sorry I think I made a mistake stating the problem.
>
> I have the following 4 docs:
>
> {"name": "foo", "prop": "value1"}
> {"name": "bar", "prop": "value2"}
> {"name": "foo", "prop": "value3"}
> {"name": "bob", "prop": "value4"}
>
> How to write the map/reduce functions so that I can get a view which returns
> the total number of unique name's? In this case, the result should be 3.
>
> -- Kevin
>
> On Mon, Apr 27, 2009 at 7:32 PM, Kevin Wang <ja...@gmail.com> wrote:
>
>> Hi All,
>>
>> While I am trying to switch my mind to map/reduce model while accessing
>> couchdb data, I found myself struggling. Here is a simple question that you
>> guys might be able to answer:
>>
>> If I have the following set of data in a doc:
>>
>> {"name": "foo", "prop": "value1"}
>> {"name": "bar", "prop": "value2"}
>> {"name": "foo", "prop": "value3"}
>> {"name": "bob", "prop": "value4"}
>>
>> How to write the map/reduce functions so that I can get a view which
>> returns the total number of unique name's? In this case, the result should
>> be 3.
>>
>> Thanks!
>>
>> -- Kevin
>>
>
Re: map/reduce function question
Posted by Kevin Wang <ja...@gmail.com>.
Sorry I think I made a mistake stating the problem.
I have the following 4 docs:
{"name": "foo", "prop": "value1"}
{"name": "bar", "prop": "value2"}
{"name": "foo", "prop": "value3"}
{"name": "bob", "prop": "value4"}
How to write the map/reduce functions so that I can get a view which returns
the total number of unique name's? In this case, the result should be 3.
-- Kevin
On Mon, Apr 27, 2009 at 7:32 PM, Kevin Wang <ja...@gmail.com> wrote:
> Hi All,
>
> While I am trying to switch my mind to map/reduce model while accessing
> couchdb data, I found myself struggling. Here is a simple question that you
> guys might be able to answer:
>
> If I have the following set of data in a doc:
>
> {"name": "foo", "prop": "value1"}
> {"name": "bar", "prop": "value2"}
> {"name": "foo", "prop": "value3"}
> {"name": "bob", "prop": "value4"}
>
> How to write the map/reduce functions so that I can get a view which
> returns the total number of unique name's? In this case, the result should
> be 3.
>
> Thanks!
>
> -- Kevin
>
Re: map/reduce function question
Posted by Kevin Wang <ja...@gmail.com>.
Hi Paul,
Thanks much for your kind help!
Will this only return the number of occurrences for each unique name, rather
than the number of unique names?
When I used Futon to access that view, it returned a list of all unique
names with corresponding number of occurrences for each of them.
When I used command curl, the result is:
{"rows":[
{"key":null,"value":4}
]}
Which seems to be the total number of docs.
Am I stupid or do I miss something?
Thanks!
-- Kevin
On Mon, Apr 27, 2009 at 10:42 PM, Paul Davis <pa...@gmail.com>wrote:
> Kevin,
>
> You'll want to do something like this:
>
> {
> "_id": "_design/foo",
> "views": {
> "unique_names": {
> "map": "function(doc) {emit(doc.name, 1);}",
> "reduce": function(keys, values, rereduce) {return sum(values);}"
> }
> }
> }
>
> And then you can call this like:
>
> http://127.0.0.1:5984/db_name/_design/foo/_view/unique_names
>
> to get ouput like
>
> {"rows": [
> {"key": null, "value": 3}
> }
>
> HTH,
> Paul Davis
>
>
> Kevin Wang wrote:
>
>> Hi All,
>>
>> While I am trying to switch my mind to map/reduce model while accessing
>> couchdb data, I found myself struggling. Here is a simple question that
>> you
>> guys might be able to answer:
>>
>> If I have the following set of data in a doc:
>>
>> {"name": "foo", "prop": "value1"}
>> {"name": "bar", "prop": "value2"}
>> {"name": "foo", "prop": "value3"}
>> {"name": "bob", "prop": "value4"}
>>
>> How to write the map/reduce functions so that I can get a view which
>> returns
>> the total number of unique name's? In this case, the result should be 3.
>>
>> Thanks!
>>
>> -- Kevin
>>
>>
>>
>
>
Re: map/reduce function question
Posted by Paul Davis <pa...@gmail.com>.
Kevin,
You'll want to do something like this:
{
"_id": "_design/foo",
"views": {
"unique_names": {
"map": "function(doc) {emit(doc.name, 1);}",
"reduce": function(keys, values, rereduce) {return
sum(values);}"
}
}
}
And then you can call this like:
http://127.0.0.1:5984/db_name/_design/foo/_view/unique_names
to get ouput like
{"rows": [
{"key": null, "value": 3}
}
HTH,
Paul Davis
Kevin Wang wrote:
> Hi All,
>
> While I am trying to switch my mind to map/reduce model while accessing
> couchdb data, I found myself struggling. Here is a simple question that you
> guys might be able to answer:
>
> If I have the following set of data in a doc:
>
> {"name": "foo", "prop": "value1"}
> {"name": "bar", "prop": "value2"}
> {"name": "foo", "prop": "value3"}
> {"name": "bob", "prop": "value4"}
>
> How to write the map/reduce functions so that I can get a view which returns
> the total number of unique name's? In this case, the result should be 3.
>
> Thanks!
>
> -- Kevin
>
>