修改要用於 SDK Java 1.x 應用程式的現有 DAX - Amazon DynamoDB

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

修改要用於 SDK Java 1.x 應用程式的現有 DAX

如果您已經有使用 Amazon DynamoDB 的 Java 應用程式,則必須對其進行修改,以便其存取您的 DynamoDB Accelerator (DAX) 叢集。您不需要重寫整個應用程式,因為 DAX Java 用戶端類似於 中包含的 DynamoDB 低階用戶端 AWS SDK for Java。

注意

這些指示適用於使用 AWS SDK for Java 1.x 的應用程式。如需使用 AWS 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

AWS 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 是同步的。對於長時間執行DAXAPI的操作,例如大型資料表Scan的 ,這可能會封鎖程式執行,直到操作完成為止。如果您的程式需要在DAXAPI操作進行期間執行其他工作,您可以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(); } }