删除DynamoDB中的项目只需要提供表名和项密钥.强烈建议使用条件表达式,以避免删除错误的项目.
像往常一样,您可以使用GUI控制台,Java或任何其他需要的使用GUI控制台删除项目
导航到控制台.在左侧的导航窗格中,选择表.然后选择表格名称和项目标签.
选择要删除的项目,然后选择操作|删除.
A 然后出现删除项目对话框,如下面的screeshot所示.选择"删除"进行确认.
如何删除项目使用Java?
在项目删除操作中使用Java仅涉及创建DynamoDB客户端实例,并通过使用项目的密钥调用 deleteItem 方法.
您可以看到以下示例,详细解释了它.
DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient( new ProfileCredentialsProvider())); Table table = dynamoDB.getTable("ProductList");DeleteItemOutcome outcome = table.deleteItem("IDnum", 151);
您还可以指定参数以防止错误删除.只需使用 ConditionExpression .
例如 :
MapexpressionAttributeValues = new HashMap (); expressionAttributeValues.put(":val", false); DeleteItemOutcome outcome = table.deleteItem("IDnum",151, "Ship = :val", null, // doesn't use ExpressionAttributeNames expressionAttributeValues);
以下是更好理解的更大范例.
注意 : 以下示例可以假定先前创建的数据源.在尝试执行之前,获取支持库并创建必要的数据源(具有所需特征的表或其他引用的源).
此示例还使用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 DeleteItemOpSample { 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 deleteItem() { Table table = dynamoDB.getTable(tableName); try { DeleteItemSpec deleteItemSpec = new DeleteItemSpec() .withPrimaryKey("ID", 303) .withConditionExpression("#ip = :val") .withNameMap(new NameMap() .with("#ip", "InProduction")) .withValueMap(new ValueMap() .withBoolean(":val", false)) .withReturnValues(ReturnValue.ALL_OLD); DeleteItemOutcome outcome = table.deleteItem(deleteItemSpec); // Confirm System.out.println("Displaying deleted item..."); System.out.println(outcome.getItem().toJSONPretty()); } catch (Exception e) { System.err.println("Cannot delete item in " + tableName); System.err.println(e.getMessage()); } } }