2013-03-12 157 views
1

我有一個帶有'count'數字字段的DynamoDB表。我正在使用AttributeAction.ADD增加字段。它可以工作,但是當我重新運行我的應用程序時,它不會增加現有值,而是再次從「1」開始。Incrementing DynamoDB數字字段不被持久

我期待:

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); 
+0

代碼看起來很好。什麼是targetKey?也許它是不同的? – 2013-03-13 16:07:45

+0

targetKey是表格的主要元素。它只是一個傳遞給此代碼的變量。 – TERACytE 2013-03-13 19:46:50

回答

0

代碼工作正常。我測試了另一個應用程序沒有任何問題。我今天重新運行了我的第一個應用程序,它再次正常工作。沒有代碼更改,因爲它駐留在共享jar中。我在猜測,這個表格最初是在第一次運行時創建的。我不知道什麼,因爲表創建代碼是在同一個罐子裏,也沒有改變。