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!");
}
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!");
}
Aggregations