修改现有适用于 Java 的 SDK 1.x 应用程序以使用 DAX - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

修改现有适用于 Java 的 SDK 1.x 应用程序以使用 DAX

如果已经有使用 Amazon DynamoDB 的 Java 应用程序,则需要修改,使其可以访问 DynamoDB Accelerator (DAX) 集群。无需重写整个应用程序,因为 DAX Java 客户端与 Amazon SDK for Java 中包含的 DynamoDB 低级别客户端非常相似。

注意

这些说明针对使用 Amazon SDK for Java 1.x 的应用程序。对于使用 Amazon SDK for Java 2.x 的应用程序,请参阅 修改现有应用程序以使用 DAX

假设您有一个名为 Music 的 DynamoDB 表。该表的分区键为 Artist,排序键为 SongTitle。下面的程序直接从 Music 表读取项目。

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()); } } }

要修改程序,请将 DynamoDB 客户端替换为 DAX 客户端。

import java.util.HashMap; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder; 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 AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion("us-east-1").withEndpointConfiguration("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111"); AmazonDynamoDB client = daxClientBuilder.build(); /* ** ... ** Remaining code omitted (it is identical) ** ... */ } }

使用 DynamoDB 文档 API

Amazon SDK for Java 为 DynamoDB 提供一个文档接口。文档 API 充当低级别 DynamoDB 客户端的封装程序。有关更多信息,请参阅文档接口

文档接口还可与低级别 DAX 客户端一起使用,如下面的示例所示。

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazon.dax.client.dynamodbv2.AmazonDaxClientBuilder; import com.amazonaws.services.dynamodbv2.document.DynamoDB; import com.amazonaws.services.dynamodbv2.document.GetItemOutcome; import com.amazonaws.services.dynamodbv2.document.Table; public class GetMusicItemWithDocumentApi { public static void main(String[] args) throws Exception { //Create a DAX client AmazonDaxClientBuilder daxClientBuilder = AmazonDaxClientBuilder.standard(); daxClientBuilder.withRegion("us-east-1").withEndpointConfiguration("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111"); AmazonDynamoDB client = daxClientBuilder.build(); // Document client wrapper DynamoDB docClient = new DynamoDB(client); Table table = docClient.getTable("Music"); try { System.out.println("Attempting to read the item..."); GetItemOutcome outcome = table.tgetItemOutcome( "Artist", "No One You Know", "SongTitle", "Scared of My Shadow"); System.out.println(outcome.getItem()); System.out.println("GetItem succeeded: " + outcome); } catch (Exception e) { System.err.println("Unable to read item"); System.err.println(e.getMessage()); } } }

DAX 异步客户端

AmazonDaxClient 是同步的。对于长时间运行的 DAX API 操作(如大型表的 Scan),可能阻止程序执行直到操作完成。如果程序需要在 DAX API 操作运行期间中时执行其他任务,可以使用 ClusterDaxAsyncClient

下面的程序演示如何使用 ClusterDaxAsyncClient 和 Java Future,实施不阻止执行的解决方案。

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(); } }