java – DynamoDB API:如何构建“添加JSON属性(如果不存在”)更新请求?

我试图使用新的 Amazon DynamoDB JSON API在名为“document”的JSON属性添加/覆盖键值对.理想情况下,我想简单地构造我的写调用来发送KV对以添加属性,并且如果给定的主键不存在,那么Dynamo会创建该属性.但是,如果我尝试这个只是一个简单的UpdateItemSpec:
PrimaryKey primaryKey = new PrimaryKey("key_str","mapKey");
ValueMap valuesMap = new ValueMap().withLong(":a",1234L).withLong(":b",1234L);
UpdateItemSpec updateSpec = new UpdateItemSpec().withPrimaryKey(primaryKey).withUpdateExpression("SET document.value1 = :a,document.value2 = :b");
updateSpec.withValueMap(valuesMap);
table.updateItem(updateSpec);

我得到com.amazonaws.AmazonServiceException:更新表达式中提供的文档路径对于更新是无效的,意味着DynamoDB找不到要应用更新的名为“document”的给定属性.

我通过以下一系列呼叫设法近似该功能

try {
    // 1. Attempt UpdateItemSpec as if attribute already exists
} catch (AmazonServiceException e) {
    // 2. Confirm the exception indicated the attribute was not present,otherwise rethrow it
    // 3. Use a put-if-absent request to initialize an empty JSON map at the attribute "document"
    // 4. Rerun the UpdateItemSpec call from the above try block
}

这是有效的,但不太理想,因为每次在表中添加新的主键时,都需要3次调用DynamoDB.我尝试了一些可以在Update Expressions中使用的attribute_not_exists函数,但是无法按照我想要的方式工作.

任何发电机专家在这里有什么想法可以做到吗?

解决方法

我收到了亚马逊支持一个答案,实际上是不可能通过一个电话来完成这个.他们建议通过在put-if-absent请求中使用所需的JSON映射而不是空的映射,将新的主键的属性从3添加到2来减少呼叫次数.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...