【问题标题】:How to add an index from command line to DynamoDB after table was created创建表后如何从命令行向 DynamoDB 添加索引
【发布时间】:2015-11-27 08:30:57
【问题描述】:
【问题讨论】:
标签:
amazon-web-services
indexing
amazon
command-line-interface
amazon-dynamodb
【解决方案1】:
aws 命令对每个级别的子命令都有帮助。例如,您可以运行aws help 来获取所有服务名称的列表并发现名称dynamodb。然后可以aws dynamodb help查找DDB命令列表,发现update-table很可能是罪魁祸首。最后,aws dynamodb update-table help 向您展示了添加全局二级索引所需的标志。
【解决方案2】:
AWS CLI 文档真的很差,而且缺少示例。显然 AWS 正在推广 SDK 或控制台。
这应该适用于更新
aws dynamodb update-table --table-name Test \
--attribute-definitions AttributeName=City,AttributeType=S AttributeName=State,AttributeType=S \
--global-secondary-index-updates \
"Create={"IndexName"="state-index", "KeySchema"=[ {"AttributeName"="State", "KeyType"="HASH" }], "Projection"={"ProjectionType"="INCLUDE", "NonKeyAttributes"="City"}, "ProvisionedThroughput"= {"ReadCapacityUnits"=1, "WriteCapacityUnits"=1} }"
【解决方案3】:
这是一个设置 R/W 上限的 shell 函数,如果提供了索引名称,则可以选择处理 --global-secondary-index-updates
dynamodb_set_caps() {
# [ "$1" ] || fail_exit "Missing table name"
# [ "$3" ] || fail_exit "Missing read capacity"
# [ "$3" ] || fail_exit "Missing write capacity"
if [ "$4" ] ; then
aws dynamodb update-table --region $region --table-name ${1} \
--provisioned-throughput ReadCapacityUnits=${2},WriteCapacityUnits=${3} \
--global-secondary-index-updates \
"Update={"IndexName"="${4}", "ProvisionedThroughput"= {"ReadCapacityUnits"=${2}, "WriteCapacityUnits"=${3}} }"
else
aws dynamodb update-table --region $region --table-name ${1} \
--provisioned-throughput ReadCapacityUnits=${2},WriteCapacityUnits=${3}
fi
}
完全同意这方面缺少 aws 文档
【解决方案4】:
这里是创建全局二级索引的参考:
https://docs.aws.amazon.com/pt_br/amazondynamodb/latest/developerguide/getting-started-step-6.html
但是,该示例仅提供为单个主键创建索引。
这段代码帮助我为复合主键创建了一个全局二级索引:
aws dynamodb update-table \
--table-name YourTableName \
--attribute-definitions AttributeName=GSI1PK,AttributeType=S \
AttributeName=GSI1SK,AttributeType=S \
AttributeName=createdAt,AttributeType=S \
--global-secondary-index-updates \
"[{\"Create\":{\"IndexName\": \"GSI1\",\"KeySchema\":[{\"AttributeName\":\"GSI1PK\",\"KeyType\":\"HASH\"},{\"AttributeName\":\"GSI1SK\",\"KeyType\":\"RANGE\"}], \
\"ProvisionedThroughput\": {\"ReadCapacityUnits\": 5, \"WriteCapacityUnits\": 5 },\"Projection\":{\"ProjectionType\":\"ALL\"}}}]" --endpoint-url http://localhost:8000
底部的注释认为您正在本地数据库中创建此索引。如果没有,请删除它。