D'autres AWS SDK exemples sont disponibles dans le GitHub dépôt AWS Doc SDK Examples
Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.
À utiliser BatchWriteItem
avec un AWS SDK ou CLI
Les exemples de code suivants montrent comment utiliserBatchWriteItem
.
Les exemples d’actions sont des extraits de code de programmes de plus grande envergure et doivent être exécutés en contexte. Vous pouvez voir cette action en contexte dans l’exemple de code suivant :
- .NET
-
- AWS SDK for .NET
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Écrit un lot d'éléments dans la table des films.
/// <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; }
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for .NET APIRéférence.
-
- Bash
-
- AWS CLI avec le script Bash
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. ############################################################################## # 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 }
Fonctions utilitaires utilisées dans cet exemple.
############################################################################### # 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 }
-
Pour API plus de détails, voir BatchWriteItemla section Référence des AWS CLI commandes.
-
- C++
-
- SDKpour C++
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. //! 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 false; } return outcome.IsSuccess(); } //! 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; }
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for C++ APIRéférence.
-
- CLI
-
- AWS CLI
-
Pour ajouter plusieurs éléments à un tableau
L'
batch-write-item
exemple suivant ajoute trois nouveaux éléments à laMusicCollection
table à l'aide d'un lot de troisPutItem
demandes. Il demande également des informations sur le nombre d'unités de capacité d'écriture consommées par l'opération et sur les collections d'articles modifiées par l'opération.aws dynamodb batch-write-item \ --request-items
file://request-items.json
\ --return-consumed-capacityINDEXES
\ --return-item-collection-metricsSIZE
Contenu de
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"} } } } ] }
Sortie :
{ "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 } } } ] }
Pour plus d'informations, consultez Batch Operations dans le manuel Amazon DynamoDB Developer Guide.
-
Pour API plus de détails, voir BatchWriteItem
la section Référence des AWS CLI commandes.
-
- Go
-
- SDKpour Go V2
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. import ( "context" "errors" "log" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" "github.com/aws/aws-sdk-go-v2/feature/dynamodb/expression" "github.com/aws/aws-sdk-go-v2/service/dynamodb" "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" ) // 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(ctx context.Context, 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(ctx, &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 }
Définissez une structure Movie utilisée dans cet exemple.
import ( "archive/zip" "bytes" "encoding/json" "fmt" "io" "log" "net/http" "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" ) // 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"]) }
-
Pour API plus de détails, voir BatchWriteItem
la section AWS SDK for Go APIRéférence.
-
- Java
-
- SDKpour Java 2.x
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Insère de nombreux éléments dans un tableau à l'aide du client de service.
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); } } }
Insère de nombreux éléments dans une table à l'aide du client amélioré.
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); } } }
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for Java 2.x APIRéférence.
-
- JavaScript
-
- SDKpour JavaScript (v3)
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. Cet exemple utilise le client de document pour simplifier l'utilisation d'éléments dans DynamoDB. Pour API plus de détails, voir BatchWrite.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { BatchWriteCommand, DynamoDBDocumentClient, } from "@aws-sdk/lib-dynamodb"; import { readFileSync } from "node: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); } };
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for JavaScript APIRéférence.
-
- SDKpour JavaScript (v2)
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. // 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); } });
-
Pour de plus amples informations, consultez le Guide du développeur AWS SDK for JavaScript.
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for JavaScript APIRéférence.
-
- PHP
-
- SDK pour PHP
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. 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; } } }
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for PHP APIRéférence.
-
- PowerShell
-
- Outils pour PowerShell
-
Exemple 1 : crée un nouvel élément ou remplace un élément existant par un nouvel élément dans les tables DynamoDB Music et Songs.
$item = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' AlbumTitle = 'Somewhat Famous' Price = 1.94 Genre = 'Country' CriticRating = 10.0 } | ConvertTo-DDBItem $writeRequest = New-Object Amazon.DynamoDBv2.Model.WriteRequest $writeRequest.PutRequest = [Amazon.DynamoDBv2.Model.PutRequest]$item
Sortie :
$requestItem = @{ 'Music' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) 'Songs' = [Amazon.DynamoDBv2.Model.WriteRequest]($writeRequest) } Set-DDBBatchItem -RequestItem $requestItem
-
Pour API plus de détails, consultez la section BatchWriteItemRéférence des AWS Tools for PowerShell applets de commande.
-
- Python
-
- SDKpour Python (Boto3)
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. class Movies: """Encapsulates an Amazon DynamoDB table of movie data. Example data structure for a movie record in this table: { "year": 1999, "title": "For Love of the Game", "info": { "directors": ["Sam Raimi"], "release_date": "1999-09-15T00:00:00Z", "rating": 6.3, "plot": "A washed up pitcher flashes through his career.", "rank": 4987, "running_time_secs": 8220, "actors": [ "Kevin Costner", "Kelly Preston", "John C. Reilly" ] } } """ 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
-
Pour API plus de détails, reportez-vous BatchWriteItemà la section AWS SDKpour la référence Python (Boto3). API
-
- Ruby
-
- SDKpour Ruby
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. class DynamoDBBasics attr_reader :dynamo_resource, :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
-
Pour API plus de détails, voir BatchWriteItemla section AWS SDK for Ruby APIRéférence.
-
- Swift
-
- SDKpour Swift
-
Note
Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS
. import AWSDynamoDB /// 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 { do { 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) } } catch { print("ERROR: populate:", dump(error)) throw error } }
-
Pour API plus de détails, reportez-vous BatchWriteItem
à AWS SDKla section dédiée à Swift API.
-