【问题标题】:Add more colums to the table and input values in dynamoDB JS shell向表中添加更多列并在 dynamoDB JS shell 中输入值
【发布时间】:2018-08-15 22:20:29
【问题描述】:

我在 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。

【问题讨论】:

    标签: amazon-dynamodb dynamo-local


    【解决方案1】:

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

    在下面的代码中,我定义了 GSI,名称为 student_test_name_gsi,属性 stu_name 为哈希键,stu_id 为范围键

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

    【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2021-12-23
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多