Search code examples
javascalaindexingriak

Riak: Create index on key via Java/Scala


I have a bucket on riak in which I store simple Timestamp -> String values in this way:

val riakClient = RiakFactory.newClient(myHttpClusterConfig)
val myBucket = riakClient.fetchBucket(name).execute
myBucket.store(timestamp.toString, value).withoutFetch().w(1).execute

What I need to do now is to add an index on the keys. I tried defining a Java POJO in this way:

public class MyWrapper {
    @RiakIndex(name="timestamp_index")
    @RiakKey
    public String timestamp;
    public String value;

    public MyWrapper(String timestamp, String value) {
        this.timestamp = timestamp;
        this.value = value;
    }
}

and then running

myBucket.store(new MyWrapper(timestamp.toString, value)).withoutFetch().w(1).execute

The problem of this approach is that in riak the actual value is stored as a json object:

{"value":"myvalue"}

while I would simply need to store the myvalue string. Is there any way to achieve this? I can't see any index(name) method when executing store, and I can't see any annotations like @RiakKey but for values.


Solution

  • You can create a RiakObject using a RiakObjectBuilder, and then add the index on that:

    val obj = RiakObjectBuilder.newBuilder(bucketName, myKey)
          .withValue(myValue)
          .addIndex("timestamp_index", timestamp)
          .build
    myBucket.store(obj).execute