Dynamodb 简明教程

DynamoDB - Delete Items

在 DynamoDB 中删除项目只需要提供表名和项目键。强烈建议使用条件表达式,这对于避免删除错误的项目是必要的。

Deleting an item in the DynamoDB only requires providing the table name and the item key. It is also strongly recommended to use of a conditional expression which will be necessary to avoid deleting the wrong items.

与往常一样,可以使用 GUI 控制台、Java 或任何其他必需的工具来执行此任务。

As usual, you can either use the GUI console, Java, or any other needed tool to perform this task.

Delete Items Using the GUI Console

导航至控制台。在左侧的导航窗格中,选择 Tables 。然后选择表名和 Items 选项卡。

Navigate to the console. In the navigation pane on the left side, select Tables. Then select the table name, and the Items tab.

delete items using gui console

选择要删除的项目并选择 Actions | Delete

Choose the items desired for deletion, and select Actions | Delete.

select actions

Delete Item(s) 对话框随后显示,如以下屏幕截图所示。选择“删除”进行确认。

A Delete Item(s) dialog box then appears as shown in the following screeshot. Choose “Delete” to confirm.

delete item

How to Delete Items Using Java?

在项目删除操作中使用 Java 只涉及创建 DynamoDB 客户端实例并使用项目的键来调用 deleteItem 方法。

Using Java in item deletion operations merely involves creating a DynamoDB client instance, and calling the deleteItem method through using the item’s key.

你可以查看以下示例,其中有详细的解释。

You can see the following example, where it has been explained in detail.

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
   new ProfileCredentialsProvider()));

Table table = dynamoDB.getTable("ProductList");
DeleteItemOutcome outcome = table.deleteItem("IDnum", 151);

你还可以指定参数来防止错误删除。只需使用 ConditionExpression

You can also specify the parameters to protect against incorrect deletion. Simply use a ConditionExpression.

例如 -

For example −

Map<String,Object> expressionAttributeValues = new HashMap<String,Object>();
expressionAttributeValues.put(":val", false);

DeleteItemOutcome outcome = table.deleteItem("IDnum",151,
   "Ship = :val",
   null,                   // doesn't use ExpressionAttributeNames
   expressionAttributeValues);

以下是一个更大的示例,以便更好地理解。

The following is a larger example for better understanding.

Note − 以下示例可能假设之前创建过数据源。在尝试执行之前,获取支持库并创建必要的数据源(具有所需特性的表,或其他参考源)。

Note − The following sample may assume a previously created data source. Before attempting to execute, acquire supporting libraries and create necessary data sources (tables with required characteristics, or other referenced sources).

此示例还使用 Eclipse IDE、AWS 凭据文件和 Eclipse AWS Java 项目中的 AWS 工具包。

This sample also uses Eclipse IDE, an AWS credentials file, and the AWS Toolkit within an Eclipse AWS Java Project.

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<String>(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<String>(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());
      }
   }
}