新的 AWS DynamoDB 文档 API 允许直接对应于底层 JSON 表示的 2 种新数据类型:地图 (又名 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,您似乎必须在本地更新以前的值,然后 PUT 代替那个值,例如在 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.
所以我的问题是:
有没有办法更新 Map 或 List 数据类型的属性而不覆盖以前的值?例如,将元素添加到 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 上的地图或列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!