1

How can I use the sorting functions over 'real' fields in the stored objects? How can I set the field on which I want to sort by?

It seems that the following refers to the key, in my case it's a UUID, and therefore useless to sort by:

            MapReduceResult result = riakClient.
                            mapReduce("some_bucket").
                            addMapPhase(new NamedJSFunction("Riak.mapValuesJson"), false).
                            addReducePhase(new NamedErlangFunction("riak_kv_mapreduce", "reduce_sort"), true).
                            execute();

thanks.

o'mac
  • 99
  • 5

2 Answers2

1

There is a JavaScript reduce function for sorting by field available on the Basho website which you might be able to use. If the structure of the document you want to sort is too complicated for this, you may need to define your own reduce function in JavaScript.

Christian Dahlqvist
  • 1,665
  • 12
  • 9
  • Thanks Christian, I saw that before. what I don't understand is how do I use it from the java client. this doesn't work: addReducePhase(new NamedJSFunction("Contrib.sort", { by: 'passengers', order: 'desc' }), true); becuse the NamedJSFunction method expects 2 String params. my document structure is not complecated - its a pojo. – o'mac Sep 16 '12 at 07:29
  • In order to be able to use the function by name and not pass it through the mapreduce query, the function needs to be made available on all riak nodes. This can be done by defining the parameter 'js_source_dir' in the app.config file as described [here](https://help.basho.com/entries/321385-how-do-i-load-3rd-party-javascript-libraries-for-use-in-map-reduce-functions). You will need to run 'bin/riak-admin js_reload' in order for the functions to become available. – Christian Dahlqvist Sep 16 '12 at 15:46
0

The other option, if you find sorting via MapReduce too cumbersome, is -- sort on the client side.

If your result set is not so huge that you can't fit it into memory (although if it is, you may have other problems, in terms of MapReduce) -- just sort it in java, in memory, when it comes back.

Dmitri Zagidulin
  • 1,117
  • 9
  • 23