Amazon DynamoDB
Developer Guide (API Version 2012-08-10)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Working with Tables Using the AWS SDK for Java Low-Level API

You can use the AWS SDK for Java low-level API (protocol-level API) to create, update, and delete tables, list all the tables in your account, or get information about a specific table. These operations map to the corresponding Amazon DynamoDB API. For more information, see Using the Amazon DynamoDB API.

The following are the common steps for table operations using the Java low-level API.

  1. Create an instance of the AmazonDynamoDBClient class (the client).

  2. Provide the required and optional parameters for the operation by creating the corresponding request objects.

    For example, create a CreateTableRequest object to create a table and an UpdateTableRequest object to update an existing table.

  3. Execute the appropriate method provided by the client that you created in the preceding step.

Creating a Table

To create a table, you must provide the table name, its primary key, and the provisioned throughput values. For more information, see Specifying Read and Write Requirements for Tables. The following Java code snippet creates an example table that uses a numeric type attribute Id as its primary key.

The following are the steps to create a table using the Java low-level API.

  1. Create an instance of the AmazonDynamoDBClient class (the client).

  2. Create an instance of the CreateTableRequest class to provide the request information.

    You must provide the table name, its primary key, and the provisioned throughput values.

  3. Execute the createTable method by providing the request object as a parameter.

The following Java code snippet demonstrates the preceding steps. The snippet creates a table (ProductCatalog) that uses Id as the primary key and set of provisioned throughput values. Depending on your application requirements, you can update the provisioned throughput values by using the updateTable method.

ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>();
attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
        
ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
ks.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
  
ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
    .withReadCapacityUnits(10L)
    .withWriteCapacityUnits(10L);
        
CreateTableRequest request = new CreateTableRequest()
    .withTableName(tableName)
    .withAttributeDefinitions(attributeDefinitions)
    .withKeySchema(ks)
    .withProvisionedThroughput(provisionedThroughput);
    
CreateTableResult result = client.createTable(request);

You must wait until Amazon DynamoDB creates the table and sets the table status to ACTIVE. The createTable request returns a CreateTableResult from which you can get the TableDescription property that provides the necessary table information.

TableDescription tableDescription = result.getTableDescription();

System.out.printf("%s: %s \t ReadCapacityUnits: %d \t WriteCapacityUnits: %d",
  tableDescription.getTableStatus(),
  tableDescription.getTableName(),
  tableDescription.getProvisionedThroughput().getReadCapacityUnits(),
  tableDescription.getProvisionedThroughput().getWriteCapacityUnits());

You can also call the describeTable method of the client to get table information at anytime.

TableDescription tableDescription = client.describeTable(
  new DescribeTableRequest().withTableName(tableName)).getTable();

Updating a Table

You can update only the provisioned throughput values of an existing table. Depending on you application requirements, you might need to update these values.

Note

You can increase the read capacity units and write capacity units anytime. However, you can decrease these values only four times in a 24 hour period. For additional guidelines and limitations, see Specifying Read and Write Requirements for Tables.

The following are the steps to update a table using the Java low-level API.

  1. Create an instance of the AmazonDynamoDBClient class (the client).

  2. Create an instance of the UpdateTableRequest class to provide the request information.

    You must provide the table name and the new provisioned throughput values.

  3. Execute the updateTable method by providing the request object as a parameter.

The following Java code snippet demonstrates the preceding steps.

client = new AmazonDynamoDBClient(credentials);
String tableName = "ProductCatalog";

ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
  .withReadCapacityUnits(15L)
  .withWriteCapacityUnits(12L);

UpdateTableRequest updateTableRequest = new UpdateTableRequest()
  .withTableName(tableName)
  .withProvisionedThroughput(provisionedThroughput);

UpdateTableResult result = client.updateTable(updateTableRequest);

Deleting a Table

The following are the steps to delete a table using the Java low-level API.

  1. Create an instance of the AmazonDynamoDBClient class (the client).

  2. Create an instance of the DeleteTableRequest class and provide the table name that you want to delete.

  3. Execute the deleteTable method by providing the request object as a parameter.

The following Java code snippet demonstrates the preceding steps.

client = new AmazonDynamoDBClient(credentials);
String tableName = "ProductCatalog";

DeleteTableRequest deleteTableRequest = new DeleteTableRequest()
  .withTableName(tableName);
DeleteTableResult result = client.deleteTable(deleteTableRequest);

Listing Tables

To list tables in your account using the AWS SDK for Java low-level API, create an instance of the AmazonDynamoDBClient and execute the listTables method. The ListTables API requires no parameters. However, you can specify optional parameters. For example, you can set the limit parameter if you want to use paging to limit the number of table names per page. This requires you to create a ListTablesRequest object and provide optional parameters as shown in the following Java code snippet. Along with the page size, the request sets the exclusiveStartTableName parameter. Initially, exclusiveStartTableName is null, however, after fetching the first page of result, to retrieve the next page of result, you must set this parameter value to the lastEvaluatedTableName property of the current result.

client = new AmazonDynamoDBClient(credentials);

// Initial value for the first page of table names.
String lastEvaluatedTableName = null;
do {
    
    ListTablesRequest listTablesRequest = new ListTablesRequest()
    .withLimit(10)
    .withExclusiveStartTableName(lastEvaluatedTableName);
    
    ListTablesResult result = client.listTables(listTablesRequest);
    lastEvaluatedTableName = result.getLastEvaluatedTableName();
    
    for (String name : result.getTableNames()) {
        System.out.println(name);
    }
    
} while (lastEvaluatedTableName != null);