For the UpdateItem operation, represents the attributes to be modified, the action to perform on each, and the new value for each.

You cannot use UpdateItem to update any primary key attributes. Instead, you will need to delete the item, and then use PutItem to create a new item with new attributes.

Attribute values cannot be null; string and binary type attributes must have lengths greater than zero; and set type attributes must not be empty. Requests with empty values will be rejected with a ValidationException exception.

Пример #1
0
        internal AttributeValueUpdate ConvertToAttributeUpdateValue()
        {
            AttributeValue attributeValue = ConvertToAttributeValue();

            AttributeValueUpdate attributeUpdate = new AttributeValueUpdate();
            if (attributeValue == null)
            {
                attributeUpdate.Action = "DELETE";
            }
            else
            {
                attributeUpdate.Action = "PUT";
                attributeUpdate.Value = attributeValue;
            }

            return attributeUpdate;
        }
Пример #2
0
        public void CRUDSamples()
        {
            EnsureTables();

            PutSample();

            {
                #region GetItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Create GetItem request
                GetItemRequest request = new GetItemRequest
                {
                    TableName = "SampleTable",
                    Key = key,
                };

                // Issue request
                var result = client.GetItem(request);

                // View response
                Console.WriteLine("Item:");
                Dictionary<string, AttributeValue> item = result.Item;
                foreach (var keyValuePair in item)
                {
                    Console.WriteLine("{0} : S={1}, N={2}, SS=[{3}], NS=[{4}]",
                        keyValuePair.Key,
                        keyValuePair.Value.S,
                        keyValuePair.Value.N,
                        string.Join(", ", keyValuePair.Value.SS ?? new List<string>()),
                        string.Join(", ", keyValuePair.Value.NS ?? new List<string>()));
                }

                #endregion
            }

            {
                #region UpdateItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Define attribute updates
                Dictionary<string, AttributeValueUpdate> updates = new Dictionary<string, AttributeValueUpdate>();
                // Update item's Setting attribute
                updates["Setting"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.PUT,
                    Value = new AttributeValue { S = "St. Petersburg, Missouri" }
                };
                // Remove item's Bibliography attribute
                updates["Bibliography"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.DELETE
                };   
                // Add a new string to the item's Genres SS attribute
                updates["Genres"] = new AttributeValueUpdate()
                {
                    Action = AttributeAction.ADD,
                    Value = new AttributeValue { SS = new List<string> { "Bildungsroman" } }
                };

                // Create UpdateItem request
                UpdateItemRequest request = new UpdateItemRequest
                {
                    TableName = "SampleTable",
                    Key = key,
                    AttributeUpdates = updates
                };

                // Issue request
                client.UpdateItem(request);

                #endregion
            }

            {
                #region DeleteItem Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define item key
                //  Hash-key of the target item is string value "Mark Twain"
                //  Range-key of the target item is string value "The Adventures of Tom Sawyer"
                Dictionary<string, AttributeValue> key = new Dictionary<string, AttributeValue>
                {
                    { "Author", new AttributeValue { S = "Mark Twain" } },
                    { "Title", new AttributeValue { S = "The Adventures of Tom Sawyer" } }
                };

                // Create DeleteItem request
                DeleteItemRequest request = new DeleteItemRequest
                {
                    TableName = "SampleTable",
                    Key = key
                };

                // Issue request
                client.DeleteItem(request);

                #endregion
            }
        }