JSON Serializer for arbitrary HashMaps in Voldemort

烂漫一生 提交于 2019-12-23 19:27:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!