-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1166 from awsdocs/dynamodb-examples
dynamo db examples
- Loading branch information
Showing
13 changed files
with
484 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
**To retrieve multiple items from a table** | ||
|
||
This example reads multiple items from the *MusicCollection* table using a batch of three GetItem requests. Only the *AlbumTitle* attribute is returned. | ||
|
||
Command:: | ||
|
||
aws dynamodb batch-get-item --request-items file://request-items.json | ||
|
||
The arguments for ``--request-items`` are stored in a JSON file, ``request-items.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"MusicCollection": { | ||
"Keys": [ | ||
{ | ||
"Artist": {"S": "No One You Know"}, | ||
"SongTitle": {"S": "Call Me Today"} | ||
}, | ||
{ | ||
"Artist": {"S": "Acme Band"}, | ||
"SongTitle": {"S": "Happy Day"} | ||
}, | ||
{ | ||
"Artist": {"S": "No One You Know"}, | ||
"SongTitle": {"S": "Scared of My Shadow"} | ||
} | ||
], | ||
"ProjectionExpression":"AlbumTitle" | ||
} | ||
} | ||
|
||
Output:: | ||
|
||
{ | ||
"UnprocessedKeys": {}, | ||
"Responses": { | ||
"MusicCollection": [ | ||
{ | ||
"AlbumTitle": { | ||
"S": "Somewhat Famous" | ||
} | ||
}, | ||
{ | ||
"AlbumTitle": { | ||
"S": "Blue Sky Blues" | ||
} | ||
}, | ||
{ | ||
"AlbumTitle": { | ||
"S": "Louder Than Ever" | ||
} | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
**To add multiple items to a table** | ||
|
||
This example adds three new items to the *MusicCollection* table using a batch of three PutItem requests. | ||
|
||
Command:: | ||
|
||
aws dynamodb batch-write-item --request-items file://request-items.json | ||
|
||
The arguments for ``--request-items`` are stored in a JSON file, ``request-items.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"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"} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
|
||
Output:: | ||
|
||
{ | ||
"UnprocessedItems": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
**To create a table** | ||
|
||
This example creates a table named *MusicCollection*. | ||
|
||
Command:: | ||
|
||
aws dynamodb create-table --table-name MusicCollection --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 | ||
|
||
Output:: | ||
|
||
{ | ||
"TableDescription": { | ||
"AttributeDefinitions": [ | ||
{ | ||
"AttributeName": "Artist", | ||
"AttributeType": "S" | ||
}, | ||
{ | ||
"AttributeName": "SongTitle", | ||
"AttributeType": "S" | ||
} | ||
], | ||
"ProvisionedThroughput": { | ||
"NumberOfDecreasesToday": 0, | ||
"WriteCapacityUnits": 5, | ||
"ReadCapacityUnits": 5 | ||
}, | ||
"TableSizeBytes": 0, | ||
"TableName": "MusicCollection", | ||
"TableStatus": "CREATING", | ||
"KeySchema": [ | ||
{ | ||
"KeyType": "HASH", | ||
"AttributeName": "Artist" | ||
}, | ||
{ | ||
"KeyType": "RANGE", | ||
"AttributeName": "SongTitle" | ||
} | ||
], | ||
"ItemCount": 0, | ||
"CreationDateTime": 1421866952.062 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
**To delete an item** | ||
|
||
This example deletes an item from the *MusicCollection* table. | ||
|
||
Command:: | ||
|
||
aws dynamodb delete-item --table-name MusicCollection --key file://key.json | ||
|
||
The arguments for ``--key`` are stored in a JSON file, ``key.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"Artist": {"S": "No One You Know"}, | ||
"SongTitle": {"S": "Scared of My Shadow"} | ||
} | ||
|
||
Output:: | ||
|
||
{ | ||
"ConsumedCapacity": { | ||
"CapacityUnits": 1.0, | ||
"TableName": "MusicCollection" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
**To delete a table** | ||
|
||
This example deletes the *MusicCollection* table. | ||
|
||
Command:: | ||
|
||
aws dynamodb delete-table --table-name MusicCollection | ||
|
||
Output:: | ||
|
||
{ | ||
"TableDescription": { | ||
"TableStatus": "DELETING", | ||
"TableSizeBytes": 0, | ||
"ItemCount": 0, | ||
"TableName": "MusicCollection", | ||
"ProvisionedThroughput": { | ||
"NumberOfDecreasesToday": 0, | ||
"WriteCapacityUnits": 5, | ||
"ReadCapacityUnits": 5 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
**To describe a table** | ||
|
||
This example describes the *MusicCollection* table. | ||
|
||
Command:: | ||
|
||
aws dynamodb describe-table --table-name MusicCollection | ||
|
||
Output:: | ||
|
||
{ | ||
"Table": { | ||
"AttributeDefinitions": [ | ||
{ | ||
"AttributeName": "Artist", | ||
"AttributeType": "S" | ||
}, | ||
{ | ||
"AttributeName": "SongTitle", | ||
"AttributeType": "S" | ||
} | ||
], | ||
"ProvisionedThroughput": { | ||
"NumberOfDecreasesToday": 0, | ||
"WriteCapacityUnits": 5, | ||
"ReadCapacityUnits": 5 | ||
}, | ||
"TableSizeBytes": 0, | ||
"TableName": "MusicCollection", | ||
"TableStatus": "ACTIVE", | ||
"KeySchema": [ | ||
{ | ||
"KeyType": "HASH", | ||
"AttributeName": "Artist" | ||
}, | ||
{ | ||
"KeyType": "RANGE", | ||
"AttributeName": "SongTitle" | ||
} | ||
], | ||
"ItemCount": 0, | ||
"CreationDateTime": 1421866952.062 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
**To read an item in a table** | ||
|
||
This example retrieves an item from the *MusicCollection* table. The table has a hash-and-range primary key (*Artist* and *SongTitle*), so you must specify both of these ttributes. | ||
|
||
|
||
Command:: | ||
|
||
aws dynamodb get-item --table-name MusicCollection --key file://key.json | ||
|
||
The arguments for ``--key`` are stored in a JSON file, ``key.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"Artist": {"S": "Acme Band"}, | ||
"SongTitle": {"S": "Happy Day"} | ||
} | ||
|
||
|
||
Output:: | ||
|
||
{ | ||
"Item": { | ||
"AlbumTitle": { | ||
"S": "Songs About Life" | ||
}, | ||
"SongTitle": { | ||
"S": "Happy Day" | ||
}, | ||
"Artist": { | ||
"S": "Acme Band" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
**To list tables** | ||
|
||
This example lists all of the tables associated with the current AWS account and endpoint | ||
|
||
Command:: | ||
|
||
aws dynamodb list-tables | ||
|
||
Output:: | ||
|
||
{ | ||
"TableNames": [ | ||
"Forum", | ||
"ProductCatalog", | ||
"Reply", | ||
"Thread", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
**To add an item to a table** | ||
|
||
This example adds a new item to the *MusicCollection* table. | ||
|
||
Command:: | ||
|
||
aws dynamodb put-item --table-name MusicCollection --item file://item.json --return-consumed-capacity TOTAL | ||
|
||
The arguments for ``--item`` are stored in a JSON file, ``item.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"Artist": {"S": "No One You Know"}, | ||
"SongTitle": {"S": "Call Me Today"}, | ||
"AlbumTitle": {"S": "Somewhat Famous"} | ||
} | ||
|
||
Output:: | ||
|
||
{ | ||
"ConsumedCapacity": { | ||
"CapacityUnits": 1.0, | ||
"TableName": "MusicCollection" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
**To query an item** | ||
|
||
This example queries items in the *MusicCollection* table. The table has a hash-and-range primary key (*Artist* and *SongTitle*), but this query only specifies the hash key value. It returns song titles by the artist named "No One You Know". | ||
|
||
Command:: | ||
|
||
aws dynamodb query --table-name MusicCollection --key-conditions file://key-conditions.json --projection-expression "SongTitle" | ||
|
||
The arguments for ``--key-conditions`` are stored in a JSON file, ``key-conditions.json``. Here are the contents of that file:: | ||
|
||
{ | ||
"Artist": { | ||
"AttributeValueList": [ | ||
{"S": "No One You Know"} | ||
], | ||
"ComparisonOperator": "EQ" | ||
} | ||
} | ||
|
||
Output:: | ||
|
||
{ | ||
"Count": 2, | ||
"Items": [ | ||
{ | ||
"SongTitle": { | ||
"S": "Call Me Today" | ||
} | ||
}, | ||
{ | ||
"SongTitle": { | ||
"S": "Scared of My Shadow" | ||
} | ||
} | ||
], | ||
"ScannedCount": 2, | ||
"ConsumedCapacity": null | ||
} |
Oops, something went wrong.