Erstellen Sie eine Tabelle mit mehreren GSIs.
# Create a table with multiple GSIs
aws dynamodb create-table \
--table-name MusicLibrary \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
AttributeName=AlbumTitle,AttributeType=S \
AttributeName=Genre,AttributeType=S \
AttributeName=Year,AttributeType=N \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=SongTitle,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--global-secondary-indexes \
"[
{
\"IndexName\": \"AlbumIndex\",
\"KeySchema\": [{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}],
\"Projection\": {\"ProjectionType\":\"ALL\"}
},
{
\"IndexName\": \"GenreYearIndex\",
\"KeySchema\": [
{\"AttributeName\":\"Genre\",\"KeyType\":\"HASH\"},
{\"AttributeName\":\"Year\",\"KeyType\":\"RANGE\"}
],
\"Projection\": {\"ProjectionType\":\"INCLUDE\",\"NonKeyAttributes\":[\"Artist\",\"SongTitle\"]}
}
]"
Erstellen Sie eine Tabelle mit On-Demand-Kapazität und GSI.
# Create a table with on-demand capacity and GSI
aws dynamodb create-table \
--table-name MusicOnDemand \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
AttributeName=Genre,AttributeType=S \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=SongTitle,KeyType=RANGE \
--billing-mode PAY_PER_REQUEST \
--global-secondary-indexes \
"[
{
\"IndexName\": \"GenreIndex\",
\"KeySchema\": [{\"AttributeName\":\"Genre\",\"KeyType\":\"HASH\"}],
\"Projection\": {\"ProjectionType\":\"ALL\"}
}
]"
Fügen Sie Elemente in eine Tabelle mit mehreren GSIs ein.
# Add items to MusicLibrary table
aws dynamodb put-item \
--table-name MusicLibrary \
--item '{
"Artist": {"S": "The Beatles"},
"SongTitle": {"S": "Hey Jude"},
"AlbumTitle": {"S": "Past Masters"},
"Genre": {"S": "Rock"},
"Year": {"N": "1968"}
}'
aws dynamodb put-item \
--table-name MusicLibrary \
--item '{
"Artist": {"S": "Miles Davis"},
"SongTitle": {"S": "So What"},
"AlbumTitle": {"S": "Kind of Blue"},
"Genre": {"S": "Jazz"},
"Year": {"N": "1959"}
}'
Fragen Sie Elemente aus einer Tabelle mit mehreren ab GSIs.
# Query the AlbumIndex GSI
echo "Querying AlbumIndex GSI:"
aws dynamodb query \
--table-name MusicLibrary \
--index-name AlbumIndex \
--key-condition-expression "AlbumTitle = :album" \
--expression-attribute-values '{":album":{"S":"Kind of Blue"}}'
# Query the GenreYearIndex GSI with a range condition
echo "Querying GenreYearIndex GSI with range condition:"
aws dynamodb query \
--table-name MusicLibrary \
--index-name GenreYearIndex \
--key-condition-expression "Genre = :genre AND #yr > :year" \
--expression-attribute-names '{"#yr": "Year"}' \
--expression-attribute-values '{":genre":{"S":"Rock"},":year":{"N":"1965"}}'