Amazon DynamoDB
开发人员指南 (API Version 2012-08-10)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

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

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

注意

适用于 Java 的开发工具包 还提供一个对象持久化模型,可用来将客户端类映射到 DynamoDB 表。该方法可以减少您必须编写的代码数量。有关更多信息,请参阅 Java:DynamoDBMapper

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

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

  • 在 Forum 表中放置一个项目

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

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

以下示例查看响应,了解响应是否包含任何未处理的请求项目。如果存在,则循环返回,并重新发送包含请求中的未处理项目的 batchWriteItem 请求。如果您已按照创建表并加载示例数据部分进行操作,您应当已经创建了 Forum 和 Thread 表。您还能够以编程方式创建这些表和上传示例数据。有关更多信息,请参阅 使用适用于 Java 的 AWS 开发工具包创建示例表和上传数据

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

Copy
// Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. 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 方法检索 Forum 和 Thread 表中的多个项目。BatchGetItemRequest 为每个要获取的项目指定表名称和键列表。示例介绍通过打印检索到的项目来处理响应。

注意

此代码示例假定您已按照 创建表并加载示例数据 部分的说明,为您的账户将数据加载到了 DynamoDB 中。

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

Copy
// Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved. // Licensed under the Apache License, Version 2.0. 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()); } } }