3

I am trying to set up the configuration for Voldemort key-value store. Now, I'd like to be able to store arbitrary hashmaps in it, but I haven't found the way to do so (or if it is possible).

According to the documentation I should use this syntax:

{"fname":"string", "lname":"string", "id":"int32", "emails":["string"]}

To indicate that I want to store a HashMap representation of a Java bean, but with constraints on allowed keys (only fname,lname,id and emails) and their types.

What I would need is to be able to store an arbitrary map like this:

{"name":"fred", "id":15}

or like this:

{"apples":"50$", "oranges":"15€"}

(Map values are meaningless, just an illustration of maps with different key names, and value types)

Is there a way to define a schema that would accept an arbitrary hashmap?

Goran Jovic
  • 9,418
  • 3
  • 43
  • 75

1 Answers1

3

What I have found useful is to just use generic raw storage (where byte[] is passed), and pre-serializing things on client side. This is trivial to use (I use Jackson):

  ObjectMapper mapper = new ObjectMapper();
  // to store:
  byte[] data = mapper.writeValueAsBytes(myMap);
  // and when retrieving back:
  Map<String,Object> data = mapper.readValue(data, Map.class);

While Voldemort does support its "JSON-like" storage (it's not JSON but simple binary dictionaries, as far as I know), there isn't much benefit to using it in my opinion, since you can not query on it or request subsets of document.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • This seems quite straightforward. Just to make sure - this `byte[]` is a JSON string representation, not a serialized Java object, right? – Goran Jovic Dec 06 '10 at 19:33
  • We are probably using different versions of of Jackson. In 1.6.3 `writeValueAsString` returns a `String`, and `readValue` for `byte[]` has some extra params. Anyway, I tried your approach and it works like a charm! Thanks.. – Goran Jovic Dec 06 '10 at 20:11
  • Ah, my bad -- it should be "writeValueAsBytes()" instead. Glad to hear it works; this is a common use case, although I haven't used it with V myself yet. – StaxMan Dec 07 '10 at 00:21