On Sat, Dec 8, 2012 at 2:12 AM, David Montgomery
<[email protected]> wrote:
> Given that map reduce is the primary way of getting data out of riak
Hi, David. MapReduce is not the primary way of getting data out of
Riak. Primary-key reads serve that purpose. Even for reading indexes,
there are built-in interfaces (/bucket/B/index/I/... on HTTP, and the
RpbIndexReq message on Protocol Buffers as of Riak 1.2). MapReduce is
an auxiliary interface that can be used effectively in some situations
to augment these other facilities.
> How do I add a index for country=US and da>201207 and da<201212?
This is a limitation of Riak's secondary indexes: you're only able to
query one index at a time. To perform the query you describe, you have
two options: create a unified index, or query by one index and filter
by the other.
To create a unified index, you would add another field, 'countryda',
and then set values there that are concatenations of the other fields,
like 'US-201209'. Then your query would be "countryda >= US-201207 and
countryda =< US-201212". Using the Python client:
client.get_index('my_bucket', 'countryda_bin', 'US-201207', 'US-201212')
To query by one and filter by the other, you would look through all
the results of, for example, country=US, and then discard any entries
where da does not fall within your range. In MapReduce terms, that
might look like:
client.index('my_bucket', 'country_bin', 'US')
.map("function(v, kd, arg) {
// find the metadata value stored with the key
var da = v.values[0].metadata.index.da_bin;
// we passed the range in the arg so we can
// reuse this same function for any range
if (da > arg.min && da < arg.max)
return [v.key]; // in range - keep result
else
return []; // out of range - discard result
}",
{"arg":{"min":"201207", "max":"201212"}})
.run()
Hope that helps,
Bryan
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com