Step 6: Create a global secondary index
In this step, you create a global secondary index for the Music
table
that you created in Step 1: Create a table.
For more information about global secondary indexes, see Using Global Secondary Indexes in DynamoDB.
To use the Amazon DynamoDB console to create a global secondary index
AlbumTitle-index
for the Music
table:
Open the DynamoDB console at https://console.amazonaws.cn/dynamodb/
. -
In the navigation pane on the left side of the console, choose Tables.
-
Choose the Music table from the table list.
-
Choose the Indexes tab for the Music table.
-
Choose Create index.
-
For the Partition key, enter
AlbumTitle
. -
For Index name, enter
AlbumTitle-index
. -
Leave the other settings on their default values and choose Create index.
The following Amazon CLI example creates a global secondary index
AlbumTitle-index
for the Music
table using
update-table
.
Linux
aws dynamodb update-table \ --table-name Music \ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S \ --global-secondary-index-updates \ "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \ \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5 },\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"
Windows CMD
aws dynamodb update-table ^ --table-name Music ^ --attribute-definitions AttributeName=AlbumTitle,AttributeType=S ^ --global-secondary-index-updates "[{\"Create\":{\"IndexName\": \"AlbumTitle-index\",\"KeySchema\":[{\"AttributeName\":\"AlbumTitle\",\"KeyType\":\"HASH\"}], \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 10, \"WriteCapacityUnits\": 5},\"Projection\":{\"ProjectionType\":\"ALL\"}}}]"
Using update-table
returns the following sample result.
{ "TableDescription": { "TableArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music", "AttributeDefinitions": [ { "AttributeName": "AlbumTitle", "AttributeType": "S" }, { "AttributeName": "Artist", "AttributeType": "S" }, { "AttributeName": "SongTitle", "AttributeType": "S" } ], "GlobalSecondaryIndexes": [ { "IndexSizeBytes": 0, "IndexName": "AlbumTitle-index", "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "IndexStatus": "CREATING", "Backfilling": false, "KeySchema": [ { "KeyType": "HASH", "AttributeName": "AlbumTitle" } ], "IndexArn": "arn:aws:dynamodb:us-west-2:111122223333:table/Music/index/AlbumTitle-index", "ItemCount": 0 } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "TableSizeBytes": 0, "TableName": "Music", "TableStatus": "UPDATING", "TableId": "a04b7240-0a46-435b-a231-b54091ab1017", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1558028402.69 } }
Note that the value of the IndexStatus
field is set to
CREATING
.
To verify that DynamoDB has finished creating the AlbumTitle-index
global secondary index, use the describe-table
command.
Linux
aws dynamodb describe-table --table-name Music | grep IndexStatus
Windows CMD
aws dynamodb describe-table --table-name Music | findstr IndexStatus
This command returns the following result. The index is ready for use when the
value of the IndexStatus
field returned is set to
ACTIVE
.
"IndexStatus": "ACTIVE",
Next, you can query the global secondary index. For details, see Step 7: Query the global secondary index.