Dynamodb 简明教程

DynamoDB - Batch Writing

批量写入操作通过创建或删除多个项目在多个项目上进行操作。这些操作使用 BatchWriteItem ,其限制不超过 16MB 的写入和 25 个请求。每个项目遵守 400KB 的大小限制。批量写入也不能执行项目更新。

What is Batch Writing?

批量写入可以在多个表中操作项目。操作调用发生在每个单独的请求中,这意味着操作不会相互影响,并且允许异构混合;例如,一个批处理中的一个 PutItem 和三个 DeleteItem 请求,其中 PutItem 请求的失败不会影响其他请求。失败的请求会导致操作返回与每个失败的请求有关的信息(键和数据)。

Note − 如果 DynamoDB 返回任何未处理的项目,请重试它们;但是,请使用退避方法来避免由于过载而导致的另一个请求失败。

当以下一个或多个语句被证明为真时,DynamoDB 会拒绝批量写入操作 −

  1. 请求超过预置吞吐量。

  2. 请求尝试使用 BatchWriteItems 来更新某个项目。

  3. 请求对单个项目执行多项操作。

  4. 请求表不存在。

  5. 请求中的项目属性与目标不匹配。

  6. 请求超过大小限制。

批量写入需要某些 RequestItem 参数−

  1. 删除操作需要 DeleteRequestsubelements 表示属性名称和值。

  2. PutRequest 项目需要 Item subelement 表示属性和属性值映射。

Response −成功操作产生 HTTP 200 响应,它指示已消耗的容量单位、表处理指标和任何未处理的项目等特征。

Batch Writes with Java

通过创建一个 DynamoDB 类实例、一个描述所有操作的 TableWriteItems 类实例,并调用 batchWriteItem 方法来使用 TableWriteItems 对象来执行批量写入。

Note −必须为批处理中的每一张表创建 TableWriteItems 实例。另外,检查请求响应以了解任何未处理的请求。

您可以查看以下批量写入示例−

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

TableWriteItems forumTableWriteItems = new TableWriteItems("Forum")
   .withItemsToPut(
   new Item()
   .withPrimaryKey("Title", "XYZ CRM")
   .withNumber("Threads", 0));

TableWriteItems threadTableWriteItems = new TableWriteItems(Thread)
   .withItemsToPut(
   new Item()
   .withPrimaryKey("ForumTitle","XYZ CRM","Topic","Updates")
   .withHashAndRangeKeysToDelete("ForumTitle","A partition key value",
   "Product Line 1", "A sort key value"));

BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem (
   forumTableWriteItems, threadTableWriteItems);

以下程序是另一个较大的示例,用于更好地理解 Java 中如何进行批量写入。

Note −以下示例可能假定已经创建了数据源。在尝试执行之前,获取支持库并创建必要的数据源(具有所需特征的表或其他引用源)。

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

package com.amazonaws.codesamples.document;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.BatchWriteItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.TableWriteItems;
import com.amazonaws.services.dynamodbv2.model.WriteRequest;

public class BatchWriteOpSample {
   static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
      new ProfileCredentialsProvider()));
   static String forumTableName = "Forum";
   static String threadTableName = "Thread";

   public static void main(String[] args) throws IOException {
      batchWriteMultiItems();
   }
   private static void batchWriteMultiItems() {
      try {
         // Place new item in Forum
         TableWriteItems forumTableWriteItems = new TableWriteItems(forumTableName)
                                                                       //Forum
            .withItemsToPut(new Item()
            .withPrimaryKey("Name", "Amazon RDS")
            .withNumber("Threads", 0));

         // Place one item, delete another in Thread
         // Specify partition key and range key
         TableWriteItems threadTableWriteItems = new TableWriteItems(threadTableName)
            .withItemsToPut(new Item()
            .withPrimaryKey("ForumName","Product
            Support","Subject","Support Thread 1")
            .withString("Message", "New OS Thread 1 message")
            .withHashAndRangeKeysToDelete("ForumName","Subject", "Polymer Blaster",
            "Support Thread 100"));

         System.out.println("Processing request...");
         BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem (
               forumTableWriteItems, threadTableWriteItems);
         do {
            // Confirm no unprocessed items
            Map<String, List<WriteRequest>> unprocessedItems
               = outcome.getUnprocessedItems();

            if (outcome.getUnprocessedItems().size() == 0) {
               System.out.println("All items processed.");
            } else {
               System.out.println("Gathering unprocessed items...");
               outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems);
            }
         } while (outcome.getUnprocessedItems().size() > 0);
      } catch (Exception e) {
         System.err.println("Could not get items: ");
         e.printStackTrace(System.err);
      }
   }
}