第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",