本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
步骤 6:创建全局二级索引
在此步骤中,您将为在Music
中创建的 步骤 1:创建表 表创建全局二级索引。
有关全局二级索引的更多信息,请参阅在 DynamoDB 中使用全局二级索引。
要使用 Amazon DynamoDB 控制台为 Music
表创建全局二级索引 AlbumTitle-index
,请执行以下操作:
-
通过以下网址打开 DynamoDB 控制台:https://console.amazonaws.cn/dynamodb/
。 -
在控制台左侧的导航窗格中,选择 Tables (表)。
-
从表列表中选择 Music 表。
-
对 Music 表选择索引选项卡。
-
选择 Create index (创建索引)。
-
对于分区键,输入
AlbumTitle
,然后选择创建索引。
以下 AWS CLI 示例使用 update-table
为 Music
表创建全局二级索引 AlbumTitle-index
。
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:522194210714: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:522194210714:table/Music/index/AlbumTitle-index", "ItemCount": 0 } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "WriteCapacityUnits": 5, "ReadCapacityUnits": 10 }, "TableSizeBytes": 0, "TableName": "Music", "TableStatus": "UPDATING", "TableId": "d04c7240-0e46-435d-b231-d54091fe1017", "KeySchema": [ { "KeyType": "HASH", "AttributeName": "Artist" }, { "KeyType": "RANGE", "AttributeName": "SongTitle" } ], "ItemCount": 0, "CreationDateTime": 1558028402.69 } }
请注意,IndexStatus
字段的值设置为 CREATING
。
要验证 DynamoDB 已完成创建 AlbumTitle-index
全局二级索引,请使用 describe-table
命令。
aws dynamodb describe-table --table-name Music | grep IndexStatus
此命令将返回以下结果。当返回的 IndexStatus
字段值设置为 ACTIVE
时,索引即准备好使用。
"IndexStatus": "ACTIVE",
接下来,您可以查询全局二级索引。有关详细信息,请参阅 步骤 7:查询全局二级索引。