

Die vorliegende Übersetzung wurde maschinell erstellt. Im Falle eines Konflikts oder eines Widerspruchs zwischen dieser übersetzten Fassung und der englischen Fassung (einschließlich infolge von Verzögerungen bei der Übersetzung) ist die englische Fassung maßgeblich.

# Verwenden einer vorhandenen SDK for Java 1.x Anwendung zur Nutzung von DAX
<a name="DAX.client.modify-your-app.java-sdk-v1"></a>

Wenn Sie bereits über eine Java-Anwendung verfügen, die Amazon DynamoDB verwendet, müssen Sie sie so ändern, dass sie auf den DynamoDB-Accelerator-(DAX)-Cluster zugreifen kann. Sie müssen nicht die gesamte Anwendung umschreiben, da der DAX-Java-Client dem DynamoDB-Low-Level-Client ähnelt, der in AWS SDK für Java enthalten ist.

**Anmerkung**  
Diese Anweisungen gelten für Anwendungen, die AWS SDK for Java 1.x verwenden. Für Anwendungen, die AWS SDK for Java 2.x verwenden, siehe [Ändern einer vorhandenen Anwendung für die Verwendung von DAX](DAX.client.modify-your-app.md).

Angenommen, Sie verfügen über eine DynamoDB-Tabelle mit dem Namen `Music`. Der Partitionsschlüssel für diese Tabelle lautet `Artist` und der Sortierschlüssel ist `SongTitle`. Das folgende Programm liest ein Element direkt aus der Tabelle `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());
        }
    }
}
```

Zum Ändern des Programms ersetzen Sie den DynamoDB-Client durch einen DAX-Client.

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

    }
}
```

## Verwenden der DynamoDB-Dokument-API
<a name="DAX.client.modify-your-app.document-api"></a>

Das AWS SDK für Java stellt eine Dokumentschnittstelle für DynamoDB bereit. Die Dokument-API agiert als Wrapper um den Low-Level-Client von DynamoDB. Weitere Informationen finden Sie unter [Dokumentschnittstellen](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.SDKs.Interfaces.Document.html).

Die Dokumentschnittstelle kann auch mit dem Low-Level-Client von DAX verwendet werden, wie im folgenden Beispiel gezeigt.

```
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-Async-Client
<a name="DAX.client.async"></a>

Der `AmazonDaxClient` ist synchron. Lang andauernde DAX-API-Operationen wie z.B. ein `Scan` einer sehr großen Tabelle können die Ausführung des Programms blockieren, bis die Operation abgeschlossen ist. Wenn Ihr Programm andere Aufgaben erledigen muss, während eine DAX-API-Operation ausgeführt wird, können Sie stattdessen `ClusterDaxAsyncClient` verwenden.

Das folgende Programm zeigt, wie Sie `ClusterDaxAsyncClient` mit Java `Future` verwenden, um eine Lösung zu implementieren, die das Programm nicht behindert.

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

	}
}
```