Riak MapReduce in single node using javascript and python
<p>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.</p>

<pre><code>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);
</code></pre>

<p>For simplicity i have checked the above example. </p>

<p>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. </p>

<pre><code>1
1
1
1
1
</code></pre>

<p>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.</p>

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

<p>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.</p>

<p>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 <code>i</code>, which is why each result is <code>1</code>.</p>

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

<p>You should note that according to the <a href="https://docs.basho.com/riak/kv/2.1.4/developing/app-guide/advanced-mapreduce/" rel="nofollow">docs site</a> javascript Map Reduce has been officially deprecated, so you may want to use Erlang functions for new development.</p>

javascript python dictionary riak