本文介绍了用于Voldemort中任意HashMaps的JSON序列化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置Voldemort键值存储的配置。现在,我希望能够存储任意hashmaps,但我还没有找到这样做的方法(或者如果可能的话)。



根据文档,我应该使用以下语法:

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

我想存储一个Java bean的HashMap表示,但是对允许的键有限制(只有 fname lname id 电子邮件)及其类型。

我需要的是能够存储一个像这样的任意地图:

  {name:fred, ID:15} 

或者像这样:

  {apples:50 $,oranges:15€} 

(地图数值没有意义,仅仅是带有不同关键字名称和值类型的地图的示例)

有没有办法定义一个可以接受的模式一个任意的散列表?

解决方案

我发现有用的是只使用通用原始存储(通过byte [])并在客户端预先序列化事物。这是微不足道的(我使用):

  ObjectMapper mapper = new ObjectMapper(); 
//存储:
byte [] data = mapper.writeValueAsBytes(myMap);
//以及取回时:
Map< String,Object> data = mapper.readValue(data,Map.class);

虽然Voldemort确实支持其类似JSON的存储(它不是JSON,而是简单的二进制字典,据我所知,使用它没有多大好处,因为你无法查询它或请求文档的子集。


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?

解决方案

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.

这篇关于用于Voldemort中任意HashMaps的JSON序列化程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 00:14