Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

Working with Tables: Java

You can use the AWS SDK for Java to create, update, and delete tables, list all the tables in your account, or get information about a specific table.

The following are the common steps for table operations using the AWS SDK for Java Document API.

Creating a Table

To create a table, you must provide the table name, its primary key, and the provisioned throughput values. The following code snippet creates an example table that uses a numeric type attribute Id as its primary key.

To create a table using the AWS SDK for Java API:

  1. Create an instance of the DynamoDB class.

  2. Instantiate a CreateTableRequest to provide the request information.

    You must provide the table name, attribute definitions, key schema, and provisioned throughput values.

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

The following code snippet demonstrates the preceding steps.

Copy
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(6L)); Table table = dynamoDB.createTable(request); table.waitForActive();

The table will not be ready for use until DynamoDB creates it and sets its status to ACTIVE. The createTable request returns a Table object that you can use to obtain more information about the table.

Example

Copy
TableDescription tableDescription = dynamoDB.getTable(tableName).describe(); System.out.printf("%s: %s \t ReadCapacityUnits: %d \t WriteCapacityUnits: %d", tableDescription.getTableStatus(), tableDescription.getTableName(), tableDescription.getProvisionedThroughput().getReadCapacityUnits(), tableDescription.getProvisionedThroughput().getWriteCapacityUnits());

You can call the describe method of the client to get table information at any time.

Example

Copy
TableDescription tableDescription = dynamoDB.getTable(tableName).describe();

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.

To update a table using the AWS SDK for Java API:

  1. Create an instance of the Table class.

  2. Create an instance of the ProvisionedThroughput class to provide the new throughput values.

  3. Execute the updateTable method by providing the ProvisionedThroughput instance as a parameter.

The following code snippet demonstrates the preceding steps.

Example

Copy
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("ProductCatalog"); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(15L) .withWriteCapacityUnits(12L); table.updateTable(provisionedThroughput); table.waitForActive();

Deleting a Table

To delete a table:

  1. Create an instance of the Table class.

  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 Table instance as a parameter.

The following code snippet demonstrates the preceding steps.

Example

Copy
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.getTable("ProductCatalog"); table.delete(); table.waitForDelete();

Listing Tables

To list tables in your account, create an instance of DynamoDB and execute the listTables method. The ListTables operation requires no parameters.

Example

Copy
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); DynamoDB dynamoDB = new DynamoDB(client); TableCollection<ListTablesResult> tables = dynamoDB.listTables(); Iterator<Table> iterator = tables.iterator(); while (iterator.hasNext()) { Table table = iterator.next(); System.out.println(table.getTableName()); }