I am new to DynamoDB and have tested a sample script using Transaction support. I use the same object provided in the dynamodb-transaction library. The only difference is that I added a range key with a hash key. Here is the table definition:
ItemId hash key, stringItemName range key, string
@DynamoDBTable(tableName = "Item") public static class ExampleItem { private String itemId; private String value; private String itemName; private Long version; @DynamoDBHashKey(attributeName = "ItemId") public String getItemId() { return itemId; } public void setItemId(String itemId) { this.itemId = itemId; } @DynamoDBRangeKey(attributeName="ItemName") public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } @DynamoDBVersionAttribute public Long getVersion() { return version; } public void setVersion(Long version) { this.version = version; } }
As you can see, I also use the version attribute. Now I am trying to execute a simple create or update script using transaction / dbmapper. Here's a snippet of code.
Transaction t1 = txManager.newTransaction(); ExampleItem keyItem = new ExampleItem(); keyItem.setItemId("Item1"); keyItem.setItemName("USA"); ExampleItem item = t1.load(keyItem); if (item != null) { item.setValue("Magenta"); item.setItemName("UK"); t1.save(item); } else { item = new ExampleItem(); item.setItemId(keyItem.getItemId()); item.setItemName("USA"); item.setValue("Violet"); t1.save(item); } t1.commit(); t1.delete();
I can add the record without any problems, but I had a problem when I try to read the record and update any attribute. I get the following exception:
Uncaught exception:com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: Item {ItemId={S: Item1,}, ItemName={S: UK,}} had unexpected attributes: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: expected attribute(s) {version={Value: {N: 1,},Exists: true}} but found null com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: Item {ItemId={S: Item1,}, ItemName={S: UK,}} had unexpected attributes: Status Code: 0, AWS Service: null, AWS Request ID: null, AWS Error Code: null, AWS Error Message: expected attribute(s) {version={Value: {N: 1,},Exists: true}} but found null
It seems like this has something to do with the version, but don't know where I am going wrong. Any pointers would be appreciated.
-Thanks
Shamik
java amazon-web-services amazon-dynamodb aws-sdk
Shamik
source share