0

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.

StefanoP
  • 3,798
  • 2
  • 19
  • 26

2 Answers2

1

If I understand what you are trying to do, you want the key/value pair "1403909549"/"Some Value" to be indexed by timestamp_index="1403909549" so that you can query specific times or ranges of times.

If that is the case, you do not need to explicitly add an index, you can query the implicit index $KEY in the same manner you would any other index.

Since all keys that Riak stores in LevelDB are indexed implicitly, I don't think a method was exposed to index them again explicitly.

Joe
  • 25,000
  • 3
  • 22
  • 44
  • Hey Joe, thank you for your answer, I didn't know of the implicit `$KEY` index, +1 for that. Your solution would work if I had to store only these `Timestamp -> String` values in `myBucket`, but unfortunately I have other keys with a different semantics that I don't want to keep into consideration when querying on `timestamp_index` – StefanoP Jun 29 '14 at 07:51
1

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
vad
  • 1,196
  • 9
  • 22