java DynamoDB - 对象到属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27500749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DynamoDB - Object to AttributeValue
提问by BPm
I'm aware of DynamoDBMapper but in my case I can't use it because I don't know all the attributes beforehand.
我知道 DynamoDBMapper,但就我而言,我无法使用它,因为我事先不知道所有属性。
I have a JSON and it's parsed to a map of objects by using Hymanson
parser:
我有一个 JSON,它使用Hymanson
解析器解析为对象映射:
Map<String, Object> userData = mapper.readValue(new File("user.json"), Map.class);
Looping through each attribute, how can I convert the value to AttributeValue
given that DynamoDB AttributeValue
supports Boolean, String, Number, Bytes, List, etc.
循环遍历每个属性,AttributeValue
如果 DynamoDBAttributeValue
支持布尔值、字符串、数字、字节、列表等,我如何将值转换为
Is there an efficient way to do this? Is there a library for this already? My naive approach is to check if each value is of type Boolean/String/Number/etc. and then call the appropriate AttributeValue
method, e.g: new AttributeValue().withN(value.toString())
- which gives me long lines of if, else if
有没有一种有效的方法来做到这一点?已经有图书馆了吗?我天真的方法是检查每个值是否属于布尔/字符串/数字/等类型。然后调用适当的AttributeValue
方法,例如:new AttributeValue().withN(value.toString())
- 这给了我很长的行if, else if
回答by BPm
Finally figured out by looking at how AWS parses the JSON
Basically, this is the code:
基本上,这是代码:
Item item = new Item().withJSON("document", jsonStr);
Map<String,AttributeValue> attributes = InternalUtils.toAttributeValues(item);
return attributes.get("document").getM();
Very neat.
非常整洁。
回答by Harsh
Following is a simple solution which can be applied to convert any DynamoDB Json to Simple JSON.
以下是一个简单的解决方案,可用于将任何 DynamoDB Json 转换为简单 JSON。
//passing the reponse.getItems()
public static Object getJson(List<Map<String,AttributeValue>> mapList) {
List<Object> finalJson= new ArrayList();
for(Map<String,AttributeValue> eachEntry : mapList) {
finalJson.add(mapToJson(eachEntry));
}
return finalJson;
}
//if the map is null then it add the key and value(string) in the finalKeyValueMap
public static Map<String,Object> mapToJson(Map<String,AttributeValue> keyValueMap){
Map<String,Object> finalKeyValueMap = new HashMap();
for(Map.Entry<String, AttributeValue> entry : keyValueMap.entrySet())
{
if(entry.getValue().getM() == null) {
finalKeyValueMap.put(entry.getKey(),entry.getValue().getS());
}
else {
finalKeyValueMap.put(entry.getKey(),mapToJson(entry.getValue().getM()));
}
}
return finalKeyValueMap;
}
This will produce your desired Json in the form of List<Map<String,Object>>
which is subset of the object
.
这将生成您想要的 Json,其形式List<Map<String,Object>>
是object
.
回答by Jay Patel
I used HymansonConverterImplto convert JsonNode
to Map<String, AttributeValue>
我使用HymansonConverterImpl转换JsonNode
为Map<String, AttributeValue>
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readValue(jsonString, JsonNode.class);
final HymansonConverter converter = new HymansonConverterImpl();
Map<String, AttributeValue> map = converter.jsonObjectToMap(jsonNode);
Hope this helps!
希望这可以帮助!
Thanks, Jay
谢谢,杰