AWS Developer Tools Blog
DynamoDB Series – Expressions
For the final installment of our Amazon DynamoDB series, we are going to look at the new expression support. There are two types of expressions used by DynamoDB. First you can use expressions to update specific fields in an item. The other way is to use expressions on puts, updates, or deletes to prevent the operation from succeeding if the item in DynamoDB doesn’t meet the expression.
Update Expressions
Update expressions are great for atomic updates to attributes in an item in DynamoDB. For example, let’s say we add a player item to a DynamoDB table that records the number of games won or lost and the last time a game was played.
PutItemRequest putRequest = new PutItemRequest
{
TableName = tableName,
Item = new Dictionary<string, AttributeValue>
{
{"id", new AttributeValue{S = "1"}},
{"name", new AttributeValue{S = "Norm"}},
{"wins", new AttributeValue{N = "0"}},
{"loses", new AttributeValue{N = "0"}}
}
};
ddbClient.PutItem(putRequest);
When a player wins the game, we need to update the wins attribute and set the time the last game was played and who the opponent was. To do that, we could get the item and look up how many wins the player currently has and then update the wins with the current wins + 1. The tricky thing is what happens if there is an update to the item in between the get and the update. We can handle that by putting an ExpectedAttribute value on the update, which will cause the update to fail, and then we could retry the whole process.
Now, using expressions, we can increment the wins attribute without having to first read the value. Let’s look at the update call to see how that works.
UpdateItemRequest updateRequest = new UpdateItemRequest
{
TableName = tableName,
Key = new Dictionary<string, AttributeValue>
{
{"id", new AttributeValue{S = "1"}}
},
UpdateExpression = "ADD #a :increment SET #b = :date, #c = :opponent",
ExpressionAttributeNames = new Dictionary<string, string>
{
{"#a", "wins"},
{"#b", "last-played"},
{"#c", "last-opponent"}
},
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{":increment", new AttributeValue{N = "1"}},
{":date", new AttributeValue{S = DateTime.UtcNow.ToString("O")}},
{":opponent", new AttributeValue{S = "Celeste"}}
}
};
ddbClient.UpdateItem(updateRequest);
The TableName and Key properties are used to identify the item we want to update. The UpdateExpression property is the interesting property where we can see the expression that is run on the item. Let’s break this statement down by each token.
The ADD token is the command token, and for a numeric attribute it adds the specified value to the attribute. Next is the #a token, which is a variable. The ‘#’ means this variable will be replaced with an attribute name. :increment is another variable that is the value to be added to the attribute #a. All tokens that start with ‘:’ are variables that will have a value supplied in the update request.
SET is another command token. It means all the attributes following will have their value set. The #b variable will get its value from the :date variable, and #c will get is value from the :opponent variable.
It is also possible to remove an attribute using the REMOVE command token.
The ExpressionAttributeNames property is used to set all the attribute variables in the expression to the actual attributes we want to use. ExpressionAttributeValues property is used to set all the value variables to the values we want to use in the expression.
Once we invoke the update, DynamoDB guarantees that all the attributes in the expression will be updated at the same time without the worry of some other thread coming in and updating the item in the middle of the process. This also saves us from using up any of our read capacity to do the the update.
Check out the DynamoDB Developer Guide for more information on how to use update expressions.
Conditional Expressions
For Puts, Updates, and Deletes, a conditional expression can be set. If the expression evaluates to false, then a ConditionalCheckFailedException exception is thrown. On the low-level service client, this can be done using the ConditionExpression property. Conditional expressions can also be used on the Document Model API. To take a look how this is done, let’s first create a game document in our game table.
DateTime lastUpdated = DateTime.Now;
Table gameTable = Table.LoadTable(ddbClient, tableName, DynamoDBEntryConversion.V2);
Document game = new Document();
game["id"] = gameId;
game["players"] = new List<string>{"Norm", "Celeste"};
game["last-updated"] = lastUpdated;
gameTable.PutItem(game);
For the game’s logic, every time the game document is updated the last-updated attribute is checked to make sure it hasn’t changed since the document was retrieved and then updated to a new date. So first let’s get the document and update the winner.
Document game = gameTable.GetItem(gameId);
game["winner"] = "Norm";
game["last-updated"] = DateTime.Now;
To declare the conditional expression I need to create an Expression object.
var expr = new Expression();
expr.ExpressionStatement = "attribute_not_exists(#timestamp) or #timestamp = :timestamp";
expr.ExpressionAttributeNames["#timestamp"] = "last-updated";
expr.ExpressionAttributeValues[":timestamp"] = lastUpdated;
This expression evaluates to true if the last-updated attribute does not exist or is equal to the last retrieved timestamp. Then, to use the expression, assign it to the UpdateItemOperationConfig and pass it to the UpdateItem operation.
UpdateItemOperationConfig updateConfig = new UpdateItemOperationConfig
{
ConditionalExpression = expr
};
try
{
gameTable.UpdateItem(game, updateConfig);
}
catch(ConditionalCheckFailedException e)
{
// Retry logic
}
To handle the expression evaluating to false, we need to catch the ConditionalCheckFailedException and call our retry logic. To avoid having to catch exceptions in our code, we can use the new “Try” methods added to the SDK, which return true or false depending on whether the write was successful. So the above code could be rewritten like this:
if(!gameTable.TryUpdateItem(game, updateConfig))
{
// Retry logic
}
This same pattern can be used for Puts and Deletes. For more information about using conditional expressions, check out the Amazon DynammoDB Developer Guide.
Conclusion
We hope you have enjoyed our series on Amazon DynamoDB this week. Hopefully, you have learned some new tricks that you can use in your application. Let us know what you think either in the comments below or through our forums.