本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
步驟 6:建立全域次要索引
在此步驟中,您會針對已在Music
建立的 步驟 1:建立資料表 資料表建立全域次要索引。
如需全域次要索引的詳細資訊,請參閱 在 DynamoDB 中使用全域次要索引。
使用 Amazon DynamoDB 主控台,為 Music
資料表建立全域次要索引 AlbumTitle-index
:
請在 https://console.aws.amazon.com/dynamodb/
開啟 DynamoDB 主控台。 -
在左側導覽窗格中,選擇 Tables (資料表)。
-
從資料表清單中,選擇 Music (音樂) 資料表。
-
為 Music (音樂) 資料表選擇 Indexes (索引) 標籤。
-
選擇 Create index (建立索引)。
-
在 [建立全域次要索引] 頁面上,執行下列動作:
-
針對 Partition key (分割區索引鍵),輸入
AlbumTitle
。 -
(選擇性) 對於索引名稱,請輸入
AlbumTitle-index
。 -
保留頁面上其他設定的預設選項,然後選擇 [建立索引]。
注意
建立索引需要一些等待時間,直到索引重新整理才會顯示為
ACTIVE
。
-
下列 AWS CLI 範例會使用建立Music
資料表AlbumTitle-index
的全域次要索引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\"}}}]"
使用 update-table
傳回以下範例結果。
{ "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 } }
請注意,IndexStatus
欄位的值會設定為 CREATING
。
若要確認 DynamoDB 已完成建立 AlbumTitle-index
全域次要索引,請使用 describe-table
命令。
Linux
aws dynamodb describe-table --table-name Music | grep IndexStatus
Windows CMD
aws dynamodb describe-table --table-name Music | findstr IndexStatus
此命令會傳回下列結果。當傳回的 IndexStatus
欄位值設定為 ACTIVE
時,表示索引已備妥可供使用。
"IndexStatus": "ACTIVE",
接著,您可以查詢全域次要索引。如需詳細資訊,請參閱 步驟 7:查詢全域次要索引。
重要
完成步驟 6 後,請等待一些等待時間。下一個步驟需要完成步驟 6 和 GSI 表中的所有動作。ACTIVE