插入和更新数据

插入数据

PutItem API用于插入一条新记录,或者替换原来的记录

例如在Reply表中插入一条新记录:

aws dynamodb put-item \
    --table-name Reply \
    --item '{
        "Id" : {"S": "Amazon DynamoDB#DynamoDB Thread 2"},
        "ReplyDateTime" : {"S": "2021-04-27T17:47:30Z"},
        "Message" : {"S": "DynamoDB Thread 2 Reply 3 text"},
        "PostedBy" : {"S": "User C"}
    }' \
    --return-consumed-capacity TOTAL

返回结果,消耗一个WCU:

{
    "ConsumedCapacity": {
        "TableName": "Reply",
        "CapacityUnits": 1.0
    }
}

更新数据

The DynamoDB UpdateItem API is used to create a new item or to replace existing items completely with a new item. It is invoked using the update-item CLI command . This API requires you to specify the full Primary Key and can selectively modify specific attributes without changing others(you don’t need to pass in the full item).

The update-item API call also allows you to specify a ConditionExpression, meaning the Update request will only execute if the ConditionExpression is satisfied. For more information please see Condition Expressions in the Developer Guide.

Let’s say we want to update the Forum item for DynamoDB to note that there are 5 messages how instead of 4, we only want that change to execute if no other processing thread has updated the item first. This allows us to create idempotent modifications. For more information on idempotent changes please see Working With Items in the Developer Guide.

To do this from the CLI, we would run:

aws dynamodb update-item \
    --table-name Forum \
    --key '{
        "Name" : {"S": "Amazon DynamoDB"}
    }' \
    --update-expression "SET Messages = :newMessages" \
    --condition-expression "Messages = :oldMessages" \
    --expression-attribute-values '{
        ":oldMessages" : {"N": "4"},
        ":newMessages" : {"N": "5"}
    }' \
    --return-consumed-capacity TOTAL

Note that if you run this exact same command again you will see this error:

An error occurred (ConditionalCheckFailedException) when calling the UpdateItem operation: The conditional request failed

Because the Messages attribute had already been incremented to 5 in the previous update-item call, the second request fails with a ConditionalCheckFailedException.

Exercise

Update the ProductCatalog item where Id=201 to add new colors “Blue” and “Yellow” to the list of colors for that bike type. Then use the API to remove those “Blue” and “Yellow” list entries to return it to the original state.

Hint: The Update Expressions page in the Developer Guide has sections on Appending and Removing Elements in a List.

The solution is expandable below but try to figure it out yourself before moving forward.

Click below to expand and see the exercise solutions

Expand this to see the solution

First we need to see what the item looks like:

aws dynamodb get-item \
    --table-name ProductCatalog \
    --key '{"Id":{"N":"201"}}'
{
    "Item": {
        "Title": {
            "S": "18-Bike-201"
        },
        "Price": {
            "N": "100"
        },
        "Brand": {
            "S": "Mountain A"
        },
        "Description": {
            "S": "201 Description"
        },
        "Color": {
            "L": [
                {
                    "S": "Red"
                },
                {
                    "S": "Black"
                }
            ]
        },
        "ProductCategory": {
            "S": "Bicycle"
        },
        "Id": {
            "N": "201"
        },
        "BicycleType": {
            "S": "Road"
        }
    }
}

We can see that there is a List attribute called Color and that it has two colors already, Red and Black. We will use the list_append() function in the UpdateExpression to add the color Blue.

aws dynamodb update-item \
    --table-name ProductCatalog \
    --key '{
        "Id" : {"N": "201"}
    }' \
    --update-expression "SET #Color = list_append(#Color, :values)" \
    --expression-attribute-names '{"#Color": "Color"}' \
    --expression-attribute-values '{
        ":values" : {"L": [{"S" : "Blue"}, {"S" : "Yellow"}]}
    }' \
    --return-consumed-capacity TOTAL

Now when we want to remove those two color entries, we have to reference the index number in the list. DynamoDB lists are 0-based, which means the 3rd and 4th elements we just added are list indexes 2 and 3. To remove them, we can use this command:

aws dynamodb update-item \
    --table-name ProductCatalog \
    --key '{
        "Id" : {"N": "201"}
    }' \
    --update-expression "REMOVE #Color[2], #Color[3]" \
    --expression-attribute-names '{"#Color": "Color"}' \
    --return-consumed-capacity TOTAL

You can use the get-item command to verify that these changes were made after each step.