使用软件开发工具包编写一批 DynamoDB 项目 Amazon - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用软件开发工具包编写一批 DynamoDB 项目 Amazon

以下代码示例演示了如何写入一批 DynamoDB 项目。

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
Amazon SDK for .NET
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

将一批项目写入影片表。

/// <summary> /// Loads the contents of a JSON file into a list of movies to be /// added to the DynamoDB table. /// </summary> /// <param name="movieFileName">The full path to the JSON file.</param> /// <returns>A generic list of movie objects.</returns> public static List<Movie> ImportMovies(string movieFileName) { if (!File.Exists(movieFileName)) { return null; } using var sr = new StreamReader(movieFileName); string json = sr.ReadToEnd(); var allMovies = JsonSerializer.Deserialize<List<Movie>>( json, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); // Now return the first 250 entries. return allMovies.GetRange(0, 250); } /// <summary> /// Writes 250 items to the movie table. /// </summary> /// <param name="client">The initialized DynamoDB client object.</param> /// <param name="movieFileName">A string containing the full path to /// the JSON file containing movie data.</param> /// <returns>A long integer value representing the number of movies /// imported from the JSON file.</returns> public static async Task<long> BatchWriteItemsAsync( AmazonDynamoDBClient client, string movieFileName) { var movies = ImportMovies(movieFileName); if (movies is null) { Console.WriteLine("Couldn't find the JSON file with movie data."); return 0; } var context = new DynamoDBContext(client); var movieBatch = context.CreateBatchWrite<Movie>(); movieBatch.AddPutItems(movies); Console.WriteLine("Adding imported movies to the table."); await movieBatch.ExecuteAsync(); return movies.Count; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for .NET API 参考BatchWriteItem中的。

Bash
Amazon CLI 使用 Bash 脚本
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

############################################################################## # function dynamodb_batch_write_item # # This function writes a batch of items into a DynamoDB table. # # Parameters: # -i item -- Path to json file containing the items to write. # # Returns: # 0 - If successful. # 1 - If it fails. ############################################################################ function dynamodb_batch_write_item() { local item response local option OPTARG # Required to use getopts command in a function. ####################################### # Function usage explanation ####################################### function usage() { echo "function dynamodb_batch_write_item" echo "Write a batch of items into a DynamoDB table." echo " -i item -- Path to json file containing the items to write." echo "" } while getopts "i:h" option; do case "${option}" in i) item="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 if [[ -z "$item" ]]; then errecho "ERROR: You must provide an item with the -i parameter." usage return 1 fi iecho "Parameters:\n" iecho " table_name: $table_name" iecho " item: $item" iecho "" response=$(aws dynamodb batch-write-item \ --request-items file://"$item") local error_code=${?} if [[ $error_code -ne 0 ]]; then aws_cli_error_log $error_code errecho "ERROR: AWS reports batch-write-item operation failed.$response" return 1 fi return 0 }

本示例中使用的实用程序函数。

############################################################################### # function iecho # # This function enables the script to display the specified text only if # the global variable $VERBOSE is set to true. ############################################################################### function iecho() { if [[ $VERBOSE == true ]]; then echo "$@" fi } ############################################################################### # function errecho # # This function outputs everything sent to it to STDERR (standard error output). ############################################################################### function errecho() { printf "%s\n" "$*" 1>&2 } ############################################################################## # function aws_cli_error_log() # # This function is used to log the error messages from the AWS CLI. # # See https://docs.aws.amazon.com/cli/latest/topic/return-codes.html#cli-aws-help-return-codes. # # The function expects the following argument: # $1 - The error code returned by the AWS CLI. # # Returns: # 0: - Success. # ############################################################################## function aws_cli_error_log() { local err_code=$1 errecho "Error code : $err_code" if [ "$err_code" == 1 ]; then errecho " One or more S3 transfers failed." elif [ "$err_code" == 2 ]; then errecho " Command line failed to parse." elif [ "$err_code" == 130 ]; then errecho " Process received SIGINT." elif [ "$err_code" == 252 ]; then errecho " Command syntax invalid." elif [ "$err_code" == 253 ]; then errecho " The system environment or configuration was invalid." elif [ "$err_code" == 254 ]; then errecho " The service returned an error." elif [ "$err_code" == 255 ]; then errecho " 255 is a catch-all error." fi return 0 }
  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考BatchWriteItem中的。

C++
适用于 C++ 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

//! Batch write items from a JSON file. /*! \sa batchWriteItem() \param jsonFilePath: JSON file path. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The input for this routine is a JSON file that you can download from the following URL: * https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SampleData.html. * * The JSON data uses the BatchWriteItem API request syntax. The JSON strings are * converted to AttributeValue objects. These AttributeValue objects will then generate * JSON strings when constructing the BatchWriteItem request, essentially outputting * their input. * * This is perhaps an artificial example, but it demonstrates the APIs. */ bool AwsDoc::DynamoDB::batchWriteItem(const Aws::String &jsonFilePath, const Aws::Client::ClientConfiguration &clientConfiguration) { std::ifstream fileStream(jsonFilePath); if (!fileStream) { std::cerr << "Error: could not open file '" << jsonFilePath << "'." << std::endl; } std::stringstream stringStream; stringStream << fileStream.rdbuf(); Aws::Utils::Json::JsonValue jsonValue(stringStream); Aws::DynamoDB::Model::BatchWriteItemRequest batchWriteItemRequest; Aws::Map<Aws::String, Aws::Utils::Json::JsonView> level1Map = jsonValue.View().GetAllObjects(); for (const auto &level1Entry: level1Map) { const Aws::Utils::Json::JsonView &entriesView = level1Entry.second; const Aws::String &tableName = level1Entry.first; // The JSON entries at this level are as follows: // key - table name // value - list of request objects if (!entriesView.IsListType()) { std::cerr << "Error: JSON file entry '" << tableName << "' is not a list." << std::endl; continue; } Aws::Utils::Array<Aws::Utils::Json::JsonView> entries = entriesView.AsArray(); Aws::Vector<Aws::DynamoDB::Model::WriteRequest> writeRequests; if (AwsDoc::DynamoDB::addWriteRequests(tableName, entries, writeRequests)) { batchWriteItemRequest.AddRequestItems(tableName, writeRequests); } } Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); Aws::DynamoDB::Model::BatchWriteItemOutcome outcome = dynamoClient.BatchWriteItem( batchWriteItemRequest); if (outcome.IsSuccess()) { std::cout << "DynamoDB::BatchWriteItem was successful." << std::endl; } else { std::cerr << "Error with DynamoDB::BatchWriteItem. " << outcome.GetError().GetMessage() << std::endl; } return true; } //! Convert requests in JSON format to a vector of WriteRequest objects. /*! \sa addWriteRequests() \param tableName: Name of the table for the write operations. \param requestsJson: Request data in JSON format. \param writeRequests: Vector to receive the WriteRequest objects. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::addWriteRequests(const Aws::String &tableName, const Aws::Utils::Array<Aws::Utils::Json::JsonView> &requestsJson, Aws::Vector<Aws::DynamoDB::Model::WriteRequest> &writeRequests) { for (size_t i = 0; i < requestsJson.GetLength(); ++i) { const Aws::Utils::Json::JsonView &requestsEntry = requestsJson[i]; if (!requestsEntry.IsObject()) { std::cerr << "Error: incorrect requestsEntry type " << requestsEntry.WriteReadable() << std::endl; return false; } Aws::Map<Aws::String, Aws::Utils::Json::JsonView> requestsMap = requestsEntry.GetAllObjects(); for (const auto &request: requestsMap) { const Aws::String &requestType = request.first; const Aws::Utils::Json::JsonView &requestJsonView = request.second; if (requestType == "PutRequest") { if (!requestJsonView.ValueExists("Item")) { std::cerr << "Error: item key missing for requests " << requestJsonView.WriteReadable() << std::endl; return false; } Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> attributes; if (!getAttributeObjectsMap(requestJsonView.GetObject("Item"), attributes)) { std::cerr << "Error getting attributes " << requestJsonView.WriteReadable() << std::endl; return false; } Aws::DynamoDB::Model::PutRequest putRequest; putRequest.SetItem(attributes); writeRequests.push_back( Aws::DynamoDB::Model::WriteRequest().WithPutRequest( putRequest)); } else { std::cerr << "Error: unimplemented request type '" << requestType << "'." << std::endl; } } } return true; } //! Generate a map of AttributeValue objects from JSON records. /*! \sa getAttributeObjectsMap() \param jsonView: JSONView of attribute records. \param writeRequests: Map to receive the AttributeValue objects. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::getAttributeObjectsMap(const Aws::Utils::Json::JsonView &jsonView, Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> &attributes) { Aws::Map<Aws::String, Aws::Utils::Json::JsonView> objectsMap = jsonView.GetAllObjects(); for (const auto &entry: objectsMap) { const Aws::String &attributeKey = entry.first; const Aws::Utils::Json::JsonView &attributeJsonView = entry.second; if (!attributeJsonView.IsObject()) { std::cerr << "Error: attribute not an object " << attributeJsonView.WriteReadable() << std::endl; return false; } attributes.emplace(attributeKey, Aws::DynamoDB::Model::AttributeValue(attributeJsonView)); } return true; }
  • 有关 API 的详细信息,请参阅 Amazon SDK for C++ API 参考BatchWriteItem中的。

