aws:
accessKey : <your-aws-access-key>
secretKey : <your-aws-secret>
region : <dynamo-db-region>
dynamodb :
endpoint:
What are Conditional Writes in AWS DynamoDB
Upasana | August 12, 2019 | 2 min read | 1,586 views | Amazon DynamoDB AWS Tutorials
By default, the DynamoDB write operations (PutItem, UpdateItem, DeleteItem) are unconditional: each of these operations will overwrite an existing item that has the specified primary key. DynamoDB provides mechanism to implement conditional writes that ensures no other write request interferes with the current operation. Conditional writes are of great importance for banking application that do not tolerate over-counting or under-counting.
First of all we need to configure properties for accessing dynamoDB ./src/main/resources/application.yml
Step 2. Configure DynamoDB
We need to create a bean for AmazonDynamoDBClient
and DynamoDB
so that we can utilize them as and when required.
@Configuration
public class DynamoConfig {
private static final Logger logger = LoggerFactory.getLogger(DynamoConfig.class);
@Bean
AmazonDynamoDBClient dbClient(@Value("${aws.dynamodb.endpoint}") String amazonDynamoDBEndpoint, AWSSettings awsSettings) {
AmazonDynamoDBClient dbClient = new AmazonDynamoDBClient(
new BasicAWSCredentials(awsSettings.getAccessKey(), awsSettings.getSecretKey()));
dbClient.setRegion(Region.getRegion(Regions.fromName(awsSettings.getRegion())));
logger.info("DB Endpoint is " + amazonDynamoDBEndpoint);
if (!StringUtils.isEmpty(amazonDynamoDBEndpoint)) {
dbClient.setEndpoint(amazonDynamoDBEndpoint);
}
if (awsSettings.getRegion().equalsIgnoreCase("local")) {
dbClient.setEndpoint("http://localhost:8000");
}
return dbClient;
}
@Bean
DynamoDB dynamoDB(AmazonDynamoDBClient client) {
return new DynamoDB(client);
}
}
Step 3. Create DAO layer
DAO layer for condtional writes utlizes UpdateExpression
and ConditionExpression
as shown in the below code.
public void incrementViewsUsingConditionalExpression(String contentId, long newValue, long expected) {
Table table = dynamoDB.getTable("local-content");
UpdateItemSpec updateItemSpec = new UpdateItemSpec()
.withPrimaryKey("id", contentId)
.withUpdateExpression("SET #fn = :newval")
.withConditionExpression("#fn = :currval")
.withNameMap(new NameMap()
.with("#fn", "views")
)
.withValueMap(new ValueMap()
.withNumber(":newval", newValue)
.withNumber(":currval", expected)
)
.withReturnValues(ReturnValue.ALL_NEW);
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
System.out.println(outcome.getItem().toJSONPretty());
}
The above code will only update attribute named views
to newval
if expected value is equal to currval
.
That’s all
Download Source
You can download source code for this article from Git Repository
https://github.com/cancerian0684/dynamodb-atomic-updates
Amazon DynamoDB:
- What are Best Practices for Using Amazon DynamoDB?
- How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
Top articles in this category:
- How to automatically Retry DynamoDB Write on ProvisionedThroughputExceededException
- AWS DynamoDB Java interview questions
- How will you ensure that no two threads update the same db record in parallel in amazon DynamoDB
- What are Best Practices for Using Amazon DynamoDB?
- What is Eventual Consistency in DynamoDB?
- How to implement Atomic Counters in DynamoDB for high throughput
- Scan all records of a Amazon DynamoDB table using a Java Code