2017-01-15 72 views
0

任何人都可以讓我知道UpdateTableSpec是否只能更新KeySchema屬性,並且有什麼方法可以更新/修改具有非keyschema屬性的表嗎?我的場景是:我創建了一個包含主要(#id屬性)和範圍鍵(#Name屬性)的組合鍵的表。現在我想添加第三個屬性Gender,它不是keyschema的一部分。可能嗎 ?DynamoDB表上的UpdateTableSpec不起作用

我使用下面的代碼來更新我的DynamoDB表,但它不會添加的性別屬性,但它成功地更新配置的屬性:

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

    } 

回答

2

您不能更新爲DynamoDB表密鑰架構。更新表API可以只用於(從doc):

  1. 修改表的配置的吞吐量設置。
  2. 在表上啓用或禁用Streams。
  3. 從表中刪除全局二級索引。
  4. 在表上創建新的全局二級索引。一旦索引開始回填,您可以使用UpdateTable執行其他操作。

我認爲最好的選擇是使用所需的新關鍵模式遷移到新表。您可以看到其他選項here