0

我在 dynamoDB JS shell 中创建了一个表,其中 stu_id 作为 hash_key_attribute。

var params = {
    TableName: 'student_test',
    KeySchema: [ 
        { // Required HASH type attribute
            AttributeName: 'stu_id',
            KeyType: 'HASH',
        },
    ],
    AttributeDefinitions: [ 
        {
            AttributeName: 'stu_id',
            AttributeType: 'N',             },
    ],
    ProvisionedThroughput: { // required provisioned throughput for the table
        ReadCapacityUnits: 1, 
        WriteCapacityUnits: 1, 
    },
};
dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});

现在我想添加 stu_name 和 school 列并插入值。尝试在 GlobalSecondaryIndexes 中定义列名,但没有成功。

我在本地使用 DynamoDB。

4

1 回答 1

1

这是使用新 GSI更新表的代码。

在下面的代码中,我将具有名称student_test_name_gsi和属性的 GSI 定义stu_name为 Hash 键和stu_idRange 键

var params = {
    TableName: 'student_test',
    AttributeDefinitions: [
        {
            AttributeName: 'stu_id',
            AttributeType: 'N',             },
        {
            AttributeName: 'stu_name',
            AttributeType: 'S',             },
    ],
        GlobalSecondaryIndexUpdates: [
            {
              Create: {
                IndexName: 'student_test_name_gsi', /* required */
                KeySchema: [ /* required */
                  {
                    AttributeName: 'stu_name', /* required */
                    KeyType: 'HASH' /* required */
                  },
                  { // Optional RANGE key type for HASH + RANGE secondary indexes
                      AttributeName: 'stu_id',
                      KeyType: 'RANGE',
                  }
                  /* more items */
                ],
                Projection: { /* required */
                  ProjectionType: 'ALL'
                },
                ProvisionedThroughput: { /* required */
                  ReadCapacityUnits: 20, /* required */
                  WriteCapacityUnits: 20 /* required */
                }
              },
            },
            /* more items */
          ],
};

dynamodb.updateTable(params, function(err, data) {
    if (err) {
            console.error("Unable to update table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Updated table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});
于 2018-08-15T14:20:29.817 回答