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

Modifying an Existing Application to Use DAX

If you already have an application that uses DynamoDB, you will need to modify it so that it can access your DAX cluster. The DAX cluster client is very similar to the DynamoDB low-level client included in the AWS SDK for Java, so you do not need to rewrite your entire application.

Suppose that you have a DynamoDB table named Music. The partition key for the table is Artist, and its sort key is SongTitle. The following program reads an item directly from the Music table:

Copy
import java.util.HashMap; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.GetItemRequest; import com.amazonaws.services.dynamodbv2.model.GetItemResult; public class GetMusicItem { public static void main(String[] args) throws Exception { // Create a DynamoDB client AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Artist", new AttributeValue().withS("No One You Know")); key.put("SongTitle", new AttributeValue().withS("Scared of My Shadow")); GetItemRequest request = new GetItemRequest() .withTableName("Music").withKey(key); try { System.out.println("Attempting to read the item..."); GetItemResult result = client.getItem(request); System.out.println("GetItem succeeded: " + result); } catch (Exception e) { System.err.println("Unable to read item"); System.err.println(e.getMessage()); } } }

To modify the program, you replace the DynamoDB client with a DAX client:

Copy
import java.util.HashMap; import com.amazon.dax.client.dynamodbv2.AmazonDaxClient; import com.amazon.dax.client.dynamodbv2.ClientConfig; import com.amazon.dax.client.dynamodbv2.ClusterDaxClient; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.GetItemRequest; import com.amazonaws.services.dynamodbv2.model.GetItemResult; public class GetMusicItem { public static void main(String[] args) throws Exception { // Create a DAX client ClientConfig daxConfig = new ClientConfig() .withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111"); AmazonDaxClient client = new ClusterDaxClient(daxConfig); /* ** ... ** Remaining code omitted (it is identical) ** ... */ } }

Using the DynamoDB Document API

The AWS SDK for Java provides a document interface for DynamoDB. The document API acts as a wrapper around the low-level DynamoDB client. (For more information, see Document Interfaces.)

The document interface can also be used with the low-level DAX client, as shown below:

Copy
import com.amazon.dax.client.dynamodbv2.AmazonDaxClient; import com.amazon.dax.client.dynamodbv2.ClientConfig; import com.amazon.dax.client.dynamodbv2.ClusterDaxClient; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.Item; import com.amazonaws.services.dynamodbv2.document.Table; public class GetMusicItemWithDocumentApi { public static void main(String[] args) throws Exception { ClientConfig daxConfig = new ClientConfig() .withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111"); AmazonDaxClient client = new ClusterDaxClient(daxConfig); // Document client wrapper DynamoDB docClient = new DynamoDB(client); Table table = docClient.getTable("Music"); try { System.out.println("Attempting to read the item..."); System.out.println(table.getItem( "Artist", "No One You Know", "SongTitle", "Scared of My Shadow")); System.out.println("GetItem succeeded: " + outcome); } catch (Exception e) { System.err.println("Unable to read item"); System.err.println(e.getMessage()); } } }

DAX Async Client

The AmazonDaxClient is synchronous. For a long-running DAX API operation, such as a Scan of a very large table, this can block program execution until the operation is complete. If your program needs to perform other work while a DAX API operation is in progress, you can use ClusterDaxAsyncClient instead.

The following program shows how to use ClusterDaxAsyncClient, along with Java Future, to implement a non-blocking solution.

Copy
import java.util.HashMap; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import com.amazon.dax.client.dynamodbv2.ClientConfig; import com.amazon.dax.client.dynamodbv2.ClusterDaxAsyncClient; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.handlers.AsyncHandler; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync; import com.amazonaws.services.dynamodbv2.model.AttributeValue; import com.amazonaws.services.dynamodbv2.model.GetItemRequest; import com.amazonaws.services.dynamodbv2.model.GetItemResult; public class DaxAsyncClientDemo { public static void main(String[] args) throws Exception { ClientConfig daxConfig = new ClientConfig().withCredentialsProvider(new ProfileCredentialsProvider()) .withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111"); AmazonDynamoDBAsync client = new ClusterDaxAsyncClient(daxConfig); HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("Artist", new AttributeValue().withS("No One You Know")); key.put("SongTitle", new AttributeValue().withS("Scared of My Shadow")); GetItemRequest request = new GetItemRequest() .withTableName("Music").withKey(key); // Java Futures Future<GetItemResult> call = client.getItemAsync(request); while (!call.isDone()) { // Do other processing while you're waiting for the response System.out.println("Doing something else for a few seconds..."); Thread.sleep(3000); } // The results should be ready by now try { call.get(); } catch (ExecutionException ee) { // Futures always wrap errors as an ExecutionException. // The *real* exception is stored as the cause of the // ExecutionException Throwable exception = ee.getCause(); System.out.println("Error getting item: " + exception.getMessage()); } // Async callbacks call = client.getItemAsync(request, new AsyncHandler<GetItemRequest, GetItemResult>() { @Override public void onSuccess(GetItemRequest request, GetItemResult getItemResult) { System.out.println("Result: " + getItemResult); } @Override public void onError(Exception e) { System.out.println("Unable to read item"); System.err.println(e.getMessage()); // Callers can also test if exception is an instance of // AmazonServiceException or AmazonClientException and cast // it to get additional information } }); call.get(); } }