score:1

Accepted answer

first problem as noted in the comments is that reduce_identity returns a list of bucket/key pairs which isn't what you want. second problem is ... you can't go straight to your pojo from mapreduce if you're storing something other than json. third (sorta) problem is ... riak mapreduce is really not made for binary values.

the following uses the kryopersonconverter and person class from the example in the cookbook that demonstrates how to use kryo in a custom converter.

i'm going to break to code sample up and inline my comments:

public class app 
{
    public static void main( string[] args ) throws riakexception
    {
        list<person> personlist = new arraylist<person>();

        person p = new person("bob","111 elm street","555-1212");
        personlist.add(p);
        p = new person("jenny","122 spruce lane","867-5309");
        personlist.add(p);
        p = new person("steve","333 oak place","555-1111");
        personlist.add(p);


        iriakclient client = riakfactory.pbcclient();
        bucket b = client.fetchbucket("people").execute();
        kryopersonconverter converter = new kryopersonconverter("people");

        for (person p2 : personlist)
        {
            b.store(p2).withconverter(converter).execute();
        }

        p = new person();
        p.setname("jenny");
        p = b.fetch(p).withconverter(converter).execute();
        assert(p.getphone().equals("867-5309")); // i got your number

everything up to now? a-ok! we've stored a pojo in riak after having used kryo to serialize it, and retrieved it.

        mapreduceresult result = client.mapreduce("people")
                                       .addmapphase(namederlangfunction.map_object_value)
                                       .execute();

        system.out.println(result.getresultraw());

and here we see the problem, as the output of that println() is:

["\u0001\u0001\u000e111 elm street\u0001\u0003bob\u0001\b555-1212","\u0001\u0001\r333 oak place\u0001\u0005steve\u0001\b555-1111","\u0001\u0001\u000f122 spruce lane\u0001\u0005jenny\u0001\b867-5309"]

unfortunately, mapreduce in riak is really meant to be used with json data (or just plain strings) when talking about stored data. we have a json array containing json strings of the bytes we stored.

to work with that, you'd have to get the strings as a collection, then convert the bytes using kryo.

        collection<string> objects = result.getresult(string.class);


        kryo kryo = new kryo();
        kryo.register(person.class);
        objectbuffer buffer = new objectbuffer(kryo);

        for (string s : objects)
        {
            person p3 = buffer.readobject(s.getbytes(), person.class);
            system.out.println(p3.getname() + " " + p3.getphone());
        }

        client.shutdown();     
    }
}

and you'd get the output:

bob 555-1212
steve 555-1111
jenny 867-5309


Related Query

More Query from same tag