

Die Version AWS SDK für Java 1.x wurde end-of-support am 31. Dezember 2025 erreicht. Wir empfehlen Ihnen, auf den zu migrieren [AWS SDK for Java 2.x](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/home.html), um weiterhin neue Funktionen, Verfügbarkeitsverbesserungen und Sicherheitsupdates zu erhalten.

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.

# Arbeiten mit Elementen in DynamoDB
<a name="examples-dynamodb-items"></a>

 DynamoDB In ist ein Element eine Sammlung von *Attributen*, von denen jedes einen *Namen* und einen *Wert* hat. Ein Attributwert kann eine Skalarfunktion, eine Gruppe oder ein Dokumenttyp sein. Weitere Informationen finden Sie unter [Benennungsregeln und Datentypen](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html) im Amazon DynamoDB Entwicklerhandbuch.

## Abrufen (empfangen) eines Elements aus einer Tabelle
<a name="dynamodb-get-item"></a>

Rufen Sie die AmazonDynamo `getItem` DB-Methode auf und übergeben Sie ihr ein [GetItemRequest](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/model/GetItemRequest.html)Objekt mit dem Tabellennamen und dem Primärschlüsselwert des gewünschten Elements. Sie gibt ein [GetItemResult](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/model/GetItemResult.html)Objekt zurück.

Sie können die `getItem()` Methode des zurückgegebenen `GetItemResult` Objekts verwenden, um eine [Map](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Map.html) von Schlüsselpaaren (String) und Wertpaaren ([AttributeValue](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/model/AttributeValue.html)) abzurufen, die dem Element zugeordnet sind.

 **Importe** 

```
import com.amazonaws.AmazonServiceException;
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 java.util.HashMap;
import java.util.Map;
```

 **Code** 

```
HashMap<String,AttributeValue> key_to_get =
    new HashMap<String,AttributeValue>();

key_to_get.put("DATABASE_NAME", new AttributeValue(name));

GetItemRequest request = null;
if (projection_expression != null) {
    request = new GetItemRequest()
        .withKey(key_to_get)
        .withTableName(table_name)
        .withProjectionExpression(projection_expression);
} else {
    request = new GetItemRequest()
        .withKey(key_to_get)
        .withTableName(table_name);
}

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

try {
    Map<String,AttributeValue> returned_item =
       ddb.getItem(request).getItem();
    if (returned_item != null) {
        Set<String> keys = returned_item.keySet();
        for (String key : keys) {
            System.out.format("%s: %s\n",
                    key, returned_item.get(key).toString());
        }
    } else {
        System.out.format("No item found with the key %s!\n", name);
    }
} catch (AmazonServiceException e) {
    System.err.println(e.getErrorMessage());
    System.exit(1);
```

Das [vollständige Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/dynamodb/src/main/java/aws/example/dynamodb/GetItem.java) finden Sie unter GitHub.

## Hinzufügen eines neuen Elements zu einer Tabelle
<a name="dynamodb-add-item"></a>

Erstellen Sie eine [Map](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Map.html) mit Schlüssel-Wert-Paaren, die die Attribute des Elements darstellen. Diese müssen Werte für die Primärschlüsselfelder der Tabelle enthalten. Wenn das Element mit dem Primärschlüssel bereits vorhanden ist, werden dessen Felder durch die Anforderung *aktualisiert*.

**Anmerkung**  
Wenn die benannte Tabelle für Ihr Konto und Ihre Region nicht existiert, [ResourceNotFoundException](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/model/ResourceNotFoundException.html)wird a ausgelöst.

 **Importe** 

```
import com.amazonaws.AmazonServiceException;
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.ResourceNotFoundException;
import java.util.ArrayList;
```

 **Code** 

```
HashMap<String,AttributeValue> item_values =
    new HashMap<String,AttributeValue>();

item_values.put("Name", new AttributeValue(name));

for (String[] field : extra_fields) {
    item_values.put(field[0], new AttributeValue(field[1]));
}

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

try {
    ddb.putItem(table_name, item_values);
} catch (ResourceNotFoundException e) {
    System.err.format("Error: The table \"%s\" can't be found.\n", table_name);
    System.err.println("Be sure that it exists and that you've typed its name correctly!");
    System.exit(1);
} catch (AmazonServiceException e) {
    System.err.println(e.getMessage());
    System.exit(1);
```

Das [vollständige Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/dynamodb/src/main/java/aws/example/dynamodb/PutItem.java) finden Sie unter GitHub.

## Aktualisieren eines vorhandenen Elements in einer Tabelle
<a name="dynamodb-update-item"></a>

Sie können ein Attribut für ein Element aktualisieren, das bereits in einer Tabelle vorhanden ist, indem Sie die `updateItem` Methode der AmazonDynamo Datenbank verwenden und dabei einen Tabellennamen, einen Primärschlüsselwert und eine Zuordnung der zu aktualisierenden Felder angeben.

**Anmerkung**  
Wenn die benannte Tabelle für Ihr Konto und Ihre Region nicht existiert oder wenn das Element, das durch den von Ihnen übergebenen Primärschlüssel identifiziert wurde, nicht existiert, [ResourceNotFoundException](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/model/ResourceNotFoundException.html)wird a ausgelöst.

 **Importe** 

```
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.AttributeAction;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate;
import com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException;
import java.util.ArrayList;
```

 **Code** 

