Store and Query App Data in DynamoDB

Amazon DynamoDB is a fast, highly scalable, highly available, cost-effective, non-relational database service. DynamoDB removes traditional scalability limitations on data storage while maintaining low latency and predictable performance.

The tutorial below explains how to integrate the DynamoDB ObjectMapper with your app, which stores Java objects in DynamoDB.

Project Setup#


You must complete all of the instructions on the Set Up the SDK for Android page before beginning this tutorial.

Create a DynamoDB Table#

Before you can read and write data to a DynamoDB database, you must create a table. When creating a table you must specify the primary key. The primary key is composed of a partition key attribute and an optional sort key attribute. For more information on how primary and sort attributes are used, see Working With Tables.

  1. To invoke the wizard, navigate to the DynamoDB Console and choose Create Table.

  2. For Table name type Books.

  3. In Primary key, for Partition key, type Author for the partition key value and choose String for the key type.

  4. Choose Add sort key.

  5. In Add sort key, type Title for the sort key value and choose String for the key type.

  6. Keep Use default settings selected and click Create.

Update IAM Roles#

In order for your Cognito identity pool to access Amazon DynamoDB, you must modify the identity pool's roles.

  1. Navigate to the Identity and Access Management Console and click Roles in the left-hand pane and search for your Identity Pool name - two roles will be listed one for unauthenticated users and one for authenticated users.

  2. Click the role for unauthenticated users (it will have "unauth" appended to your Identity Pool name) and click the Create Role Policy button.

  3. Select Policy Generator and click the Select button.

  4. In the Edit Permissions page enter the settings shown in the following image. The Amazon Resource Name (ARN) of a DynamoDB table looks like arn:aws:dynamodb:us-west-2:123456789012:table/my-table-name and is composed of the region in which the table is located, the owner's AWS account number, and the name of the table in the format table/my-table-name. For more information about specifying ARNs, see Amazon Resource Names for DynamoDB.

  5. Click the Add Statement button, click the Next Step button and the Wizard will show you the configuration generated.

  6. Click the Apply Policy button.

Add Import Statements#

Add the following imports to the main activity of your app:

import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.*;

Initialize AmazonDynamoDBClient#

Pass your initialized Amazon Cognito credentials provider to the AmazonDynamoDB constructor:

AmazonDynamoDBClient ddbClient = new AmazonDynamoDBClient(credentialsProvider);

Initialize DynamoDBMapper#

Pass your initialized DynamoDB client to the DynamoDBMapper constructor:

DynamoDBMapper mapper = new DynamoDBMapper(ddbClient);

Write a Row#

To write a row to the table, define a class to hold your row data. This class must be derived from AWSDynamoDBModel and implement the AWSDynamoDBModel interface. The class should also contain properties that hold the attribute data for the row. The following class declaration illustrates such a class:

@DynamoDBTable(tableName = "Books")
    public class Book {
        private String title;
        private String author;
        private int price;
        private String isbn;
        private Boolean hardCover;

        @DynamoDBIndexRangeKey(attributeName = "Title")
        public String getTitle() {
            return title;

        public void setTitle(String title) {
            this.title = title;

        @DynamoDBIndexHashKey(attributeName = "Author")
        public String getAuthor() {
            return author;

        public void setAuthor(String author) {
   = author;

        @DynamoDBAttribute(attributeName = "Price")
        public int getPrice() {
            return price;

        public void setPrice(int price) {
            this.price = price;

        @DynamoDBHashKey(attributeName = "ISBN")
        public String getIsbn() {
            return isbn;

        public void setIsbn(String isbn) {
            this.isbn = isbn;

        @DynamoDBAttribute(attributeName = "Hardcover")
        public Boolean getHardCover() {
            return hardCover;

        public void setHardCover(Boolean hardCover) {
            this.hardCover = hardCover;

To save an object, first create it and set the appropriate fields:

Book book = new Book();
book.setTitle("Great Expectations");
book.setAuthor("Charles Dickens");

Then save the object:;

To update a row, modify the instance of the DDTableRow class and call as shown above.

Retrieve a Row#

Retrieve an object using a primary key:

Book selectedBook = mapper.load(Book.class, "1234567890");

For more information on accessing DynamoDB from an Android application, see Amazon Dynamo DB.