CLI
Amazon CLI

向表中添加多个项

以下 batch-write-item 示例将使用一批三个 PutItem 请求向 MusicCollection 表中添加三个新项。它还会请求有关操作所用的写入容量单位数以及操作修改的任何项集合的信息。

aws dynamodb batch-write-item \ --request-items file://request-items.json \ --return-consumed-capacity INDEXES \ --return-item-collection-metrics SIZE

request-items.json 的内容:

{ "MusicCollection": [ { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Call Me Today"}, "AlbumTitle": {"S": "Somewhat Famous"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"}, "AlbumTitle": {"S": "Songs About Life"} } } }, { "PutRequest": { "Item": { "Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Scared of My Shadow"}, "AlbumTitle": {"S": "Blue Sky Blues"} } } } ] }

输出:

{ "UnprocessedItems": {}, "ItemCollectionMetrics": { "MusicCollection": [ { "ItemCollectionKey": { "Artist": { "S": "No One You Know" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] }, { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } ] }, "ConsumedCapacity": [ { "TableName": "MusicCollection", "CapacityUnits": 6.0, "Table": { "CapacityUnits": 3.0 }, "LocalSecondaryIndexes": { "AlbumTitleIndex": { "CapacityUnits": 3.0 } } } ] }

有关更多信息,请参阅《Amazon DynamoDB 开发人员指南》中的基本操作

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考BatchWriteItem中的。

Go
适用于 Go V2 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// TableBasics encapsulates the Amazon DynamoDB service actions used in the examples. // It contains a DynamoDB service client that is used to act on the specified table. type TableBasics struct { DynamoDbClient *dynamodb.Client TableName string } // AddMovieBatch adds a slice of movies to the DynamoDB table. The function sends // batches of 25 movies to DynamoDB until all movies are added or it reaches the // specified maximum. func (basics TableBasics) AddMovieBatch(movies []Movie, maxMovies int) (int, error) { var err error var item map[string]types.AttributeValue written := 0 batchSize := 25 // DynamoDB allows a maximum batch size of 25 items. start := 0 end := start + batchSize for start < maxMovies && start < len(movies) { var writeReqs []types.WriteRequest if end > len(movies) { end = len(movies) } for _, movie := range movies[start:end] { item, err = attributevalue.MarshalMap(movie) if err != nil { log.Printf("Couldn't marshal movie %v for batch writing. Here's why: %v\n", movie.Title, err) } else { writeReqs = append( writeReqs, types.WriteRequest{PutRequest: &types.PutRequest{Item: item}}, ) } } _, err = basics.DynamoDbClient.BatchWriteItem(context.TODO(), &dynamodb.BatchWriteItemInput{ RequestItems: map[string][]types.WriteRequest{basics.TableName: writeReqs}}) if err != nil { log.Printf("Couldn't add a batch of movies to %v. Here's why: %v\n", basics.TableName, err) } else { written += len(writeReqs) } start = end end += batchSize } return written, err } // Movie encapsulates data about a movie. Title and Year are the composite primary key // of the movie in Amazon DynamoDB. Title is the sort key, Year is the partition key, // and Info is additional data. type Movie struct { Title string `dynamodbav:"title"` Year int `dynamodbav:"year"` Info map[string]interface{} `dynamodbav:"info"` } // GetKey returns the composite primary key of the movie in a format that can be // sent to DynamoDB. func (movie Movie) GetKey() map[string]types.AttributeValue { title, err := attributevalue.Marshal(movie.Title) if err != nil { panic(err) } year, err := attributevalue.Marshal(movie.Year) if err != nil { panic(err) } return map[string]types.AttributeValue{"title": title, "year": year} } // String returns the title, year, rating, and plot of a movie, formatted for the example. func (movie Movie) String() string { return fmt.Sprintf("%v\n\tReleased: %v\n\tRating: %v\n\tPlot: %v\n", movie.Title, movie.Year, movie.Info["rating"], movie.Info["plot"]) }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Go API 参考BatchWriteItem中的。

Java
适用于 Java 2.x 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

使用服务客户端将许多项目插入表中。

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.BatchWriteItemRequest; import software.amazon.awssdk.services.dynamodb.model.BatchWriteItemResponse; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.PutRequest; import software.amazon.awssdk.services.dynamodb.model.WriteRequest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Before running this Java V2 code example, set up your development environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class BatchWriteItems { public static void main(String[] args){ final String usage = """ Usage: <tableName> Where: tableName - The Amazon DynamoDB table (for example, Music).\s """; String tableName = "Music"; Region region = Region.US_EAST_1; DynamoDbClient dynamoDbClient = DynamoDbClient.builder() .region(region) .build(); addBatchItems(dynamoDbClient, tableName); } public static void addBatchItems(DynamoDbClient dynamoDbClient, String tableName) { // Specify the updates you want to perform. List<WriteRequest> writeRequests = new ArrayList<>(); // Set item 1. Map<String, AttributeValue> item1Attributes = new HashMap<>(); item1Attributes.put("Artist", AttributeValue.builder().s("Artist1").build()); item1Attributes.put("Rating", AttributeValue.builder().s("5").build()); item1Attributes.put("Comments", AttributeValue.builder().s("Great song!").build()); item1Attributes.put("SongTitle", AttributeValue.builder().s("SongTitle1").build()); writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(item1Attributes).build()).build()); // Set item 2. Map<String, AttributeValue> item2Attributes = new HashMap<>(); item2Attributes.put("Artist", AttributeValue.builder().s("Artist2").build()); item2Attributes.put("Rating", AttributeValue.builder().s("4").build()); item2Attributes.put("Comments", AttributeValue.builder().s("Nice melody.").build()); item2Attributes.put("SongTitle", AttributeValue.builder().s("SongTitle2").build()); writeRequests.add(WriteRequest.builder().putRequest(PutRequest.builder().item(item2Attributes).build()).build()); try { // Create the BatchWriteItemRequest. BatchWriteItemRequest batchWriteItemRequest = BatchWriteItemRequest.builder() .requestItems(Map.of(tableName, writeRequests)) .build(); // Execute the BatchWriteItem operation. BatchWriteItemResponse batchWriteItemResponse = dynamoDbClient.batchWriteItem(batchWriteItemRequest); // Process the response. System.out.println("Batch write successful: " + batchWriteItemResponse); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } }

使用增强型客户端将许多项目插入到表中。

import com.example.dynamodb.Customer; import com.example.dynamodb.Music; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient; import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable; import software.amazon.awssdk.enhanced.dynamodb.Key; import software.amazon.awssdk.enhanced.dynamodb.TableSchema; import software.amazon.awssdk.enhanced.dynamodb.model.BatchWriteItemEnhancedRequest; import software.amazon.awssdk.enhanced.dynamodb.model.WriteBatch; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneOffset; /* * Before running this code example, create an Amazon DynamoDB table named Customer with these columns: * - id - the id of the record that is the key * - custName - the customer name * - email - the email value * - registrationDate - an instant value when the item was added to the table * * Also, ensure that you have set up your development environment, including your credentials. * * For information, see this documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class EnhancedBatchWriteItems { public static void main(String[] args) { Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(ddb) .build(); putBatchRecords(enhancedClient); ddb.close(); } public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) { try { DynamoDbTable<Customer> customerMappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class)); DynamoDbTable<Music> musicMappedTable = enhancedClient.table("Music", TableSchema.fromBean(Music.class)); LocalDate localDate = LocalDate.parse("2020-04-07"); LocalDateTime localDateTime = localDate.atStartOfDay(); Instant instant = localDateTime.toInstant(ZoneOffset.UTC); Customer record2 = new Customer(); record2.setCustName("Fred Pink"); record2.setId("id110"); record2.setEmail("fredp@noserver.com"); record2.setRegistrationDate(instant); Customer record3 = new Customer(); record3.setCustName("Susan Pink"); record3.setId("id120"); record3.setEmail("spink@noserver.com"); record3.setRegistrationDate(instant); Customer record4 = new Customer(); record4.setCustName("Jerry orange"); record4.setId("id101"); record4.setEmail("jorange@noserver.com"); record4.setRegistrationDate(instant); BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest .builder() .writeBatches( WriteBatch.builder(Customer.class) // add items to the Customer // table .mappedTableResource(customerMappedTable) .addPutItem(builder -> builder.item(record2)) .addPutItem(builder -> builder.item(record3)) .addPutItem(builder -> builder.item(record4)) .build(), WriteBatch.builder(Music.class) // delete an item from the Music // table .mappedTableResource(musicMappedTable) .addDeleteItem(builder -> builder.key( Key.builder().partitionValue( "Famous Band") .build())) .build()) .build(); // Add three items to the Customer table and delete one item from the Music // table. enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest); System.out.println("done"); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for Java 2.x API 参考BatchWriteItem中的。

JavaScript
适用于 JavaScript (v3) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

此示例使用文档客户端来简化在 DynamoDB 中处理项目的过程。有关 API 的详细信息,请参阅BatchWrite

import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { BatchWriteCommand, DynamoDBDocumentClient, } from "@aws-sdk/lib-dynamodb"; import { readFileSync } from "fs"; // These modules are local to our GitHub repository. We recommend cloning // the project from GitHub if you want to run this example. // For more information, see https://github.com/awsdocs/aws-doc-sdk-examples. import { dirnameFromMetaUrl } from "@aws-doc-sdk-examples/lib/utils/util-fs.js"; import { chunkArray } from "@aws-doc-sdk-examples/lib/utils/util-array.js"; const dirname = dirnameFromMetaUrl(import.meta.url); const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const file = readFileSync( `${dirname}../../../../../resources/sample_files/movies.json`, ); const movies = JSON.parse(file.toString()); // chunkArray is a local convenience function. It takes an array and returns // a generator function. The generator function yields every N items. const movieChunks = chunkArray(movies, 25); // For every chunk of 25 movies, make one BatchWrite request. for (const chunk of movieChunks) { const putRequests = chunk.map((movie) => ({ PutRequest: { Item: movie, }, })); const command = new BatchWriteCommand({ RequestItems: { // An existing table is required. A composite key of 'title' and 'year' is recommended // to account for duplicate titles. ["BatchWriteMoviesTable"]: putRequests, }, }); await docClient.send(command); } };
  • 有关 API 的详细信息,请参阅 Amazon SDK for JavaScript API 参考BatchWriteItem中的。

适用于 JavaScript (v2) 的软件开发工具包
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create DynamoDB service object var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); var params = { RequestItems: { TABLE_NAME: [ { PutRequest: { Item: { KEY: { N: "KEY_VALUE" }, ATTRIBUTE_1: { S: "ATTRIBUTE_1_VALUE" }, ATTRIBUTE_2: { N: "ATTRIBUTE_2_VALUE" }, }, }, }, { PutRequest: { Item: { KEY: { N: "KEY_VALUE" }, ATTRIBUTE_1: { S: "ATTRIBUTE_1_VALUE" }, ATTRIBUTE_2: { N: "ATTRIBUTE_2_VALUE" }, }, }, }, ], }, }; ddb.batchWriteItem(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
PHP
适用于 PHP 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

public function writeBatch(string $TableName, array $Batch, int $depth = 2) { if (--$depth <= 0) { throw new Exception("Max depth exceeded. Please try with fewer batch items or increase depth."); } $marshal = new Marshaler(); $total = 0; foreach (array_chunk($Batch, 25) as $Items) { foreach ($Items as $Item) { $BatchWrite['RequestItems'][$TableName][] = ['PutRequest' => ['Item' => $marshal->marshalItem($Item)]]; } try { echo "Batching another " . count($Items) . " for a total of " . ($total += count($Items)) . " items!\n"; $response = $this->dynamoDbClient->batchWriteItem($BatchWrite); $BatchWrite = []; } catch (Exception $e) { echo "uh oh..."; echo $e->getMessage(); die(); } if ($total >= 250) { echo "250 movies is probably enough. Right? We can stop there.\n"; break; } } }
  • 有关 API 的详细信息,请参阅 Amazon SDK for PHP API 参考BatchWriteItem中的。

Python
适用于 Python (Boto3) 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

class Movies: """Encapsulates an Amazon DynamoDB table of movie data.""" def __init__(self, dyn_resource): """ :param dyn_resource: A Boto3 DynamoDB resource. """ self.dyn_resource = dyn_resource # The table variable is set during the scenario in the call to # 'exists' if the table exists. Otherwise, it is set by 'create_table'. self.table = None def write_batch(self, movies): """ Fills an Amazon DynamoDB table with the specified data, using the Boto3 Table.batch_writer() function to put the items in the table. Inside the context manager, Table.batch_writer builds a list of requests. On exiting the context manager, Table.batch_writer starts sending batches of write requests to Amazon DynamoDB and automatically handles chunking, buffering, and retrying. :param movies: The data to put in the table. Each item must contain at least the keys required by the schema that was specified when the table was created. """ try: with self.table.batch_writer() as writer: for movie in movies: writer.put_item(Item=movie) except ClientError as err: logger.error( "Couldn't load data into table %s. Here's why: %s: %s", self.table.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • 有关 API 的详细信息,请参阅适用BatchWriteItemPython 的 Amazon SDK (Boto3) API 参考

Ruby
适用于 Ruby 的 SDK
注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

class DynamoDBBasics attr_reader :dynamo_resource attr_reader :table def initialize(table_name) client = Aws::DynamoDB::Client.new(region: "us-east-1") @dynamo_resource = Aws::DynamoDB::Resource.new(client: client) @table = @dynamo_resource.table(table_name) end # Fills an Amazon DynamoDB table with the specified data. Items are sent in # batches of 25 until all items are written. # # @param movies [Enumerable] The data to put in the table. Each item must contain at least # the keys required by the schema that was specified when the # table was created. def write_batch(movies) index = 0 slice_size = 25 while index < movies.length movie_items = [] movies[index, slice_size].each do |movie| movie_items.append({put_request: { item: movie }}) end @dynamo_resource.client.batch_write_item({request_items: { @table.name => movie_items }}) index += slice_size end rescue Aws::DynamoDB::Errors::ServiceError => e puts( "Couldn't load data into table #{@table.name}. Here's why:") puts("\t#{e.code}: #{e.message}") raise end
  • 有关 API 的详细信息,请参阅 Amazon SDK for Ruby API 参考BatchWriteItem中的。

Swift
SDK for Swift
注意

这是预览版 SDK 的预发布文档。本文档随时可能更改。

注意

还有更多相关信息 GitHub。在 Amazon 代码示例存储库中查找完整示例,了解如何进行设置和运行。

/// Populate the movie database from the specified JSON file. /// /// - Parameter jsonPath: Path to a JSON file containing movie data. /// func populate(jsonPath: String) async throws { guard let client = self.ddbClient else { throw MoviesError.UninitializedClient } // Create a Swift `URL` and use it to load the file into a `Data` // object. Then decode the JSON into an array of `Movie` objects. let fileUrl = URL(fileURLWithPath: jsonPath) let jsonData = try Data(contentsOf: fileUrl) var movieList = try JSONDecoder().decode([Movie].self, from: jsonData) // Truncate the list to the first 200 entries or so for this example. if movieList.count > 200 { movieList = Array(movieList[...199]) } // Before sending records to the database, break the movie list into // 25-entry chunks, which is the maximum size of a batch item request. let count = movieList.count let chunks = stride(from: 0, to: count, by: 25).map { Array(movieList[$0 ..< Swift.min($0 + 25, count)]) } // For each chunk, create a list of write request records and populate // them with `PutRequest` requests, each specifying one movie from the // chunk. Once the chunk's items are all in the `PutRequest` list, // send them to Amazon DynamoDB using the // `DynamoDBClient.batchWriteItem()` function. for chunk in chunks { var requestList: [DynamoDBClientTypes.WriteRequest] = [] for movie in chunk { let item = try await movie.getAsItem() let request = DynamoDBClientTypes.WriteRequest( putRequest: .init( item: item ) ) requestList.append(request) } let input = BatchWriteItemInput(requestItems: [tableName: requestList]) _ = try await client.batchWriteItem(input: input) } }
  • 有关 API 的详细信息,请参阅适用于 S wift 的 Amazon SDK API 参考BatchWriteItem中。

有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅结合使用 DynamoDB 与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。