I am following this document:
My goal is to use AtomicCounter to generate unique IDs, but I always get null for both examples:
My code looks like this:
@DynamoDbBean
public class CustomerDocument {
private String root;
private Long updateCounter;
private Long customCounter;
@DynamoDbPartitionKey
public String getRoot() {
return this.root;
}
public void setRoot(String id) {
this.root = id;
}
@DynamoDbAtomicCounter
public Long getUpdateCounter() {
return this.updateCounter;
}
public void setUpdateCounter(Long counter) {
this.updateCounter = counter;
}
@DynamoDbAtomicCounter(delta = 5, startValue = 10)
public Long getCustomCounter() {
return this.customCounter;
}
public void setCustomCounter(Long counter) {
this.customCounter = counter;
}
}
repository
CustomerDocument document = new CustomerDocument();
document.setRoot(root);
customerTable.updateItem(document);
CustomerDocument retrievedCustomer = customerTable.getItem(document);
retrievedCustomer.getUpdateCounter(); // null
retrievedCustomer.getCustomCounter(); // null
customerTable.updateItem(document);
retrievedCustomer = orderCounterTable.getItem(document);
retrievedCustomer.getUpdateCounter(); // null
retrievedCustomer.getCustomCounter(); // null
any idea about the issue, please?
Edit
based on the answer above, my code now looks like this:
public CustomerDocument nextCounter(String root) {
CustomerDocument document = new CustomerDocument();
document.setId(root);
CustomerDocument item = orderCounterTable.getItem(document);
if (item == null) {
CustomerDocument defaultDocument = new CustomerDocument();
defaultDocument.setId(root);
defaultDocument.setCount(1L);
customerTable.putItem(defaultDocument);
return defaultDocument;
} else {
CustomerDocument newDocument = new CustomerDocument();
newDocument.setId(item.getId());
return customerTable.updateItem(newDocument);
}
}
But I still get null
every orderCounterTable.updateItem(newDocument)
:
I get only:
{
"id": "20042024"
}
2
Answers
Comparing my code and the code of @smac2020, I realise that I use
.extensions(VersionedRecordExtension.builder().build())
:By deleting this configuration, my solution works fine.
my final code was just:
Not
putItem
, nothing else.This functionality works as shown in the sample serverless photo asset management app that uses DynamoDbAtomicCounter.
In this example, the front end is a React app. The backend uses AWS SDK for Java V2. Each time the application finds a new label using Amazon Rekognition, the count column in the Amazon DynamoDB table is incremented by using DynamoDbAtomicCounter. Here is the React UI. You can see the count for each Label.
Here is the applicable code that makes it work.
This is the DynamoDB class that uses the necessary annotations – such as @DynamoDbAtomicCounter.
Here is a method where it is used with the Enhanced Client:
UPDATE
Note that there is a trick here – from the SDK team:
"I figured out what’s happening. It’s because we’re reusing the same object that we read from the db with ‘getItem’. If you create a new object and set the data, it works."
That may be the cause of your issue. Create a new object, set the data, and the increment will happen. Notice that i create a new object here:
You can find this complete example in the AWS Code Library. You can code the backend and use the AWS CDK to standup the resources and run this app. Follow the directions in the doc and follow the CDK instructions. Once you follow everything, you will see the app in action and the working DynamoDbAtomicCounter.
See:
Create a photo asset management application that lets users manage photos using labels
UPDATE ON YOUR CODE
I had to made a few changes to your code.
CODE:
EnhancedAtomicCounter
CustomerDocument
In my example, I made a few updates to the record by changing the name. Now
updateCounter should be 3. It is: