aws java api_例如:使用 AWS SDK for Java 文档 API 的批量操作 - Amazon DynamoDB

冷吉星
2023-12-01

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

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

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

注意

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

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

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

在 Forum 表中放置一个项目。

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

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

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

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

/**

* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.

*

* This file is licensed under the Apache License, Version 2.0 (the "License").

* You may not use this file except in compliance with the License. A copy of

* the License is located at

*

* http://aws.amazon.com/apache2.0/

*

* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR

* CONDITIONS OF ANY KIND, either express or implied. See the License for the

* specific language governing permissions and limitations under the License.

*/

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(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> 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 中的代码示例创建表和加载数据部分的说明,为您的账户将数据加载到 DynamoDB 中。

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

/**

* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.

*

* This file is licensed under the Apache License, Version 2.0 (the "License").

* You may not use this file except in compliance with the License. A copy of

* the License is located at

*

* http://aws.amazon.com/apache2.0/

*

* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR

* CONDITIONS OF ANY KIND, either express or implied. See the License for the

* specific language governing permissions and limitations under the License.

*/

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 unprocessed = null;

do {

for (String tableName : outcome.getTableItems().keySet()) {

System.out.println("Items in table " + tableName);

List 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());

}

}

}

 类似资料: