通过 updateTable 创建多个 GSI - DynamoDB

Creating multiple GSIs by updateTable - DynamoDB

我正在使用 DynmaoDB updateTable,根据文档,如果我们想创建多个全局二级索引 (GSI),我们需要在 "GlobalSecondaryIndexUpdates" 字段中有多个对象,所以我'传递以下参数,但不更新 GSI;但是,如果我只是创建一个 GSI(在 "GlobalSecondaryIndexUpdates" 字段中传递一个对象,它就可以工作);这是我为创建多个 GSI 而传递的参数:

{
    "TableName": "movies",
    "AttributeDefinitions": [{
            "AttributeName": "id",
            "AttributeType": "N"
        }, {
            "AttributeName": "title",
            "AttributeType": "S"
        }, {
            "AttributeName": "subtitle",
            "AttributeType": "S"
        }],
    "GlobalSecondaryIndexUpdates": [{
            "Create": {
                "IndexName": "title",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": "5",
                    "WriteCapacityUnits": "5"
                },
                "KeySchema": [{
                        "AttributeName": "title",
                        "KeyType": "HASH"
                    }],
                "Projection": {
                    "ProjectionType": "ALL"
                }
            }
        }, {
            "Create": {
                "IndexName": "subtitle",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": "5",
                    "WriteCapacityUnits": "5"
                },
                "KeySchema": [{
                        "AttributeName": "subtitle",
                        "KeyType": "HASH"
                    }],
                "Projection": {
                    "ProjectionType": "ALL"
                }
            }
        }]
}

我传递的参数格式有误吗?

来自 DynamoDB documentation:

You can only create or delete one global secondary index per UpdateTable operation. However, if you run multiple UpdateTable operations simultaneously, you can create multiple indexes at a time. You can run up to five of these UpdateTable operations on a table at once, and each operation can create exactly one index.

因此 SDK 现在提供了关于该主题的 Table.createGSI helper function for exactly this purpose. See a blog post

这个助手 returns 一个 Index 对象,您可以调用 index.waitForActive(); 等到索引建立后再构建下一个 GSI,从而避免此异常。

如果您正在使用 bash,您可以使用适当的 aws 命令创建索引,然后使用以下代码段在更新之间等待

waitForIndexUpdate() {
  tableActive=false
  while [ "$tableActive" = false ];
  do
    describeTable=$(aws dynamodb describe-table --endpoint-url "$dynamoHost" --table-name "$tableName")
    if [[ $describeTable == *"\"IndexStatus\": \"CREATING\""* ]];
    then
      echo "waiting for index update to complete..."
      sleep .5
    else
      tableActive=true
    fi
  done
}