如何在AWS DynamoDB文档API上更新地图或列表? [英] How to update a Map or a List on AWS DynamoDB document API?
问题描述
新的 AWS DynamoDB文档API 允许2种直接对应于基础JSON表示的新数据类型:Map(又名JSON对象)和List(又名JSON数组)。
The new AWS DynamoDB document API allows 2 new data types that correspond directly to the underlying JSON representation: Map (aka JSON object) and List (aka JSON array).
但是,我找不到更新这些数据类型的属性而不完全覆盖它们的方法。相反,可以通过添加另一个数字来更新Number属性,因此在Java中您可以执行以下操作:
However, I can't find a way to update attributes of these data types without completely overwriting them. In contrast, a Number attribute can be updated by ADDing another number, so in Java you can do something like:
new AttributeUpdate("Some numeric attribute").addNumeric(17);
同样可以 addElements 指向Set数据类型的属性。 (在旧的API中,您将使用AttributeAction.ADD用于两种目的。)
Similarly you can addElements to an attribute of a Set data type. (In the old API you would use AttributeAction.ADD for both purposes.)
但是对于Map或List,似乎您必须在本地更新先前的值,然后输入它而不是该值,例如在Java中:
But for a Map or a List, it seems you must update the previous value locally, then PUT it instead of 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 under some circumstances much less efficient.
所以我的问题是:
-
有没有办法更新地图的属性或列表数据类型没有覆盖以前的值?例如,要向List添加元素,还是将元素放在Map中?
Is there a way to update an attribute of a Map or a List data type without overwriting the previous value? For example, to add an element to a List, or to put an element in a Map?
您将如何使用Java API实现它?
How would you implement it using the Java API?
你知道将来支持这项计划吗?
Do you know of plans to support this in the future?
推荐答案
请查看 UpdateItem API
例如,给定一个带有列表的项目:
For example given an item with a list:
{
"hashkey": {"S" : "my_key"},
"my_list" : {"L":
[{"N":"3"},{"N":"7"} ]
}
您可以使用以下代码更新列表:
You can update the list with code like the following:
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);`
您还可以通过倒车追加到列表中list_append表达式中参数的顺序。
You can also append to the list by reversing the order of the arguments in the list_append expression.
表达式如下: SET user.address.zipcode =:zip
将解决结合表达式属性值的JSON地图元素 {:zip:{N:12345}}
An expression like: SET user.address.zipcode = :zip
would address a JSON map element combined with expression attribute values {":zip" : {"N":"12345"}}
这篇关于如何在AWS DynamoDB文档API上更新地图或列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!