Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

修改现有应用程序以使用 DAX

如果您已经有使用 DynamoDB 的 Java 应用程序,则需要修改它,以使它能够访问您的 DAX 集群。DAX Java 客户端与 AWS 适用于 Java 的开发工具包 中包含的 DynamoDB 低级别客户端非常相似,因此您无需重写整个应用程序。

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

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

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

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) ** ... */ } }

使用 DynamoDB 文档 API

AWS 适用于 Java 的开发工具包 为 DynamoDB 提供了一个文档接口。该文档 API 充当一个围绕低级别 DynamoDB 客户端的包装程序。(有关更多信息,请参阅文档接口。)

该文档接口还可与低级别 DAX 客户端一起使用,如下所示:

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 异步客户端

AmazonDaxClient 是同步的。对于长时间运行的 DAX API 操作 (如超大型表的 Scan),这可能会在操作完成前阻止程序执行。如果您的程序需要在 DAX API 操作正在进行中时执行其他任务,您可改用 ClusterDaxAsyncClient

以下程序展示了如何使用 ClusterDaxAsyncClient 以及 Java Future 来实施非阻止性解决方案。

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