问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/4356854/json-serializer-for-arbitrary-hashmaps-in-voldemort