Lavora con gli elementi in DynamoDB - AWS SDK for Java 2.x

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Lavora con gli elementi in DynamoDB

In DynamoDB, una voce è una raccolta di attributi, ognuno dei quali dispone di un nome e un valore. Un valore attributo può essere un tipo scalare, set o documento. Per ulteriori informazioni, consulta Regole di denominazione e tipi di dati nella Guida per gli Amazon DynamoDB sviluppatori.

Recuperare (ottenere) un item da una tabella

Chiama il getItem metodo DynamoDbClient's e passagli un GetItemRequestoggetto con il nome della tabella e il valore della chiave primaria dell'elemento desiderato. Restituisce un GetItemResponseoggetto con tutti gli attributi di quell'elemento. Puoi specificare una o più espressioni di proiezione in GetItemRequest per recuperare attributi specifici.

È possibile utilizzare il item() metodo dell'GetItemResponseoggetto restituito per recuperare una mappa di coppie chiave (String AttributeValue) e valore () associate all'elemento.

Importazioni

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; import java.util.HashMap; import java.util.Map; import java.util.Set;

Codice

public static void getDynamoDBItem(DynamoDbClient ddb,String tableName,String key,String keyVal ) { HashMap<String,AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); keyToGet.put(key, AttributeValue.builder() .s(keyVal).build()); GetItemRequest request = GetItemRequest.builder() .key(keyToGet) .tableName(tableName) .build(); try { Map<String,AttributeValue> returnedItem = ddb.getItem(request).item(); if (returnedItem != null) { Set<String> keys = returnedItem.keySet(); System.out.println("Amazon DynamoDB table attributes: \n"); for (String key1 : keys) { System.out.format("%s: %s\n", key1, returnedItem.get(key1).toString()); } } else { System.out.format("No item found with the key %s!\n", key); } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }

Vedi l'esempio completo su. GitHub

Recupero (Get) di una voce da una tabella utilizzando il client asincrono

Invocate il getItem metodo di DynamoDbAsyncClient e passategli un GetItemRequestoggetto con il nome della tabella e il valore della chiave primaria dell'elemento desiderato.

È possibile restituire un'istanza di raccolta con tutti gli attributi per tale voce (fare riferimento all'esempio seguente).

Importazioni

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.GetItemRequest; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient; import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;

Codice

public static void getItem(DynamoDbAsyncClient client, String tableName, String key, String keyVal) { HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>(); keyToGet.put(key, AttributeValue.builder() .s(keyVal).build()); try { // Create a GetItemRequest instance GetItemRequest request = GetItemRequest.builder() .key(keyToGet) .tableName(tableName) .build(); // Invoke the DynamoDbAsyncClient object's getItem java.util.Collection<AttributeValue> returnedItem = client.getItem(request).join().item().values(); // Convert Set to Map Map<String, AttributeValue> map = returnedItem.stream().collect(Collectors.toMap(AttributeValue::s, s->s)); Set<String> keys = map.keySet(); for (String sinKey : keys) { System.out.format("%s: %s\n", sinKey, map.get(sinKey).toString()); } } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); }

Vedi l'esempio completo su. GitHub

Aggiunta di un nuovo item a una tabella

Creare una mappa di coppie chiave-valore che rappresentino gli attributi della voce. Queste devono includere valori per i campi chiave primaria della tabella. Se l'elemento identificato dalla chiave primaria esiste già, i relativi campi vengono aggiornati dalla richiesta.

Nota

Se la tabella denominata non esiste per il tuo account e la tua regione, ResourceNotFoundExceptionviene generata a.

Importazioni

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.PutItemRequest; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import java.util.HashMap;

Codice

public static void putItemInTable(DynamoDbClient ddb, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal){ HashMap<String,AttributeValue> itemValues = new HashMap<String,AttributeValue>(); // Add all content to the table itemValues.put(key, AttributeValue.builder().s(keyVal).build()); itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build()); itemValues.put(albumTitle, AttributeValue.builder().s(albumTitleValue).build()); itemValues.put(awards, AttributeValue.builder().s(awardVal).build()); PutItemRequest request = PutItemRequest.builder() .tableName(tableName) .item(itemValues) .build(); try { ddb.putItem(request); System.out.println(tableName +" was successfully updated"); } catch (ResourceNotFoundException e) { System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName); System.err.println("Be sure that it exists and that you've typed its name correctly!"); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }

Vedi l'esempio completo su. GitHub

Aggiornamento di un item esistente in una tabella

È possibile aggiornare un attributo per una voce che esiste già in una tabella utilizzando il metodo updateItem di DynamoDbClient, fornendo un nome di tabella, un valore chiave primaria e una mappa di campi da aggiornare.

Nota

Se la tabella denominata non esiste per il tuo account e la tua regione, o se l'elemento identificato dalla chiave primaria che hai passato non esiste, ResourceNotFoundExceptionviene generato a.

Importazioni

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeAction; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate; import software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException; import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.HashMap;

Codice

public static void updateTableItem(DynamoDbClient ddb, String tableName, String key, String keyVal, String name, String updateVal){ HashMap<String,AttributeValue> itemKey = new HashMap<String,AttributeValue>(); itemKey.put(key, AttributeValue.builder().s(keyVal).build()); HashMap<String,AttributeValueUpdate> updatedValues = new HashMap<String,AttributeValueUpdate>(); // Update the column specified by name with updatedVal updatedValues.put(name, AttributeValueUpdate.builder() .value(AttributeValue.builder().s(updateVal).build()) .action(AttributeAction.PUT) .build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName(tableName) .key(itemKey) .attributeUpdates(updatedValues) .build(); try { ddb.updateItem(request); } catch (ResourceNotFoundException e) { System.err.println(e.getMessage()); System.exit(1); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("Done!"); }

Vedi l'esempio completo su. GitHub

Aggiornamento di una voce (item) esistente in una tabella

È possibile eliminare un elemento esistente in una tabella utilizzando il deleteItem metodo DynamoDbClient's e fornendo un nome di tabella oltre al valore della chiave primaria.

Nota

Se la tabella denominata non esiste per il tuo account e la tua regione, o se l'elemento identificato dalla chiave primaria che hai passato non esiste, ResourceNotFoundExceptionviene generato un.

Importazioni

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.DeleteItemRequest; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import java.util.HashMap;

Codice

public static void deleteDynamoDBItem(DynamoDbClient ddb, String tableName, String key, String keyVal) { HashMap<String,AttributeValue> keyToGet = new HashMap<String,AttributeValue>(); keyToGet.put(key, AttributeValue.builder() .s(keyVal) .build()); DeleteItemRequest deleteReq = DeleteItemRequest.builder() .tableName(tableName) .key(keyToGet) .build(); try { ddb.deleteItem(deleteReq); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } }

Vedi l'esempio completo su. GitHub

Ulteriori informazioni