我有一个带有“计数”数字字段的 DynamoDB 表。我使用 AttributeAction.ADD 增加该字段。它可以工作,但是当我重新运行我的应用程序时,它不会增加现有值,而是再次从“1”开始。
我期待:
run #1: 1,2,3,4,5
run #2: 6,7,8,9,10
相反,我看到:
run #1: 1,2,3,4,5
run #2: 1,2,3,4,5
我做错了什么?这是我在应用程序中运行的代码:
Map<String, AttributeValueUpdate> updateItems = new HashMap<String, AttributeValueUpdate>();
Key key = new Key().withHashKeyElement(new AttributeValue().withS(targetKey));
updateItems.put("count",
new AttributeValueUpdate()
.withAction(AttributeAction.ADD)
.withValue(new AttributeValue().withN("1")));
ReturnValue returnValues = ReturnValue.ALL_NEW;
UpdateItemRequest updateItemRequest = new UpdateItemRequest()
.withTableName(tableName)
.withKey(key)
.withAttributeUpdates(updateItems)
.withReturnValues(returnValues);
UpdateItemResult result = dynamoDB.updateItem(updateItemRequest);
最佳答案
代码运行良好。我测试了另一个应用程序没有任何问题。我今天重新运行了我的第一个应用程序,它再次运行良好。没有代码更改,因为它驻留在共享 jar 中。我猜测最初在我第一次运行时创建的表存在问题。我不确定什么,因为表创建代码位于同一个 jar 内并且也没有更改。
关于java - 递增的 DynamoDB 编号字段未保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15369027/