0

I want to perform MapReduce job on data in Riak DB using javascript. But stuck in very begining, i couldnot understand how it is returning value.

client = riak.RiakClient()
query = client.add('user')
query.map("""
            function(v){
               var i=0;
               i++;
             return [i];
             }
         """)

for result in query.run():
    print "%s" % (result);

For simplicity i have checked the above example.

Here query is bucket and user contain five sets of data in RiakDB. i think map() returns single value but it returns array with 5 value, i think equivalent to five set of data in RiakDB.

1
1
1
1
1

And here, why I can return only array? it treats each dataset independently, and returns for each. so i think i have five 1's. Due to this reason when i process fetched data inside map(), returns gives unexpected result for me.

so please give me some suggestion. i think it is basic thing but i couldnot get it. i highly appreciate your help.

bikas
  • 81
  • 1
  • 8

1 Answers1

1

When you run a MapReduce job, the map phase code is sent out to the vnodes where the data is stored and executed for each value in the data. The resulting arrays are collected and passed to a single reduce phase, which also returns an array. If there are sufficiently many results, the reduce phase may be run multiple times, with the previous reduce result and a batch of map results as input.

The fact that you are getting 5 results implies that 5 keys were seen in your bucket. There is no global state shared between instances of the map phase function, so each will have an independent i, which is why each result is 1.

You might try returning [v.key] so that you have something unique for each one, or if the values are expected to be small, you could return [JSON.stringify(v)] so you can see the entire structure that is passed to the map.

You should note that according to the docs site javascript Map Reduce has been officially deprecated, so you may want to use Erlang functions for new development.

Joe
  • 25,000
  • 3
  • 22
  • 44