Search in sources :

Example 1 with UpdateItemRequest

use of software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest in project aws-doc-sdk-examples by awsdocs.

the class Scenario method updateTableItem.

// snippet-end:[dynamodb.java2.scenario.populate_table.main]
// Update the record to include show only directors.
public static void updateTableItem(DynamoDbClient ddb, String tableName) {
    HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
    // Specify the key and sort key.
    itemKey.put("year", AttributeValue.builder().n("1933").build());
    itemKey.put("title", AttributeValue.builder().s("King Kong").build());
    HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
    // Update the column specified by info with updatedVal.
    updatedValues.put("info", AttributeValueUpdate.builder().value(AttributeValue.builder().s("{\"directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack\"]").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("Item was updated!");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 2 with UpdateItemRequest

use of software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest in project aws-doc-sdk-examples by awsdocs.

the class UpdateItem method updateTableItem.

// snippet-start:[dynamodb.java2.update_item.main]
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!");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Aggregations

HashMap (java.util.HashMap)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 AttributeValueUpdate (software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate)2 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)2 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)2 UpdateItemRequest (software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest)2