Dynamodb 简明教程

DynamoDB - Batch Retrieve

批量获取操作返回单个或多个项目的属性。这些操作通常包括使用主键来识别所需项目。 BatchGetItem 操作受限于个别操作及其自身独特的约束。

Batch Retrieve operations return attributes of a single or multiple items. These operations generally consist of using the primary key to identify the desired item(s). The BatchGetItem operations are subject to the limits of individual operations as well as their own unique constraints.

批量获取操作中的以下请求导致拒绝−

The following requests in batch retrieval operations result in rejection −

  1. Make a request for more than 100 items.

  2. Make a request exceeding throughput.

批量检索操作可对可能会超出限制的请求进行部分处理。

Batch retrieve operations perform partial processing of requests carrying the potential to exceed limits.

For example − 检索多个项目大小足以超出限制的请求会部分处理该请求,并显示一个错误消息,注明未处理部分。在返还未处理项目时,要创建回退算法解决方案来管理此问题,而不是限制表格。

For example − a request to retrieve multiple items large enough in size to exceed limits results in part of the request processing, and an error message noting the unprocessed portion. On return of unprocessed items, create a back-off algorithm solution to manage this rather than throttling tables.

BatchGet 操作最终使用一致读取进行,需要对强一致读取进行修改。它们还并行执行检索。

The BatchGet operations perform eventually with consistent reads, requiring modification for strongly consistent ones. They also perform retrievals in parallel.

Note − 返还项目的顺序。DynamoDB 不会对项目进行排序。它也不会指示所请求项目缺失。此外,这些请求会使用容量单位。

Note − The order of the returned items. DynamoDB does not sort the items. It also does not indicate the absence of the requested items. Furthermore, those requests consume capacity units.

所有 BatchGet 操作都需要 RequestItems 参数,例如读取一致性、属性名称和主键。

All the BatchGet operations require RequestItems parameters such as the read consistency, attribute names, and primary keys.

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 Retrievals with Java

在 BatchGet 操作中使用 Java 要求创建一个 DynamoDB 类实例、 TableKeysAndAttributes 类实例(描述项目的哈希值列表),然后将 TableKeysAndAttributes 对象传递给 BatchGetItem 方法。

Using Java in BatchGet operations requires creating a DynamoDB class instance, TableKeysAndAttributes class instance describing a primary key values list for the items, and passing the TableKeysAndAttributes object to the BatchGetItem method.

以下是 BatchGet 操作的一个示例 −

The following is an example of a BatchGet operation −

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

TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes
   (forumTableName);

forumTableKeysAndAttributes.addHashOnlyPrimaryKeys (
   "Title",
   "Updates",
   "Product Line 1"
);
TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes (
   threadTableName);

threadTableKeysAndAttributes.addHashAndRangePrimaryKeys (
   "ForumTitle",
   "Topic",
   "Product Line 1",
   "P1 Thread 1",
   "Product Line 1",
   "P1 Thread 2",
   "Product Line 2",
   "P2 Thread 1"
);
BatchGetItemOutcome outcome = dynamoDB.batchGetItem (
   forumTableKeysAndAttributes, threadTableKeysAndAttributes);

for (String tableName : outcome.getTableItems().keySet()) {
   System.out.println("Table items " + tableName);
   List<Item> items = outcome.getTableItems().get(tableName);
   for (Item item : items) {
      System.out.println(item);
   }
}

你可以查阅以下更全面的示例。

You can review the following larger example.

Note − 以下程序可能会假设有一个已创建的数据源。在尝试执行之前,获得支持库并创建必要的数据源(具备所需特征的表格或其他引用的源)。

Note − The following program 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 program 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.List;
import java.util.Map;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes;
import com.amazonaws.services.dynamodbv2.model.KeysAndAttributes;

public class BatchGetOpSample {
   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 {
      retrieveMultipleItemsBatchGet();
   }
   private static void retrieveMultipleItemsBatchGet() {
      try {
         TableKeysAndAttributes forumTableKeysAndAttributes =
            new TableKeysAndAttributes(forumTableName);

         //Create partition key
         forumTableKeysAndAttributes.addHashOnlyPrimaryKeys (
            "Name",
            "XYZ Melt-O-tron",
            "High-Performance Processing"
         );
         TableKeysAndAttributes threadTableKeysAndAttributes =
            new TableKeysAndAttributes(threadTableName);

         //Create partition key and sort key
         threadTableKeysAndAttributes.addHashAndRangePrimaryKeys (
            "ForumName",
            "Subject",
            "High-Performance Processing",
            "HP Processing Thread One",
            "High-Performance Processing",
            "HP Processing Thread Two",
            "Melt-O-Tron",
            "MeltO Thread One"
         );
         System.out.println("Processing...");
         BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes,
            threadTableKeysAndAttributes);

         Map<String, KeysAndAttributes> unprocessed = null;
         do {
            for (String tableName : outcome.getTableItems().keySet()) {
               System.out.println("Table items for " + tableName);
               List<Item> items = outcome.getTableItems().get(tableName);

               for (Item item : items) {
                  System.out.println(item.toJSONPretty());
               }
            }
            // Confirm no unprocessed items
            unprocessed = outcome.getUnprocessedKeys();

            if (unprocessed.isEmpty()) {
               System.out.println("All items processed.");
            } else {
               System.out.println("Gathering unprocessed items...");
               outcome = dynamoDB.batchGetItemUnprocessed(unprocessed);
            }
         } while (!unprocessed.isEmpty());
      } catch (Exception e) {
         System.err.println("Could not get items.");
         System.err.println(e.getMessage());
      }
   }
}