[Recipes] [Recipes] Using DynamoDB SDK TableUtils Class And Its Utility Methods

Problem: 

DynamoDB Java APIs TableUtils class contains utility methods for working with DynamoDB tables. Experiment with it.

Solution Summary: 

We will rewrite the table creation using createTableIfNotExists method so that we don’t have to worry about if the table is already present or not.

Prerequisites: 

This is in continuation to the lab: Create Table, Load Data And Write Cleanup Code in Java Spring For DynamoDB

You don’t have to create DynamoDB object from the client object AmazonDynamoDB. However, you will need to retain it in current code as it is being used for loading data.

Solution Steps: 

Code:

    final AmazonDynamoDB amazonDynamoDB;

    final DynamoDB dynamoDB;

    @Autowired

    DefaultDataLoaderImpl(AmazonDynamoDB amazonDynamoDB){

        dynamoDB = new DynamoDB(amazonDynamoDB);

        this.amazonDynamoDB = amazonDynamoDB;

    }

 

Table Creation Code

  public void createTable() throws InterruptedException {

        CreateTableRequest createTableRequest = new CreateTableRequest(

                Arrays.asList(

                        new AttributeDefinition("Name", ScalarAttributeType.S),

                        new AttributeDefinition("Designation", ScalarAttributeType.S)),

                demoTableName,

                Arrays.asList(

                        new KeySchemaElement("Name", KeyType.HASH),  //Partition key

                        new KeySchemaElement("Designation", KeyType.RANGE)), //Sort key

                new ProvisionedThroughput(10L, 10L));

        System.out.println("Attempting to create table; please wait...");

        TableUtils.createTableIfNotExists(amazonDynamoDB, createTableRequest);

        try {

            TableUtils.waitUntilActive(amazonDynamoDB, demoTableName);

        } catch (AmazonClientException e) {

            System.out.println("Failed.");

            e.printStackTrace();

        }

    }

 

Other methods

TableUtils class has few other useful methods too such as:

  • deleteTableIfExists(AmazonDynamoDB dynamo, DeleteTableRequest deleteTableRequest)
  • waitUntilActive(AmazonDynamoDB dynamo, String tableName)
  • waitUntilActive(AmazonDynamoDB dynamo, String tableName, int timeout, int interval)
  • waitUntilExists(AmazonDynamoDB dynamo, String tableName)
  • waitUntilExists(AmazonDynamoDB dynamo, String tableName, int timeout, int interval)

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream