📅  最后修改于: 2020-11-28 14:00:11             🧑  作者: Mango
批检索操作返回单个或多个项目的属性。这些操作通常包括使用主键来标识所需的项目。 BatchGetItem操作受单个操作的限制以及它们自己的唯一约束。
批量检索操作中的以下请求导致拒绝-
批量检索操作对可能超出限制的请求执行部分处理。
例如-请求检索多个大小足够大以超过限制的项目会导致请求处理的一部分,并出现一条错误消息,指出未处理的部分。返回未处理的项目时,创建一个退避算法解决方案来管理该表而不是限制表。
BatchGet操作最终以一致的读取执行,需要对高度一致的读取进行修改。他们还并行执行检索。
注意-返回项目的顺序。 DynamoDB不对项目排序。它还不表示缺少所请求的项目。此外,这些请求消耗容量单位。
所有的BatchGet操作都需要RequestItems参数,例如读取一致性,属性名称和主键。
响应-成功的操作将导致HTTP 200响应,该响应指示诸如消耗的容量单位,表处理指标以及任何未处理项之类的特征。
在BatchGet操作使用Java需要创建DynamoDB类的实例,说明了项主键值列表,并传递TableKeysAndAttributes TableKeysAndAttributes类的实例对象的BatchGetItem方法。
以下是BatchGet操作的示例-
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- items = outcome.getTableItems().get(tableName);
for (Item item : items) {
System.out.println(item);
}
}
您可以查看以下较大的示例。
注–以下程序可能假定以前创建的数据源。在尝试执行之前,请获取支持库并创建必要的数据源(具有所需特征的表或其他引用的源)。
该程序还使用Eclipse IDE,AWS凭证文件和Eclipse AWS Java Project中的AWS Toolkit。
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 unprocessed = null;
do {
for (String tableName : outcome.getTableItems().keySet()) {
System.out.println("Table items for " + tableName);
List- 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());
}
}
}