Creating multiple GSIs by updateTable - DynamoDB

2019-06-05 22:05发布

I'm uisng updateTable of DynmaoDB and based on the documentation, if we want to create multiple Global Secondary Indexes (GSIs) we need to have multiple objects in "GlobalSecondaryIndexUpdates" field, so I'm passing the following params, but it does not update the GSIs; however if I'm just creating one GSI (passing one object in "GlobalSecondaryIndexUpdates" field, it works); here is the params I'm passing for creating multiple GSIs:

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

Am I passing the params in a wrong format?

2条回答
Explosion°爆炸
2楼-- · 2019-06-05 22:25

From the 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.

查看更多
时光不老,我们不散
3楼-- · 2019-06-05 22:41

So the SDK now provides a Table.createGSI helper function for exactly this purpose. See a blog post on the topic.

This helper returns an Index object and you can call index.waitForActive(); to wait until the index has been built before building the next GSI which avoids this Exception.

查看更多
登录 后发表回答