创建 table 后如何从命令行向 DynamoDB 添加索引

How to add an index from command line to DynamoDB after table was created

如果我找不到任何相关信息,您能否为我指出适当的文档主题或提供如何向 DynamoDB 添加索引的示例。

根据此博客:http://aws.amazon.com/blogs/aws/amazon-dynamodb-update-online-indexing-reserved-capacity-improvements/?sc_ichannel=em&sc_icountry=global&sc_icampaigntype=launch&sc_icampaign=em_130867660&sc_idetail=em_1273527421&ref_=pe_411040_130867660_15 似乎可以使用 UI 来完成,但是没有提及 CLI 界面用法。

提前致谢, 叶夫黑尼

aws命令对每个级别的子命令都有帮助。例如,您可以 运行 aws help 获取所有服务名称的列表并发现名称 dynamodb。然后你可以 aws dynamodb help 找到 DDB 命令列表,发现 update-table 可能是罪魁祸首。最后,aws dynamodb update-table help 向您展示了添加全局二级索引所需的标志。

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}   }"

这里有一个 shell 函数来设置 R/W 上限,并可选地处理 --global-secondary-index-updates(如果提供了索引名称)

dynamodb_set_caps() {
 #  [ "" ] || fail_exit "Missing table name"
 #  [ "" ] || fail_exit "Missing read capacity"
 #  [ "" ] || fail_exit "Missing write capacity"
  if [ "" ] ; then 
    aws dynamodb update-table  --region $region --table-name  \
      --provisioned-throughput ReadCapacityUnits=,WriteCapacityUnits= \
      --global-secondary-index-updates \
      "Update={"IndexName"="", "ProvisionedThroughput"= {"ReadCapacityUnits"=, "WriteCapacityUnits"=} }"
  else
    aws dynamodb update-table  --region $region --table-name  \
      --provisioned-throughput ReadCapacityUnits=,WriteCapacityUnits=
  fi
}

完全同意 aws 文档在这方面的不足

这里是创建全局二级索引的参考:

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

最后一行的注释认为您正在本地数据库中创建此索引。如果没有,就删除它。