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 UpdateItem
avec un AWS SDK ou CLI
Les exemples de code suivants montrent comment utiliserUpdateItem
.
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 les exemples de code suivants :
- .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
. /// <summary> /// Updates an existing item in the movies table. /// </summary> /// <param name="client">An initialized Amazon DynamoDB client object.</param> /// <param name="newMovie">A Movie object containing information for /// the movie to update.</param> /// <param name="newInfo">A MovieInfo object that contains the /// information that will be changed.</param> /// <param name="tableName">The name of the table that contains the movie.</param> /// <returns>A Boolean value that indicates the success of the operation.</returns> public static async Task<bool> UpdateItemAsync( AmazonDynamoDBClient client, Movie newMovie, MovieInfo newInfo, string tableName) { var key = new Dictionary<string, AttributeValue> { ["title"] = new AttributeValue { S = newMovie.Title }, ["year"] = new AttributeValue { N = newMovie.Year.ToString() }, }; var updates = new Dictionary<string, AttributeValueUpdate> { ["info.plot"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { S = newInfo.Plot }, }, ["info.rating"] = new AttributeValueUpdate { Action = AttributeAction.PUT, Value = new AttributeValue { N = newInfo.Rank.ToString() }, }, }; var request = new UpdateItemRequest { AttributeUpdates = updates, Key = key, TableName = tableName, }; var response = await client.UpdateItemAsync(request); return response.HttpStatusCode == System.Net.HttpStatusCode.OK; }
-
Pour API plus de détails, voir UpdateItemla 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_update_item # # This function updates an item in a DynamoDB table. # # # Parameters: # -n table_name -- The name of the table. # -k keys -- Path to json file containing the keys that identify the item to update. # -e update expression -- An expression that defines one or more attributes to be updated. # -v values -- Path to json file containing the update values. # # Returns: # 0 - If successful. # 1 - If it fails. ############################################################################# function dynamodb_update_item() { local table_name keys update_expression values response local option OPTARG # Required to use getopts command in a function. ####################################### # Function usage explanation ####################################### function usage() { echo "function dynamodb_update_item" echo "Update an item in a DynamoDB table." echo " -n table_name -- The name of the table." echo " -k keys -- Path to json file containing the keys that identify the item to update." echo " -e update expression -- An expression that defines one or more attributes to be updated." echo " -v values -- Path to json file containing the update values." echo "" } while getopts "n:k:e:v:h" option; do case "${option}" in n) table_name="${OPTARG}" ;; k) keys="${OPTARG}" ;; e) update_expression="${OPTARG}" ;; v) values="${OPTARG}" ;; h) usage return 0 ;; \?) echo "Invalid parameter" usage return 1 ;; esac done export OPTIND=1 if [[ -z "$table_name" ]]; then errecho "ERROR: You must provide a table name with the -n parameter." usage return 1 fi if [[ -z "$keys" ]]; then errecho "ERROR: You must provide a keys json file path the -k parameter." usage return 1 fi if [[ -z "$update_expression" ]]; then errecho "ERROR: You must provide an update expression with the -e parameter." usage return 1 fi if [[ -z "$values" ]]; then errecho "ERROR: You must provide a values json file path the -v parameter." usage return 1 fi iecho "Parameters:\n" iecho " table_name: $table_name" iecho " keys: $keys" iecho " update_expression: $update_expression" iecho " values: $values" response=$(aws dynamodb update-item \ --table-name "$table_name" \ --key file://"$keys" \ --update-expression "$update_expression" \ --expression-attribute-values file://"$values") local error_code=${?} if [[ $error_code -ne 0 ]]; then aws_cli_error_log $error_code errecho "ERROR: AWS reports update-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 UpdateItemla 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
. //! Update an Amazon DynamoDB table item. /*! \sa updateItem() \param tableName: The table name. \param partitionKey: The partition key. \param partitionValue: The value for the partition key. \param attributeKey: The key for the attribute to be updated. \param attributeValue: The value for the attribute to be updated. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ /* * The example code only sets/updates an attribute value. It processes * the attribute value as a string, even if the value could be interpreted * as a number. Also, the example code does not remove an existing attribute * from the key value. */ bool AwsDoc::DynamoDB::updateItem(const Aws::String &tableName, const Aws::String &partitionKey, const Aws::String &partitionValue, const Aws::String &attributeKey, const Aws::String &attributeValue, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::DynamoDB::DynamoDBClient dynamoClient(clientConfiguration); // *** Define UpdateItem request arguments. // Define TableName argument. Aws::DynamoDB::Model::UpdateItemRequest request; request.SetTableName(tableName); // Define KeyName argument. Aws::DynamoDB::Model::AttributeValue attribValue; attribValue.SetS(partitionValue); request.AddKey(partitionKey, attribValue); // Construct the SET update expression argument. Aws::String update_expression("SET #a = :valueA"); request.SetUpdateExpression(update_expression); // Construct attribute name argument. Aws::Map<Aws::String, Aws::String> expressionAttributeNames; expressionAttributeNames["#a"] = attributeKey; request.SetExpressionAttributeNames(expressionAttributeNames); // Construct attribute value argument. Aws::DynamoDB::Model::AttributeValue attributeUpdatedValue; attributeUpdatedValue.SetS(attributeValue); Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> expressionAttributeValues; expressionAttributeValues[":valueA"] = attributeUpdatedValue; request.SetExpressionAttributeValues(expressionAttributeValues); // Update the item. const Aws::DynamoDB::Model::UpdateItemOutcome &outcome = dynamoClient.UpdateItem( request); if (outcome.IsSuccess()) { std::cout << "Item was updated" << std::endl; } else { std::cerr << outcome.GetError().GetMessage() << std::endl; return false; } return waitTableActive(tableName, dynamoClient); }
Code qui attend que la table soit active.
//! Query a newly created DynamoDB table until it is active. /*! \sa waitTableActive() \param waitTableActive: The DynamoDB table's name. \param dynamoClient: A DynamoDB client. \return bool: Function succeeded. */ bool AwsDoc::DynamoDB::waitTableActive(const Aws::String &tableName, const Aws::DynamoDB::DynamoDBClient &dynamoClient) { // Repeatedly call DescribeTable until table is ACTIVE. const int MAX_QUERIES = 20; Aws::DynamoDB::Model::DescribeTableRequest request; request.SetTableName(tableName); int count = 0; while (count < MAX_QUERIES) { const Aws::DynamoDB::Model::DescribeTableOutcome &result = dynamoClient.DescribeTable( request); if (result.IsSuccess()) { Aws::DynamoDB::Model::TableStatus status = result.GetResult().GetTable().GetTableStatus(); if (Aws::DynamoDB::Model::TableStatus::ACTIVE != status) { std::this_thread::sleep_for(std::chrono::seconds(1)); } else { return true; } } else { std::cerr << "Error DynamoDB::waitTableActive " << result.GetError().GetMessage() << std::endl; return false; } count++; } return false; }
-
Pour API plus de détails, voir UpdateItemla section AWS SDK for C++ APIRéférence.
-
- CLI
-
- AWS CLI
-
Exemple 1 : pour mettre à jour un élément dans un tableau
L'exemple d'
update-item
suivant met à jour un élément à partir de la tableMusicCollection
. Il ajoute un nouvel attribut (Year
) et modifie l'AlbumTitle
attribut. Tous les attributs de l'élément, tels qu'ils apparaissent après la mise à jour, sont renvoyés dans la réponse.aws dynamodb update-item \ --table-name
MusicCollection
\ --keyfile://key.json
\ --update-expression"SET #Y = :y, #AT = :t"
\ --expression-attribute-namesfile://expression-attribute-names.json
\ --expression-attribute-valuesfile://expression-attribute-values.json
\ --return-valuesALL_NEW
\ --return-consumed-capacityTOTAL
\ --return-item-collection-metricsSIZE
Contenu de
key.json
:{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }
Contenu de
expression-attribute-names.json
:{ "#Y":"Year", "#AT":"AlbumTitle" }
Contenu de
expression-attribute-values.json
:{ ":y":{"N": "2015"}, ":t":{"S": "Louder Than Ever"} }
Sortie :
{ "Attributes": { "AlbumTitle": { "S": "Louder Than Ever" }, "Awards": { "N": "10" }, "Artist": { "S": "Acme Band" }, "Year": { "N": "2015" }, "SongTitle": { "S": "Happy Day" } }, "ConsumedCapacity": { "TableName": "MusicCollection", "CapacityUnits": 3.0 }, "ItemCollectionMetrics": { "ItemCollectionKey": { "Artist": { "S": "Acme Band" } }, "SizeEstimateRangeGB": [ 0.0, 1.0 ] } }
Pour plus d'informations, consultez la section Rédaction d'un article dans le guide du développeur Amazon DynamoDB.
Exemple 2 : pour mettre à jour un élément de manière conditionnelle
L'exemple suivant met à jour un élément de la
MusicCollection
table, mais uniquement si l'élément existant ne possède pas encore d'Year
attribut.aws dynamodb update-item \ --table-name
MusicCollection
\ --keyfile://key.json
\ --update-expression"SET #Y = :y, #AT = :t"
\ --expression-attribute-namesfile://expression-attribute-names.json
\ --expression-attribute-valuesfile://expression-attribute-values.json
\ --condition-expression"attribute_not_exists(#Y)"
Contenu de
key.json
:{ "Artist": {"S": "Acme Band"}, "SongTitle": {"S": "Happy Day"} }
Contenu de
expression-attribute-names.json
:{ "#Y":"Year", "#AT":"AlbumTitle" }
Contenu de
expression-attribute-values.json
:{ ":y":{"N": "2015"}, ":t":{"S": "Louder Than Ever"} }
Si l'élément possède déjà un
Year
attribut, DynamoDB renvoie le résultat suivant.An error occurred (ConditionalCheckFailedException) when calling the UpdateItem operation: The conditional request failed
Pour plus d'informations, consultez la section Rédaction d'un article dans le guide du développeur Amazon DynamoDB.
-
Pour API plus de détails, voir UpdateItem
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
. // 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 } // UpdateMovie updates the rating and plot of a movie that already exists in the // DynamoDB table. This function uses the `expression` package to build the update // expression. func (basics TableBasics) UpdateMovie(ctx context.Context, movie Movie) (map[string]map[string]interface{}, error) { var err error var response *dynamodb.UpdateItemOutput var attributeMap map[string]map[string]interface{} update := expression.Set(expression.Name("info.rating"), expression.Value(movie.Info["rating"])) update.Set(expression.Name("info.plot"), expression.Value(movie.Info["plot"])) expr, err := expression.NewBuilder().WithUpdate(update).Build() if err != nil { log.Printf("Couldn't build expression for update. Here's why: %v\n", err) } else { response, err = basics.DynamoDbClient.UpdateItem(ctx, &dynamodb.UpdateItemInput{ TableName: aws.String(basics.TableName), Key: movie.GetKey(), ExpressionAttributeNames: expr.Names(), ExpressionAttributeValues: expr.Values(), UpdateExpression: expr.Update(), ReturnValues: types.ReturnValueUpdatedNew, }) if err != nil { log.Printf("Couldn't update movie %v. Here's why: %v\n", movie.Title, err) } else { err = attributevalue.UnmarshalMap(response.Attributes, &attributeMap) if err != nil { log.Printf("Couldn't unmarshall update response. Here's why: %v\n", err) } } } return attributeMap, 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"]) }
-
Pour API plus de détails, voir UpdateItem
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
. Met à jour un élément d'un tableau à l'aide de DynamoDbClient.
import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.dynamodb.model.DynamoDbException; import software.amazon.awssdk.services.dynamodb.model.AttributeAction; import software.amazon.awssdk.services.dynamodb.model.AttributeValue; import software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate; import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest; import software.amazon.awssdk.services.dynamodb.DynamoDbClient; import java.util.HashMap; /** * 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 * * To update an Amazon DynamoDB table using the AWS SDK for Java V2, its better * practice to use the * Enhanced Client, See the EnhancedModifyItem example. */ public class UpdateItem { public static void main(String[] args) { final String usage = """ Usage: <tableName> <key> <keyVal> <name> <updateVal> Where: tableName - The Amazon DynamoDB table (for example, Music3). key - The name of the key in the table (for example, Artist). keyVal - The value of the key (for example, Famous Band). name - The name of the column where the value is updated (for example, Awards). updateVal - The value used to update an item (for example, 14). Example: UpdateItem Music3 Artist Famous Band Awards 14 """; if (args.length != 5) { System.out.println(usage); System.exit(1); } String tableName = args[0]; String key = args[1]; String keyVal = args[2]; String name = args[3]; String updateVal = args[4]; Region region = Region.US_EAST_1; DynamoDbClient ddb = DynamoDbClient.builder() .region(region) .build(); updateTableItem(ddb, tableName, key, keyVal, name, updateVal); ddb.close(); } public static void updateTableItem(DynamoDbClient ddb, String tableName, String key, String keyVal, String name, String updateVal) { HashMap<String, AttributeValue> itemKey = new HashMap<>(); itemKey.put(key, AttributeValue.builder() .s(keyVal) .build()); HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<>(); updatedValues.put(name, AttributeValueUpdate.builder() .value(AttributeValue.builder().s(updateVal).build()) .action(AttributeAction.PUT) .build()); UpdateItemRequest request = UpdateItemRequest.builder() .tableName(tableName) .key(itemKey) .attributeUpdates(updatedValues) .build(); try { ddb.updateItem(request); } catch (DynamoDbException e) { System.err.println(e.getMessage()); System.exit(1); } System.out.println("The Amazon DynamoDB table was updated!"); } }
-
Pour API plus de détails, voir UpdateItemla 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 UpdateCommand.
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DynamoDBDocumentClient, UpdateCommand } from "@aws-sdk/lib-dynamodb"; const client = new DynamoDBClient({}); const docClient = DynamoDBDocumentClient.from(client); export const main = async () => { const command = new UpdateCommand({ TableName: "Dogs", Key: { Breed: "Labrador", }, UpdateExpression: "set Color = :color", ExpressionAttributeValues: { ":color": "black", }, ReturnValues: "ALL_NEW", }); const response = await docClient.send(command); console.log(response); return response; };
-
Pour API plus de détails, voir UpdateItemla section AWS SDK for JavaScript APIRéférence.
-
- Kotlin
-
- SDKpour Kotlin
-
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
. suspend fun updateTableItem( tableNameVal: String, keyName: String, keyVal: String, name: String, updateVal: String, ) { val itemKey = mutableMapOf<String, AttributeValue>() itemKey[keyName] = AttributeValue.S(keyVal) val updatedValues = mutableMapOf<String, AttributeValueUpdate>() updatedValues[name] = AttributeValueUpdate { value = AttributeValue.S(updateVal) action = AttributeAction.Put } val request = UpdateItemRequest { tableName = tableNameVal key = itemKey attributeUpdates = updatedValues } DynamoDbClient { region = "us-east-1" }.use { ddb -> ddb.updateItem(request) println("Item in $tableNameVal was updated") } }
-
Pour API plus de détails, voir UpdateItem
la APIréférence AWS SDK à Kotlin.
-
- 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
. echo "What rating would you like to give {$movie['Item']['title']['S']}?\n"; $rating = 0; while (!is_numeric($rating) || intval($rating) != $rating || $rating < 1 || $rating > 10) { $rating = testable_readline("Rating (1-10): "); } $service->updateItemAttributeByKey($tableName, $key, 'rating', 'N', $rating); public function updateItemAttributeByKey( string $tableName, array $key, string $attributeName, string $attributeType, string $newValue ) { $this->dynamoDbClient->updateItem([ 'Key' => $key['Item'], 'TableName' => $tableName, 'UpdateExpression' => "set #NV=:NV", 'ExpressionAttributeNames' => [ '#NV' => $attributeName, ], 'ExpressionAttributeValues' => [ ':NV' => [ $attributeType => $newValue ] ], ]); }
-
Pour API plus de détails, voir UpdateItemla section AWS SDK for PHP APIRéférence.
-
- PowerShell
-
- Outils pour PowerShell
-
Exemple 1 : définit l'attribut genre sur « Rap » sur l'élément DynamoDB avec la clé de partition et la SongTitle clé de tri Artist.
$key = @{ SongTitle = 'Somewhere Down The Road' Artist = 'No One You Know' } | ConvertTo-DDBItem $updateDdbItem = @{ TableName = 'Music' Key = $key UpdateExpression = 'set Genre = :val1' ExpressionAttributeValue = (@{ ':val1' = ([Amazon.DynamoDBv2.Model.AttributeValue]'Rap') }) } Update-DDBItem @updateDdbItem
Sortie :
Name Value ---- ----- Genre Rap
-
Pour API plus de détails, consultez la section UpdateItemRé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
. Mettez à jour un élément à l'aide d'une expression de mise à jour.
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 update_movie(self, title, year, rating, plot): """ Updates rating and plot data for a movie in the table. :param title: The title of the movie to update. :param year: The release year of the movie to update. :param rating: The updated rating to the give the movie. :param plot: The updated plot summary to give the movie. :return: The fields that were updated, with their new values. """ try: response = self.table.update_item( Key={"year": year, "title": title}, UpdateExpression="set info.rating=:r, info.plot=:p", ExpressionAttributeValues={":r": Decimal(str(rating)), ":p": plot}, ReturnValues="UPDATED_NEW", ) except ClientError as err: logger.error( "Couldn't update movie %s in table %s. Here's why: %s: %s", title, self.table.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["Attributes"]
Mettez à jour un élément à l'aide d'une expression de mise à jour qui inclut une opération arithmétique.
class UpdateQueryWrapper: def __init__(self, table): self.table = table def update_rating(self, title, year, rating_change): """ Updates the quality rating of a movie in the table by using an arithmetic operation in the update expression. By specifying an arithmetic operation, you can adjust a value in a single request, rather than first getting its value and then setting its new value. :param title: The title of the movie to update. :param year: The release year of the movie to update. :param rating_change: The amount to add to the current rating for the movie. :return: The updated rating. """ try: response = self.table.update_item( Key={"year": year, "title": title}, UpdateExpression="set info.rating = info.rating + :val", ExpressionAttributeValues={":val": Decimal(str(rating_change))}, ReturnValues="UPDATED_NEW", ) except ClientError as err: logger.error( "Couldn't update movie %s in table %s. Here's why: %s: %s", title, self.table.name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["Attributes"]
Mettre à jour un élément uniquement lorsqu'il remplit certaines conditions.
class UpdateQueryWrapper: def __init__(self, table): self.table = table def remove_actors(self, title, year, actor_threshold): """ Removes an actor from a movie, but only when the number of actors is greater than a specified threshold. If the movie does not list more than the threshold, no actors are removed. :param title: The title of the movie to update. :param year: The release year of the movie to update. :param actor_threshold: The threshold of actors to check. :return: The movie data after the update. """ try: response = self.table.update_item( Key={"year": year, "title": title}, UpdateExpression="remove info.actors[0]", ConditionExpression="size(info.actors) > :num", ExpressionAttributeValues={":num": actor_threshold}, ReturnValues="ALL_NEW", ) except ClientError as err: if err.response["Error"]["Code"] == "ConditionalCheckFailedException": logger.warning( "Didn't update %s because it has fewer than %s actors.", title, actor_threshold + 1, ) else: logger.error( "Couldn't update movie %s. Here's why: %s: %s", title, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response["Attributes"]
-
Pour API plus de détails, reportez-vous UpdateItemà la section AWS SDKrelative à 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 # Updates rating and plot data for a movie in the table. # # @param movie [Hash] The title, year, plot, rating of the movie. def update_item(movie) response = @table.update_item( key: { 'year' => movie[:year], 'title' => movie[:title] }, update_expression: 'set info.rating=:r', expression_attribute_values: { ':r' => movie[:rating] }, return_values: 'UPDATED_NEW' ) rescue Aws::DynamoDB::Errors::ServiceError => e puts("Couldn't update movie #{movie[:title]} (#{movie[:year]}) in table #{@table.name}\n") puts("\t#{e.code}: #{e.message}") raise else response.attributes end
-
Pour API plus de détails, voir UpdateItemla section AWS SDK for Ruby APIRéférence.
-
- SAP ABAP
-
- SDKpour SAP ABAP
-
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
. TRY. oo_output = lo_dyn->updateitem( iv_tablename = iv_table_name it_key = it_item_key it_attributeupdates = it_attribute_updates ). MESSAGE '1 item updated in DynamoDB Table' && iv_table_name TYPE 'I'. CATCH /aws1/cx_dyncondalcheckfaile00. MESSAGE 'A condition specified in the operation could not be evaluated.' TYPE 'E'. CATCH /aws1/cx_dynresourcenotfoundex. MESSAGE 'The table or index does not exist' TYPE 'E'. CATCH /aws1/cx_dyntransactconflictex. MESSAGE 'Another transaction is using the item' TYPE 'E'. ENDTRY.
-
Pour API plus de détails, reportez-vous UpdateItemà la section AWS SDKpour SAP ABAP API ré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 /// Update the specified movie with new `rating` and `plot` information. /// /// - Parameters: /// - title: The title of the movie to update. /// - year: The release year of the movie to update. /// - rating: The new rating for the movie. /// - plot: The new plot summary string for the movie. /// /// - Returns: An array of mappings of attribute names to their new /// listing each item actually changed. Items that didn't need to change /// aren't included in this list. `nil` if no changes were made. /// func update(title: String, year: Int, rating: Double? = nil, plot: String? = nil) async throws -> [Swift.String: DynamoDBClientTypes.AttributeValue]? { do { guard let client = self.ddbClient else { throw MoviesError.UninitializedClient } // Build the update expression and the list of expression attribute // values. Include only the information that's changed. var expressionParts: [String] = [] var attrValues: [Swift.String: DynamoDBClientTypes.AttributeValue] = [:] if rating != nil { expressionParts.append("info.rating=:r") attrValues[":r"] = .n(String(rating!)) } if plot != nil { expressionParts.append("info.plot=:p") attrValues[":p"] = .s(plot!) } let expression = "set \(expressionParts.joined(separator: ", "))" let input = UpdateItemInput( // Create substitution tokens for the attribute values, to ensure // no conflicts in expression syntax. expressionAttributeValues: attrValues, // The key identifying the movie to update consists of the release // year and title. key: [ "year": .n(String(year)), "title": .s(title) ], returnValues: .updatedNew, tableName: self.tableName, updateExpression: expression ) let output = try await client.updateItem(input: input) guard let attributes: [Swift.String: DynamoDBClientTypes.AttributeValue] = output.attributes else { throw MoviesError.InvalidAttributes } return attributes } catch { print("ERROR: update:", dump(error)) throw error } }
-
Pour API plus de détails, reportez-vous UpdateItem
à AWS SDKla section dédiée à Swift API.
-