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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:54:06  来源:igfitidea点击:

DynamoDB - Object to AttributeValue

javaamazon-dynamodb

提问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 Hymansonparser:

我有一个 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 AttributeValuegiven that DynamoDB AttributeValuesupports 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 AttributeValuemethod, 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

通过查看AWS如何解析 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 JsonNodeto Map<String, AttributeValue>

我使用HymansonConverterImpl转换JsonNodeMap<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

谢谢,杰