Work with tables in DynamoDB - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Work with tables in DynamoDB

Tables are the containers for all items in a DynamoDB database. Before you can add or remove data from DynamoDB, you must create a table.

For each table, you must define:

  • A table name that is unique for your account and Region.

  • A primary key for which every value must be unique; no two items in your table can have the same primary key value.

    A primary key can be simple, consisting of a single partition (HASH) key, or composite, consisting of a partition and a sort (RANGE) key.

    Each key value has an associated data type, enumerated by the ScalarAttributeType class. The key value can be binary (B), numeric (N), or a string (S). For more information, see Naming Rules and Data Types in the Amazon DynamoDB Developer Guide.

  • Provisioned throughput are values that define the number of reserved read/write capacity units for the table.

    Note

    Amazon DynamoDB pricing is based on the provisioned throughput values that you set on your tables, so reserve only as much capacity as you think you’ll need for your table.

    Provisioned throughput for a table can be modified at any time, so you can adjust capacity as your needs change.

Create a table

Use the DynamoDbClient’s createTable method to create a new DynamoDB table. You need to construct table attributes and a table schema, both of which are used to identify the primary key of your table. You must also supply initial provisioned throughput values and a table name.

Note

If a table with the name you chose already exists, an DynamoDbException is thrown.

Create a table with a simple primary key

This code creates a table with one attribute that is the table's simple primary key. the example uses AttributeDefinition and KeySchemaElement objects for the CreateTableRequest.

Imports

import software.amazon.awssdk.core.waiters.WaiterResponse; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.waiters.DynamoDbWaiter;

Code

public static String createTable(DynamoDbClient ddb, String tableName, String key) { DynamoDbWaiter dbWaiter = ddb.waiter(); CreateTableRequest request = CreateTableRequest.builder() .attributeDefinitions(AttributeDefinition.builder() .attributeName(key) .attributeType(ScalarAttributeType.S) .build()) .keySchema(KeySchemaElement.builder() .attributeName(key) .keyType(KeyType.HASH) .build()) .provisionedThroughput(ProvisionedThroughput.builder() .readCapacityUnits(new Long(10)) .writeCapacityUnits(new Long(10)) .build()) .tableName(tableName) .build(); String newTable =""; try { CreateTableResponse response = ddb.createTable(request); DescribeTableRequest tableRequest = DescribeTableRequest.builder() .tableName(tableName) .build(); // Wait until the Amazon DynamoDB table is created WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); waiterResponse.matched().response().ifPresent(System.out::println); newTable = response.tableDescription().tableName(); return newTable; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }

See the complete example on GitHub.

Create a table with a composite primary key

The following example creates a table with two attributes. Both attributes are used for the composite primary key.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest; import software.amazon.awssdk.services.dynamodb.model.CreateTableResponse; import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement; import software.amazon.awssdk.services.dynamodb.model.KeyType; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType; import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

Code

public static String createTableComKey(DynamoDbClient ddb, String tableName) { CreateTableRequest request = CreateTableRequest.builder() .attributeDefinitions( AttributeDefinition.builder() .attributeName("Language") .attributeType(ScalarAttributeType.S) .build(), AttributeDefinition.builder() .attributeName("Greeting") .attributeType(ScalarAttributeType.S) .build()) .keySchema( KeySchemaElement.builder() .attributeName("Language") .keyType(KeyType.HASH) .build(), KeySchemaElement.builder() .attributeName("Greeting") .keyType(KeyType.RANGE) .build()) .provisionedThroughput( ProvisionedThroughput.builder() .readCapacityUnits(new Long(10)) .writeCapacityUnits(new Long(10)).build()) .tableName(tableName) .build(); String tableId = ""; try { CreateTableResponse result = ddb.createTable(request); tableId = result.tableDescription().tableId(); return tableId; } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } return ""; }

See the complete example on GitHub.

List tables

You can list the tables in a particular Region by calling the DynamoDbClient’s listTables method.

Note

If the named table doesn’t exist for your account and Region, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse; import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.List;

Code

public static void listAllTables(DynamoDbClient ddb){ boolean moreTables = true; String lastName = null; while(moreTables) { try { ListTablesResponse response = null; if (lastName == null) { ListTablesRequest request = ListTablesRequest.builder().build(); response = ddb.listTables(request); } else { ListTablesRequest request = ListTablesRequest.builder() .exclusiveStartTableName(lastName).build(); response = ddb.listTables(request); } List<String> tableNames = response.tableNames(); if (tableNames.size() > 0) { for (String curName : tableNames) { System.out.format("* %s\n", curName); } } else { System.out.println("No tables found!"); System.exit(0); } lastName = response.lastEvaluatedTableName(); if (lastName == null) { moreTables = false; } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } System.out.println("\nDone!"); }

By default, up to 100 tables are returned per call—​use lastEvaluatedTableName on the returned ListTablesResponse object to get the last table that was evaluated. You can use this value to start the listing after the last returned value of the previous listing.

See the complete example on GitHub.

Describe (get information about) a table

Use the DynamoDbClient’s describeTable method to get information about a table.

Note

If the named table doesn’t exist for your account and Region, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition; import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputDescription; import software.amazon.awssdk.services.dynamodb.model.TableDescription; import java.util.List;

Code

public static void describeDymamoDBTable(DynamoDbClient ddb,String tableName ) { DescribeTableRequest request = DescribeTableRequest.builder() .tableName(tableName) .build(); try { TableDescription tableInfo = ddb.describeTable(request).table(); if (tableInfo != null) { System.out.format("Table name : %s\n", tableInfo.tableName()); System.out.format("Table ARN : %s\n", tableInfo.tableArn()); System.out.format("Status : %s\n", tableInfo.tableStatus()); System.out.format("Item count : %d\n", tableInfo.itemCount().longValue()); System.out.format("Size (bytes): %d\n", tableInfo.tableSizeBytes().longValue()); ProvisionedThroughputDescription throughputInfo = tableInfo.provisionedThroughput(); System.out.println("Throughput"); System.out.format(" Read Capacity : %d\n", throughputInfo.readCapacityUnits().longValue()); System.out.format(" Write Capacity: %d\n", throughputInfo.writeCapacityUnits().longValue()); List<AttributeDefinition> attributes = tableInfo.attributeDefinitions(); System.out.println("Attributes"); for (AttributeDefinition a : attributes) { System.out.format(" %s (%s)\n", a.attributeName(), a.attributeType()); } } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("\nDone!"); }

See the complete example on GitHub.

Modify (update) a table

You can modify your table’s provisioned throughput values at any time by calling the DynamoDbClient’s updateTable method.

Note

If the named table doesn’t exist for your account and Region, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.UpdateTableRequest; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

Code

public static void updateDynamoDBTable(DynamoDbClient ddb, String tableName, Long readCapacity, Long writeCapacity) { System.out.format( "Updating %s with new provisioned throughput values\n", tableName); System.out.format("Read capacity : %d\n", readCapacity); System.out.format("Write capacity : %d\n", writeCapacity); ProvisionedThroughput tableThroughput = ProvisionedThroughput.builder() .readCapacityUnits(readCapacity) .writeCapacityUnits(writeCapacity) .build(); UpdateTableRequest request = UpdateTableRequest.builder() .provisionedThroughput(tableThroughput) .tableName(tableName) .build(); try { ddb.updateTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }

See the complete example on GitHub.

Delete a table

To delete a table, call DynamoDbClient’s deleteTable method and provide the table’s name.

Note

If the named table doesn’t exist for your account and Region, a ResourceNotFoundException is thrown.

Imports

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;

Code

public static void deleteDynamoDBTable(DynamoDbClient ddb, String tableName) { DeleteTableRequest request = DeleteTableRequest.builder() .tableName(tableName) .build(); try { ddb.deleteTable(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println(tableName +" was successfully deleted!"); }

See the complete example on GitHub.

More information