Java – how do I update a map or list on the AWS dynamodb documentation API?

The new AWS dynamodb document API allows two new data types to directly correspond to the basic JSON representation: Map (also known as JSON object) and list (also known as JSON array)

However, I did not find a way to update the properties of these data types without completely overwriting them Instead, you can update the number property by adding another number, so you can do the following in Java:

new AttributeUpdate("Some numeric attribute").addNumeric(17);

Similarly, you can set addelements to set the properties of the data type (in the old API, you will use attributeaction.add for both purposes.)

However, for maps or lists, it seems that you must update the previous value locally and replace it with that value, for example, in Java:

List<String> list = item.getList("Some list attribute");
list.add("new element");
new AttributeUpdate("Some list attribute").put(list);

This is much less readable and, in some cases, less efficient

So my question is:

>Is there any way to update the properties of map or list data types without overwriting the previous values? For example, to add an element to a list, or to place an element in a map? > How do I implement it using the Java API? > Do you know the future supports this plan?

Solution

Please check the updateexpression in the updateitem API

For example, given an item that contains a list:

{
    "hashkey": {"S" : "my_key"},"my_list" : {"L": 
        [{"N":"3"},{"N":"7"} ]
}

You can update the list with the following code:

UpdateItemRequest request = new UpdateItemRequest();
request.setTableName("myTableName");
request.setKey(Collections.singletonMap("hashkey",new AttributeValue().withS("my_key")));
request.setUpdateExpression("list_append(:prepend_value,my_list)");
request.setExpressionAttributeValues(
    Collections.singletonMap(":prepend_value",new AttributeValue().withN("1"))
    );
dynamodb.updateItem(request);`

You can also reverse the list_ Append to the list in the order of the parameters in the append expression

An expression, such as set user address. Zipcode =: zip will parse JSON mapping elements and expression attribute values {": zip": {"n": "12345"}}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>