On 25 Oct 2011, at 18:48, Alexander Robbins wrote:

> That code snippet isn't working because I've got a RawClient instance and its 
> mapReduce method wants a MapReduceSpec. I ran into this problem while trying 
> to use search also. I ended up making a search object and then serializing it 
> to json and providing it to the MapReduceSpec constructor, then passing the 
> resulting MapReduceSpec into the mapReduce method on RawClient. Is there a 
> better way? I considered just using String.format to put the variables into a 
> handwritten json string and using that to instantiate the MapReduceSpec.
> 
> Thanks!
> Alex
> 
> My gross method of making a MapReduceSpec:
>     final Map<String, Object> mapReduceObject = new HashMap<String, Object>();
>     final Map<String, Object> inputs = new HashMap<String, Object>();
>     final List<Map> query = new ArrayList<Map>();
>     final List<String> searchTerms = new ArrayList<String>();
>     searchTerms.add(bucket);
>     searchTerms.add(terms);
>     inputs.put("module", "riak_search");
>     inputs.put("function", "mapred_search");
>     inputs.put("arg", searchTerms);
>     final Map<String,String> reduce = new HashMap<String,String>();
>     reduce.put("language", "erlang");
>     reduce.put("module", "riak_kv_mapreduce");
>     reduce.put("function", "reduce_identity");
>     final Map<String,Map<String,String>> map1 = new 
> HashMap<String,Map<String,String>>();
>     map1.put("reduce", reduce);
>     query.add(map1);
>     mapReduceObject.put("inputs", inputs);
>     mapReduceObject.put("query", query);
>     final MapReduceSpec spec = new MapReduceSpec(new 
> Gson().toJson(mapReduceObject));

OK. So the high-level libraries are there to help, even if you have a 
RawClient, try this:

        IndexQuery q = new BinValueQuery(BinIndex.named("docUID"), "mentions", 
"5b9d1a6250dbd3e77ff004a12d06958745ee32844ad4aa668001bbe69b5efcf8");
        MapReduce mr = new IndexMapReduce(YOUR_RAW_CLIENT_INSTANCE, indexQuery);

        mr.addReducePhase(NamedErlangFunction.REDUCE_IDENTITY, 
"{reduce_phase_only_1, true}");
        MapReduceResult result = mr.execute();  
        Collection<String[]> r = result.getResult(String[].class);

You can do the same thing with the search map reduce

        MapReduce mr = new SearchMapReduce(YOUR_RAW_CLIENT, "bucket", "your 
search query");

And then use the MapReduce methods to add phases and execute as before.
_______________________________________________
riak-users mailing list
riak-users@lists.basho.com
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com

Reply via email to