```
HashMap<String,AttributeValue> item_key =
   new HashMap<String,AttributeValue>();

item_key.put("Name", new AttributeValue(name));

HashMap<String,AttributeValueUpdate> updated_values =
    new HashMap<String,AttributeValueUpdate>();

for (String[] field : extra_fields) {
    updated_values.put(field[0], new AttributeValueUpdate(
                new AttributeValue(field[1]), AttributeAction.PUT));
}

final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();

try {
    ddb.updateItem(table_name, item_key, updated_values);
} catch (ResourceNotFoundException e) {
    System.err.println(e.getMessage());
    System.exit(1);
} catch (AmazonServiceException e) {
    System.err.println(e.getMessage());
    System.exit(1);
```

Das [vollständige Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/dynamodb/src/main/java/aws/example/dynamodb/UpdateItem.java) finden Sie unter GitHub.

## Verwenden Sie die Dynamo-Klasse DBMapper
<a name="use-the-dynamodbmapper-class"></a>

Die [AWS SDK für Java](https://aws.amazon.com/sdk-for-java/)stellt eine [DBMapperDynamo-Klasse](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html) bereit, mit der Sie Ihre clientseitigen Klassen Tabellen zuordnen können. Amazon DynamoDB Um die [DBMapperDynamo-Klasse](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html) zu verwenden, definieren Sie die Beziehung zwischen Elementen in einer DynamoDB Tabelle und ihren entsprechenden Objektinstanzen in Ihrem Code mithilfe von Anmerkungen (wie im folgenden Codebeispiel gezeigt). Mit der [DBMapperDynamo-Klasse](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html) können Sie auf Ihre Tabellen zugreifen, verschiedene Erstellungs-, Lese-, Aktualisierungs- und Löschvorgänge (CRUD) ausführen und Abfragen ausführen.

**Anmerkung**  
Mit der [DBMapperDynamo-Klasse](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html) können Sie keine Tabellen erstellen, aktualisieren oder löschen.

 **Importe** 

```
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException;
```

 **Code** 

Das folgende Java-Codebeispiel zeigt Ihnen, wie Sie mithilfe der [DBMapperDynamo-Klasse](https://docs.aws.amazon.com/sdk-for-java/v1/reference/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBMapper.html) Inhalte zur *Music-Tabelle* hinzufügen. Nachdem der Inhalt der Tabelle hinzugefügt wurde, beachten Sie, dass ein Element mithilfe der Schlüssel *Partition* und *Sortieren* geladen wird. Anschließend wird das Element *Auszeichnungen* aktualisiert. Informationen zum Erstellen der *Music-Tabelle* finden Sie unter [Create a Table](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html) im Amazon DynamoDB Developer Guide.

```
       AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
       MusicItems items = new MusicItems();

       try{
           // Add new content to the Music table
           items.setArtist(artist);
           items.setSongTitle(songTitle);
           items.setAlbumTitle(albumTitle);
           items.setAwards(Integer.parseInt(awards)); //convert to an int

           // Save the item
           DynamoDBMapper mapper = new DynamoDBMapper(client);
           mapper.save(items);

           // Load an item based on the Partition Key and Sort Key
           // Both values need to be passed to the mapper.load method
           String artistName = artist;
           String songQueryTitle = songTitle;

           // Retrieve the item
           MusicItems itemRetrieved = mapper.load(MusicItems.class, artistName, songQueryTitle);
           System.out.println("Item retrieved:");
           System.out.println(itemRetrieved);

           // Modify the Award value
           itemRetrieved.setAwards(2);
           mapper.save(itemRetrieved);
           System.out.println("Item updated:");
           System.out.println(itemRetrieved);

           System.out.print("Done");
       } catch (AmazonDynamoDBException e) {
           e.getStackTrace();
       }
   }

   @DynamoDBTable(tableName="Music")
   public static class MusicItems {

       //Set up Data Members that correspond to columns in the Music table
       private String artist;
       private String songTitle;
       private String albumTitle;
       private int awards;

       @DynamoDBHashKey(attributeName="Artist")
       public String getArtist() {
           return this.artist;
       }

       public void setArtist(String artist) {
           this.artist = artist;
       }

       @DynamoDBRangeKey(attributeName="SongTitle")
       public String getSongTitle() {
           return this.songTitle;
       }

       public void setSongTitle(String title) {
           this.songTitle = title;
       }

       @DynamoDBAttribute(attributeName="AlbumTitle")
       public String getAlbumTitle() {
           return this.albumTitle;
       }

       public void setAlbumTitle(String title) {
           this.albumTitle = title;
       }

       @DynamoDBAttribute(attributeName="Awards")
       public int getAwards() {
           return this.awards;
       }

       public void setAwards(int awards) {
           this.awards = awards;
       }
   }
```

Das [vollständige Beispiel](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/java/example_code/dynamodb/src/main/java/aws/example/dynamodb/UseDynamoMapping.java) finden Sie unter GitHub.

## Weitere Infos
<a name="more-info"></a>
+  [Richtlinien für die Arbeit mit Elementen](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForItems.html) im Amazon DynamoDB Entwicklerhandbuch
+  [Arbeiten mit Elementen aus DynamoDB dem](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html) Amazon DynamoDB Entwicklerhandbuch