示例:使用 AWS SDK for Java 文档 API 的批处理操作 - Amazon DynamoDB

示例:使用 AWS SDK for Java 文档 API 的批处理操作

本部分提供在 Amazon DynamoDB 中使用 AWS SDK for Java 文档 API 执行批量写入和批量获取操作的示例。

注意

SDK for Java 还提供一个对象持久化模型,可用来将客户端类映射到 DynamoDB 表。该方法可以减少需要编写的代码数量。有关更多信息,请参阅 Java 1.x:DynamoDBMapper

示例:使用 AWS SDK for Java 文档 API 的批处理写入操作

以下 Java 代码示例使用 batchWriteItem 方法执行以下放置和删除操作:

  • Forum 表中放置一个项目。

  • Thread 表中放置一个项目并删除一个项目。

在创建批量写入请求时,您可以就一个或多个表指定任意数量的放置和删除请求。但是,batchWriteItem 对批量写入请求的大小,以及单个批量写入操作中的放置和删除操作数量有限制。如果您的请求超出这些限制,请求会遭到拒绝。如果您的表的预置吞吐量不足,无法处理此请求,那么响应将返回未处理的请求项目。

以下示例查看响应,了解响应是否包含任何未处理的请求项目。如果存在,则循环返回,并重新发送包含请求中的未处理项目的 batchWriteItem 请求。如果您遵循该指南中的示例,则应已创建 ForumThread 表。您还能够以编程方式创建这些表和上传示例数据。有关更多信息,请参阅 使用 AWS SDK for Java 创建表示例并上传数据

有关测试以下示例的分步说明,请参阅 Java 代码示例

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.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; 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 DocumentAPIBatchWrite { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); static DynamoDB dynamoDB = new DynamoDB(client); static String forumTableName = "Forum"; static String threadTableName = "Thread"; public static void main(String[] args) throws IOException { writeMultipleItemsBatchWrite(); } private static void writeMultipleItemsBatchWrite() { try { // Add a new item to Forum TableWriteItems forumTableWriteItems = new TableWriteItems(forumTableName) // Forum .withItemsToPut(new Item().withPrimaryKey("Name", "Amazon RDS").withNumber("Threads", 0)); // Add a new item, and delete an existing item, from Thread // This table has a partition key and range key, so need to specify // both of them TableWriteItems threadTableWriteItems = new TableWriteItems(threadTableName) .withItemsToPut( new Item().withPrimaryKey("ForumName", "Amazon RDS", "Subject", "Amazon RDS Thread 1") .withString("Message", "ElastiCache Thread 1 message") .withStringSet("Tags", new HashSet<String>(Arrays.asList("cache", "in-memory")))) .withHashAndRangeKeysToDelete("ForumName", "Subject", "Amazon S3", "S3 Thread 100"); System.out.println("Making the request."); BatchWriteItemOutcome outcome = dynamoDB.batchWriteItem(forumTableWriteItems, threadTableWriteItems); do { // Check for unprocessed keys which could happen if you exceed // provisioned throughput Map<String, List<WriteRequest>> unprocessedItems = outcome.getUnprocessedItems(); if (outcome.getUnprocessedItems().size() == 0) { System.out.println("No unprocessed items found"); } else { System.out.println("Retrieving the unprocessed items"); outcome = dynamoDB.batchWriteItemUnprocessed(unprocessedItems); } } while (outcome.getUnprocessedItems().size() > 0); } catch (Exception e) { System.err.println("Failed to retrieve items: "); e.printStackTrace(System.err); } } }

示例:使用 AWS SDK for Java 文档 API 的批处理获取操作

以下 Java 代码示例使用 batchGetItem 方法检索 ForumThread 表中的多个项目。BatchGetItemRequest 为每个要获取的项目指定表名称和键列表。示例介绍通过打印检索到的项目来处理响应。

注意

此代码示例假定您已将数据加载到您的帐户的 DynamoDB 中,方法是按照 为 DynamoDB 中的代码示例创建表和加载数据 部分。

有关运行以下示例的分步说明,请参阅 Java 代码示例

package com.amazonaws.codesamples.document; import java.io.IOException; import java.util.List; import java.util.Map; import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; 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 DocumentAPIBatchGet { static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build(); static DynamoDB dynamoDB = new DynamoDB(client); 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); // Add a partition key forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name", "Amazon S3", "Amazon DynamoDB"); TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName); // Add a partition key and a sort key threadTableKeysAndAttributes.addHashAndRangePrimaryKeys("ForumName", "Subject", "Amazon DynamoDB", "DynamoDB Thread 1", "Amazon DynamoDB", "DynamoDB Thread 2", "Amazon S3", "S3 Thread 1"); System.out.println("Making the request."); BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes, threadTableKeysAndAttributes); Map<String, KeysAndAttributes> unprocessed = null; do { for (String tableName : outcome.getTableItems().keySet()) { System.out.println("Items in table " + tableName); List<Item> items = outcome.getTableItems().get(tableName); for (Item item : items) { System.out.println(item.toJSONPretty()); } } // Check for unprocessed keys which could happen if you exceed // provisioned // throughput or reach the limit on response size. unprocessed = outcome.getUnprocessedKeys(); if (unprocessed.isEmpty()) { System.out.println("No unprocessed keys found"); } else { System.out.println("Retrieving the unprocessed keys"); outcome = dynamoDB.batchGetItemUnprocessed(unprocessed); } } while (!unprocessed.isEmpty()); } catch (Exception e) { System.err.println("Failed to retrieve items."); System.err.println(e.getMessage()); } } }