0

I have data on Riak DB in the format like:

Bakshi - {u'friendlist': [u'Sita', u' Hari', u' Shyam', u' Howard', u' Bindu', u' Kishna', u' Karma', u' Dinesh'], u'is_active': True, u'user': u'Bakshi'}

Here fethcing Data Via Map/Reduce

client = riak.RiakClient()
user_bucket = client.add('userfriendlist')

user_bucket.map("""
       function(v){
           var data = JSON.parse(v.values[0].data);
           if(data.is_active == true){
               tempUser = data.user;
               tempFriendlist = data.friendlist;
               for (var i=0; i<tempFriendlist.length; i++){
                    friendValue = tempFriendlist[i];                                    
                    return [[tempUser,friendValue]];

               }           
           }
        }

      """)

and print the return value

for result in user_bucket.run():
    print "%s - %s" % (result[0],result[1]);

It gives the output like:

Bakshi - Sita

But the expected outpt is:

Bakshi - Sita
Bakshi- Hari
Bakshi- Shyam
Bakshi- Howard
Bakshi- Bindu
Bakshi- Kishna
Bakshi- Karma
Bakshi- Dinesh

what i am doing wrong here, I cannot do iteration inside map()?

bikas
  • 81
  • 1
  • 8

1 Answers1

1

I am not very familiar with the technology you are using, but considering how mapping works in JS, I think you what you do all the time is return the first element as a result in your function. i.e. you return [Bakshi - Sita] as your result and then the mapping function stops, once it hits the return.

I think you need to do something like:

user_bucket.map("""
   function(v){
       var data = JSON.parse(v.values[0].data);
       var mapped = [];
       if(data.is_active == true){
           tempUser = data.user;
           tempFriendlist = data.friendlist;
           for (var i=0; i<tempFriendlist.length; i++){
                friendValue = tempFriendlist[i]; 
                mapped.push[[tempUser, friendValue]];                                   
           }         
       }
       return mapped;  //if data.is_active == false, it will return empty array if you return here. If you want to return undefined for data.is_active == false, put this return statement inside your if, as the last thing after your for loop.
    }

  """)

I hope this helps you.

Theodoros Klikas
  • 2,059
  • 1
  • 11
  • 7