How to add a secondary index with the Java client

Jeff Peck jeffp at tnrglobal.com
Mon Apr 8 18:02:47 EDT 2013


Brian,

Thank you for explaining that and suggesting to extend HashMap. I did exactly that. Here is what it looks like:

class DocMap extends HashMap<String, Object> {
	/**
	 * Generated id
	 */
	private static final long serialVersionUID = 5807773481499313384L;
	
	@RiakIndex(name="status") private String status;

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}
}

I am about to try it, but I first need to make a few more changes in the code to adapt this new object. In the meantime, would you say that this looks correct and that it would be able to effectively write a status field to a secondary index if I were to use "setStatus"?

Thanks,
Jeff


On Apr 8, 2013, at 5:48 PM, Brian Roach <roach at basho.com> wrote:

> Jeff,
> 
> If you're just passing in an instance of the core Java HashMap ... you can't.
> 
> The way the default JSONConverter works for metadata (such as indexes)
> is via annotations.
> 
> The object being passed in needs to have a field annotated with
> @RiakIndex("index_name"). That field can be a Long/Set<Long> or
> String/Set<String> (for _int and _bin indexes respectively).
> 
> These are not converted to JSON so they won't affect your serialized
> data. You can have multiple fields for multiple indexes.
> 
> You don't have to append "_int" or "_bin" to the index name in the
> annotation - it's done automatically based on the type.
> 
> Easiest thing to do woud be to extend HashMap and simply add the
> annotated field(s).
> 
> Thanks,
> _Roach
> 
> On Mon, Apr 8, 2013 at 2:56 PM, Jeff Peck <jeffp at tnrglobal.com> wrote:
>> Hello,
>> 
>> I have been looking through the documentation for an example of how to add a secondary index in Riak, using the Java client.
>> 
>> I am currently storing my object (which is a HashMap) like this:
>> 
>> bucket.store(key, docHashMap).execute();
>> 
>> What would I need to do to add an index to that object before it gets stored?
>> 
>> Thanks,
>> Jeff
>> 
>> 
>> 
>> _______________________________________________
>> riak-users mailing list
>> riak-users at lists.basho.com
>> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com





More information about the riak-users mailing list