第 6 步:创建一个全局二级索引 - Amazon DynamoDB
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

第 6 步:创建一个全局二级索引

在这一步中,您将为 Music 中创建的 第 1 步:创建一个表 表创建全局二级索引。

有关全局二级索引的更多信息,请参阅 在 DynamoDB 中使用全局二级索引

要使用 Amazon DynamoDB 控制台为 Music 表创建全局二级索引 AlbumTitle-index,请执行以下操作:

  1. 打开 DynamoDB 控制台:https://console.aws.amazon.com/dynamodb/

  2. 在控制台左侧的导航窗格中,选择

  3. 从表列表中选择 Music 表。

  4. 对 Music 表选择 Indexes (索引) 选项卡。

  5. 选择 Create index (创建索引)

    
                                显示“创建索引”按钮的控制台屏幕截图。
  6. 对于 Partition key (分区键),输入 AlbumTitle

  7. 对于 Index name (索引名称),输入 AlbumTitle-index

  8. 将其他设置保留为默认值,然后选择 Create index (创建索引)

    
                                显示“创建索引”对话框中的已完成字段的控制台屏幕截图。

下面的 Amazon CLI 示例使用 update-tableMusic 表创建全局二级索引 AlbumTitle-index

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 步:查询该全局二级索引