【问题标题】:How to add an index from command line to DynamoDB after table was created创建表后如何从命令行向 DynamoDB 添加索引
【发布时间】:2015-11-27 08:30:57
【问题描述】:

如果我找不到任何相关信息,请您指出一个适当的文档主题或提供一个如何向 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 接口的使用。

提前致谢, 耶夫赫尼

【问题讨论】:

    标签: 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
          

          底部的注释认为您正在本地数据库中创建此索引。如果没有,请删除它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-03-22
            • 2020-11-12
            • 2011-10-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多