Insert Example in DynamoDB Mapper

Trying to insert items into your table using DynamoDB Mapper? This is the article for you.

This is a continuation of my DynamoDBMapper series. Last time we talked about DynamoDB Query operations. In this post, we’re going to talk about the Save operation which behind the scenes uses the PutItem API.

So lets get started.

Note that the save option in DynamDB is equivalent to an insertion as well.

Model Class

Keep in mind you need to model your insertion object with the correct annotations in order for this to work. Also note I’m using Lombok for Getter/Setter/Constructor generation.

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

@ToString
@NoArgsConstructor
@Getter
@Setter
@DynamoDBTable(tableName = "CustomerOrders")
public class CustomerOrder {
    @DynamoDBHashKey(attributeName = "CustomerID")
    private String customerID;

    @DynamoDBRangeKey(attributeName = "OrderID")
    private String orderID;

    @DynamoDBAttribute(attributeName = "OrderAddress")
    private String orderAddress;

    @DynamoDBAttribute(attributeName = "OrderTotal")
    private Double orderTotal;
}

Insertion Code

After you’ve established your model, you can use this code as a reference to save your items to DynamoDB.

        //Specify credential details
        AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(
                new BasicAWSCredentials(System.getenv("ACCESS_KEY"),
                                        System.getenv("SECRET_ACCESS_KEY")));

        //Create client
        AmazonDynamoDB ddbClient = AmazonDynamoDBClientBuilder.standard()
                .withCredentials(credentials)
                .withRegion("us-east-1") //Remember to change your region!
                .build();

        DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);

        CustomerOrder customerOrder = new CustomerOrder();
        customerOrder.setCustomerID("CUSTOMER3");
        customerOrder.setOrderID("5");

        mapper.save(customerOrder,
                    new DynamoDBMapperConfig(DynamoDBMapperConfig.ConsistentReads.CONSISTENT));

The mapper.save is a void method and unfortunately there’s no way of confirming your save succeeded beyond querying for the item.

Also of note is the fact that I passed in a DynamoDBMapperconfig instance with a ConsistentReads value set to CONSISTENT.

This save setting ensures that before DynamoDB returns your call as 200OK, it will ensure that behind the scenes the data has been copied to a majority of nodes in the cluster. This has to do with the way DynamoDB replicates copies of your across machines.

This save behaviour is desirable in scenarios where consistency matters. Things like banking applications.

This added plus does not come for free though. There is an extra charge (approx double last time I checked) for using this behaviour, so tread carefully.

Not specifying any option for the mapper config will default to the normal mode which only ensures one of the nodes in the cluster has committed the insert / save.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts