- Amazon DynamoDB

The file contains utility methods.

The getDynamoDBClient and getDaxClient methods provide Amazon DynamoDB and DynamoDB Accelerator (DAX) clients. For control plane operations (CreateTable, DeleteTable) and write operations, the program uses the DynamoDB client. If you specify a DAX cluster endpoint, the main program creates a DAX client for performing read operations (GetItem, Query, Scan).

The other TryDaxHelper methods (createTable, writeData, deleteTable) are for setting up and tearing down the DynamoDB table and its data.

You can modify the program in several ways:

  • Use different provisioned throughput settings for the table.

  • Modify the size of each item written (see the stringSize variable in the writeData method).

  • Modify the number of GetItem, Query, and Scan tests and their parameters.

  • Comment out the lines containing helper.CreateTable and helper.DeleteTable (if you don't want to create and delete the table each time you run the program).


To run this program, you can set up Maven to use the client for the DAX SDK for Java and the AWS SDK for Java as dependencies. For more information, see Using the client as an Apache Maven dependency.

Or, you can download and include both the DAX Java client and the AWS SDK for Java in your classpath. See Java and DAX for an example of setting your CLASSPATH variable.

import; import; import; import; import; import; import; import; import; import; import; import com.amazonaws.util.EC2MetadataUtils; public class TryDaxHelper { private static final String region = EC2MetadataUtils.getEC2InstanceRegion(); DynamoDB getDynamoDBClient() { System.out.println("Creating a DynamoDB client"); AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard() .withRegion(region) .build(); return new DynamoDB(client); } DynamoDB getDaxClient(String daxEndpoint) { System.out.println("Creating a DAX client with cluster endpoint " + daxEndpoint); AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion(region).withEndpointConfiguration(daxEndpoint); AmazonDynamoDB client =; return new DynamoDB(client); } void createTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("Attempting to create table; please wait..."); table = client.createTable(tableName, Arrays.asList( new KeySchemaElement("pk", KeyType.HASH), // Partition key new KeySchemaElement("sk", KeyType.RANGE)), // Sort key Arrays.asList( new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L)); table.waitForActive(); System.out.println("Successfully created table. Table status: " + table.getDescription().getTableStatus()); } catch (Exception e) { System.err.println("Unable to create table: "); e.printStackTrace(); } } void writeData(String tableName, DynamoDB client, int pkmax, int skmax) { Table table = client.getTable(tableName); System.out.println("Writing data to the table..."); int stringSize = 1000; StringBuilder sb = new StringBuilder(stringSize); for (int i = 0; i < stringSize; i++) { sb.append('X'); } String someData = sb.toString(); try { for (Integer ipk = 1; ipk <= pkmax; ipk++) { System.out.println(("Writing " + skmax + " items for partition key: " + ipk)); for (Integer isk = 1; isk <= skmax; isk++) { table.putItem(new Item() .withPrimaryKey("pk", ipk, "sk", isk) .withString("someData", someData)); } } } catch (Exception e) { System.err.println("Unable to write item:"); e.printStackTrace(); } } void deleteTable(String tableName, DynamoDB client) { Table table = client.getTable(tableName); try { System.out.println("\nAttempting to delete table; please wait..."); table.delete(); table.waitForDelete(); System.out.println("Successfully deleted table."); } catch (Exception e) { System.err.println("Unable to delete table: "); e.printStackTrace(); } } }