My first Couchbase Mapreduce does not work properly


#1

Hi all,
I’m new to couchbase and I am trying to write a custom view,
here is my code

Map:

function (doc, meta) {
emit(‘1’,1);
}

Reduce:

function(key, values, rereduce) {
var sum = 0;
for(i=0; i < values.length; i++) {
sum = sum + values[i];
}
return(sum);
}

I suppose this mr should output a single line like :

Key Value
1 123

But what I get is:

Key Value
null 123

I do not know what is going wrong , I just can not get any Key if I write a reduce function.


#2

Hello,

First of all, do not code a custom reduce function when it is not necessary. If you need some basic mathematical function such as sum, count, statistics use the built-in functions. See the section
Reduce Functions

So based on your view (not sure to understand the key to 1, I guess this is a simple test), this is the expected behavior:

  • when you execute the view without any parameter, the reduce function will be used to so the some of “all the keys” without any group: so no key, sum of values = 123.

  • Then you can select the group level, in your case you have a single group, so you can do
    group_level=1
    and you will see
    key:1, value 123

You can find some information about this in the documentation, Chapter : Grouping in Queries

Regards
Tug
@tgrall


#3

Great, it works now. Thanks!

BTW, is there a list of parameter introductions? and is there a good way to add these parameters while debugging, do I have to manually add them to the test URL?


#4

I found the parameters in the Filter List , thanks!


#5

just to share this with the community, here the documentation chapter with the various parameters you can use:

http://www.couchbase.com/docs/couchbase-manual-2.1.0/couchbase-views-querying-rest-api.html