aws DynamoDB 使用步骤(二)

第5步:查询表中的数据

使用AWS管理控制台

按照以下步骤,使用DynamoDB控制台查询音乐表中的数据。

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

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

3.从表列表中选择音乐表。

4.选择查看项目。

5.选择查询。

6.对于分区键,输入Acme Band,然后选择运行。
在这里插入图片描述

使用AWS CLI

你通过DynamoDB的API使用query并提供分区密钥来查询一个项目。

aws dynamodb query \
    --table-name Music \
    --key-condition-expression "Artist = :name" \
    --expression-attribute-values  '{":name":{"S":"Acme Band”}}'

使用查询返回所有与这个特定艺术家相关的歌曲。

{
    "Items": [
        {
            "AlbumTitle": {
                "S": "Updated Album Title"
            },
            "Awards": {
                "N": "10"
            },
            "Artist": {
                "S": "Acme Band"
            },
            "SongTitle": {
                "S": "Happy Day"
            }
        },
        {
            "AlbumTitle": {
                "S": "Another Album Title"
            },
            "Awards": {
                "N": "8"
            },
            "Artist": {
                "S": "Acme Band"
            },
            "SongTitle": {
                "S": "PartiQL Rocks"
            }
        }
    ],
    "Count": 2,
    "ScannedCount": 2,
    "ConsumedCapacity": null
}

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

使用AWS管理控制台

要使用Amazon DynamoDB控制台为音乐表创建一个全局二级索引AlbumTitle-index。

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

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

3.从表列表中选择音乐表。

4.选择音乐表的索引标签。

5.选择创建索引。
在这里插入图片描述

6.对于分区键,输入AlbumTitle。

7.对于索引名称,输入AlbumTitle-index。

8.将其他设置保留在其默认值上,并选择创建索引。
在这里插入图片描述

使用AWS CLI

下面的AWS CLI例子使用update-table为音乐表创建了一个全局二级索引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",
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值