Dynamodb 简明教程

DynamoDB - Batch Writing

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

Batch writing operates on multiple items by creating or deleting several items. These operations utilize BatchWriteItem, which carries the limitations of no more than 16MB writes and 25 requests. Each item obeys a 400KB size limit. Batch writes also cannot perform item updates.

What is Batch Writing?

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

Batch writes can manipulate items across multiple tables. Operation invocation happens for each individual request, which means operations do not impact each other, and heterogeneous mixes are permitted; for example, one PutItem and three DeleteItem requests in a batch, with the failure of the PutItem request not impacting the others. Failed requests result in the operation returning information (keys and data) pertaining to each failed request.

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

Note − If DynamoDB returns any items without processing them, retry them; however, use a back-off method to avoid another request failure based on overloading.

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

DynamoDB rejects a batch write operation when one or more of the following statements proves to be true −

  1. The request exceeds the provisioned throughput.

  2. The request attempts to use BatchWriteItems to update an item.

  3. The request performs several operations on a single item.

  4. The request tables do not exist.

  5. The item attributes in the request do not match the target.

  6. The requests exceed size limits.

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

Batch writes require certain RequestItem parameters −

  1. Deletion operations need DeleteRequest key subelements meaning an attribute name and value.

  2. The PutRequest items require an Item subelement meaning an attribute and attribute value map.

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

Response − A successful operation results in an HTTP 200 response, which indicates characteristics like capacity units consumed, table processing metrics, and any unprocessed items.

Batch Writes with Java

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

Perform a batch write by creating a DynamoDB class instance, a TableWriteItems class instance describing all operations, and calling the batchWriteItem method to use the TableWriteItems object.

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

Note − You must create a TableWriteItems instance for every table in a batch write to multiple tables. Also, check your request response for any unprocessed requests.

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

You can review the following example of a batch write −

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 中如何进行批量写入。

The following program is another bigger example for better understanding of how a batch writes with Java.

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

Note − The following example 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 Toolkit。

This example 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.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);
      }
   }
}