

# 借助 AWS CLI 在 DynamoDB 中使用全局二级索引
<a name="GCICli"></a>

您可以使用 AWS CLI 创建具有一个或多个全局二级索引的 Amazon DynamoDB 表、描述表中的索引，以及使用索引执行查询。

**Topics**
+ [创建一个具有全局二级索引的表。](#GCICli.CreateTableWithIndex)
+ [向现有表添加全局二级索引](#GCICli.CreateIndexAfterTable)
+ [描述一个具有全局二级索引的表](#GCICli.DescribeTableWithIndex)
+ [查询全局二级索引](#GCICli.QueryAnIndex)

## 创建一个具有全局二级索引的表。
<a name="GCICli.CreateTableWithIndex"></a>

全局二级索引可以在您创建表的同时创建。为此，请使用 `create-table` 参数并为一个或多个全局二级索引提供您的规范。下面的示例创建了一个 `GameScores` 表，全局二级索引 `GameTitleIndex`。基表的分区键为 `UserId`，排序键 `GameTitle`，可以有效地找到特定游戏的单个用户的最佳分数，而 GSI 则具有分区键 `GameTitle` 和排序键 `TopScore`，允许您快速找到特定游戏的总体最高分。

```
aws dynamodb create-table \
    --table-name GameScores \
    --attribute-definitions AttributeName=UserId,AttributeType=S \
                            AttributeName=GameTitle,AttributeType=S \
                            AttributeName=TopScore,AttributeType=N  \
    --key-schema AttributeName=UserId,KeyType=HASH \
                 AttributeName=GameTitle,KeyType=RANGE \
    --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 \
    --global-secondary-indexes \
        "[
            {
                \"IndexName\": \"GameTitleIndex\",
                \"KeySchema\": [{\"AttributeName\":\"GameTitle\",\"KeyType\":\"HASH\"},
                                {\"AttributeName\":\"TopScore\",\"KeyType\":\"RANGE\"}],
                \"Projection\":{
                    \"ProjectionType\":\"INCLUDE\",
                    \"NonKeyAttributes\":[\"UserId\"]
                },
                \"ProvisionedThroughput\": {
                    \"ReadCapacityUnits\": 10,
                    \"WriteCapacityUnits\": 5
                }
            }
        ]"
```

您必须等待 DynamoDB 创建该表并将表的状态设置为 `ACTIVE`。然后，您就可以开始在表中添加数据项目。您可以使用 [describe-table](https://docs.aws.amazon.com/cli/latest/reference/dynamodb/describe-table.html) 确定表创建的状态。

## 向现有表添加全局二级索引
<a name="GCICli.CreateIndexAfterTable"></a>

创建表后也可以添加或修改全局二级索引。为此，请使用 `update-table` 参数并为一个或多个全局二级索引提供您的规范。以下示例使用与前面示例相同的架构，但假定表已经创建，我们稍后将添加 GSI。

```
aws dynamodb update-table \
    --table-name GameScores \
    --attribute-definitions AttributeName=TopScore,AttributeType=N  \
    --global-secondary-index-updates \
        "[
            {
                \"Create\": {
                    \"IndexName\": \"GameTitleIndex\",
                    \"KeySchema\": [{\"AttributeName\":\"GameTitle\",\"KeyType\":\"HASH\"},
                                    {\"AttributeName\":\"TopScore\",\"KeyType\":\"RANGE\"}],
                    \"Projection\":{
                        \"ProjectionType\":\"INCLUDE\",
                        \"NonKeyAttributes\":[\"UserId\"]
                    }
                }
            }
        ]"
```

## 描述一个具有全局二级索引的表
<a name="GCICli.DescribeTableWithIndex"></a>

要获取有关表的全局二级索引的信息，请使用 `describe-table` 参数。对于每个索引，您都可以查看其名称、键架构和投影的属性。

```
aws dynamodb describe-table --table-name GameScores
```

## 查询全局二级索引
<a name="GCICli.QueryAnIndex"></a>

您可以对全局二级索引使用 `query` 操作，基本上与对表执行 `query` 操作相同。您需要指定索引名称、索引排序键的查询条件以及要返回的属性。在本示例中，索引为 `GameTitleIndex`，索引排序键为 `GameTitle`。

要返回的只包含投影到索引的属性。您也可以修改此查询，让返回结果中也包含非键属性，但是这样会导致表抓取活动的成本相对较高的。有关表获取的更多信息，请参阅 [属性投影](GSI.md#GSI.Projections)。

```
aws dynamodb query --table-name GameScores\
    --index-name GameTitleIndex \
    --key-condition-expression "GameTitle = :v_game" \
    --expression-attribute-values '{":v_game":{"S":"Alien Adventure"} }'
```