更新DynamoDB中的项目主要包括指定项目的完整主键和表名称.它需要为您修改的每个属性创建一个新值.该操作使用 UpdateItem ,它会修改现有项目或在发现缺失项目时创建它们.
在更新中,您可能希望通过显示来跟踪更改在操作之前和之后的原始值和新值. UpdateItem使用 ReturnValues 参数来实现此目的.
注意 : 该操作不报告容量单位消耗,但您可以使用 ReturnConsumedCapacity 参数.
使用GUI控制台,Java或任何其他工具执行此任务.
如何使用GUI工具更新项目?
导航到控制台.在左侧的导航窗格中,选择表.选择所需的表格,然后选择项目标签.
选择更新所需的项目,然后选择操作|编辑.
修改任何属性或编辑项窗口中必需的值.
使用Java更新项
在项目更新操作中使用Java需要创建一个Table类实例,并调用其 updateItem 方法.然后指定项目的主键,并提供 UpdateExpression 详细设计属性修改.
以下是相同的示例;
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); Table table = dynamoDB.getTable("ProductList");MapexpressionAttributeNames = new HashMap ();expressionAttributeNames.put("#M", "Make");expressionAttributeNames.put("#P", "PriceexpressionAttributeNames.put("#N", "ID");Map expressionAttributeValues = new HashMap ();expressionAttributeValues.put(":val1", new HashSet (Arrays.asList("Make1","Make2")));expressionAttributeValues.put(":val2", 1); //PriceUpdateItemOutcome outcome = table.updateItem( "internalID", // key attribute name 111, // key attribute value "add #M :val1 set #P = #P - :val2 remove #N", // UpdateExpression expressionAttributeNames, expressionAttributeValues);
updateItem 方法还允许指定条件,可以在以下示例中看到 :
Table table = dynamoDB.getTable("ProductList");MapexpressionAttributeNames = new HashMap ();expressionAttributeNames.put("#P", "Price");Map expressionAttributeValues = new HashMap ();expressionAttributeValues.put(":val1", 44); // change Price to 44expressionAttributeValues.put(":val2", 15); // only if currently 15UpdateItemOutcome outcome = table.updateItem (new PrimaryKey("internalID",111), "set #P = :val1", // Update "#P = :val2", // Condition expressionAttributeNames, expressionAttributeValues);
使用计数器更新项目
DynamoDB允许使用原子计数器,这意味着使用UpdateItem递增/递减属性值而不会影响其他要求;此外,计数器总是更新.
以下是一个解释如何完成的例子.
注意 去;以下示例可以假定先前创建的数据源.在尝试执行之前,获取支持库并创建必要的数据源(具有所需特征的表或其他引用的源).
此示例还使用Eclipse IDE,AWS凭证文件和Eclipse AWS Java项目中的AWS Toolkit.
package com.amazonaws.codesamples.document;import java.io.IOException;import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.Map;import com.amazonaws.auth.profile.ProfileCredentialsProvider;import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome;import com.amazonaws.services.dynamodbv2.document.DynamoDB;import com.amazonaws.services.dynamodbv2.document.Item;import com.amazonaws.services.dynamodbv2.document.Table;import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;import com.amazonaws.services.dynamodbv2.document.utils.NameMap;import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;import com.amazonaws.services.dynamodbv2.model.ReturnValue;public class UpdateItemOpSample { static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); static String tblName = "ProductList"; public static void main(String[] args) throws IOException { createItems(); retrieveItem(); // Execute updates updateMultipleAttributes(); updateAddNewAttribute(); updateExistingAttributeConditionally(); // Item deletion deleteItem(); } private static void createItems() { Table table = dynamoDB.getTable(tblName); try { Item item = new Item() .withPrimaryKey("ID", 303) .withString("Nomenclature", "Polymer Blaster 4000") .withStringSet( "Manufacturers", new HashSet(Arrays.asList("XYZ Inc.", "LMNOP Inc."))) .withNumber("Price", 50000) .withBoolean("InProduction", true) .withString("Category", "Laser Cutter"); table.putItem(item); item = new Item() .withPrimaryKey("ID", 313) .withString("Nomenclature", "Agitatatron 2000") .withStringSet( "Manufacturers", new HashSet (Arrays.asList("XYZ Inc,", "CDE Inc."))) .withNumber("Price", 40000) .withBoolean("InProduction", true) .withString("Category", "Agitator"); table.putItem(item); } catch (Exception e) { System.err.println("Cannot create items."); System.err.println(e.getMessage()); } } private static void updateAddNewAttribute() { Table table = dynamoDB.getTable(tableName); try { Map expressionAttributeNames = new HashMap (); expressionAttributeNames.put("#na", "NewAttribute"); UpdateItemSpec updateItemSpec = new UpdateItemSpec() .withPrimaryKey("ID", 303) .withUpdateExpression("set #na = :val1") .withNameMap(new NameMap() .with("#na", "NewAttribute")) .withValueMap(new ValueMap() .withString(":val1", "A value")) .withReturnValues(ReturnValue.ALL_NEW); UpdateItemOutcome outcome = table.updateItem(updateItemSpec); // Confirm System.out.println("Displaying updated item..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Cannot add an attribute in " + tableName); System.err.println(e.getMessage()); } } }