Complete the code to create a DynamoDB table with a partition key.
aws dynamodb create-table --table-name UserData --attribute-definitions AttributeName=UserId,AttributeType=[1] AttributeName=ShardId,AttributeType=S --key-schema AttributeName=UserId,KeyType=HASH AttributeName=ShardId,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
The partition key type for user IDs is usually a string, so 'S' is correct.
Complete the code to add a shard ID attribute to distribute writes.
aws dynamodb put-item --table-name UserData --item '{"UserId": {"S": "user123"}, "ShardId": {"[1]": "shard1"}}'
The shard ID is a string, so the attribute type should be 'S'.
Fix the error in the update expression to increment a shard counter.
aws dynamodb update-item --table-name UserData --key '{"UserId": {"S": "user123"}, "ShardId": {"S": "shard1"}}' --update-expression "SET WriteCount = WriteCount [1] :inc" --expression-attribute-values '{":inc": {"N": "1"}}'
To increment the WriteCount, use the '+' operator in the update expression.
Fill both blanks to create a batch write request for multiple shards.
aws dynamodb batch-write-item --request-items '{"UserData": [[1], [2]]}'
Batch write needs multiple PutRequest items for different shards to distribute writes.
Fill all three blanks to create a conditional write that only updates if the shard count is below a limit.
aws dynamodb update-item --table-name UserData --key '{"UserId": {"S": "user123"}, "ShardId": {"S": "shard1"}}' --update-expression "SET WriteCount = WriteCount + :inc" --condition-expression "WriteCount [1] :max" --expression-attribute-values '{":inc": {"N": "1"}, ":max": {"N": "[2]"}}' --return-values [3]
The condition ensures WriteCount is less than 5 before incrementing. 'ALL_NEW' returns the updated item.