0

Can any one let me know whether UpdateTableSpec can only update KeySchema attributes and is there any way to update/modify a table with non-keyschema attributes ? My scenario is : I created a table with composite key comprising of a primary ( #id attribute) and range key ( #Name attribute). Now I want to add a third attribute, Gender, which is not a part of keyschema. Is it possible ?

I am updating my DynamoDB table using the following code but it does not add the Gender attribute, although it successfully updates the provisioned attribute:

static void updateTable() {
        System.out.println("Updating the table with new attributes ...");
            Table table = dynamoDB.getTable(tableName);
        UpdateTableSpec updateTableSpec = new UpdateTableSpec();
        List<AttributeDefinition> attributeDefinitionList = updateTableSpec.getAttributeDefinitions();
        if (null == attributeDefinitionList) {
            attributeDefinitionList = new ArrayList<AttributeDefinition>();
        }
        attributeDefinitionList.add(new AttributeDefinition()
                        .withAttributeName("Gender")
                        .withAttributeType("S"));
        updateTableSpec.withAttributeDefinitions(attributeDefinitionList)
                .withProvisionedThroughput(new ProvisionedThroughput()
                .withReadCapacityUnits(6L)
                .withWriteCapacityUnits(7L));;
        table.updateTable(updateTableSpec);
        try {
            table.waitForActive();
            System.out.println("Table updated succesfully");
        } catch (final Exception ex) {
            System.out.println("Exception occurred while updating the table");
        }

    }
Alexander Patrikalakis
  • 5,054
  • 1
  • 30
  • 48
user2582651
  • 33
  • 1
  • 8

1 Answers1

2

You cannot update key schema for a DynamoDB table. Update Table API can be used only for (from the doc):

  1. Modify the provisioned throughput settings of the table.
  2. Enable or disable Streams on the table.
  3. Remove a global secondary index from the table.
  4. Create a new global secondary index on the table. Once the index begins backfilling, you can use UpdateTable to perform other operations.

Your best option, I think, is to migrate to a new table with the desired new key schema. You can see other options here.

Community
  • 1
  • 1
Dasharath
  • 549
  • 4
  • 15