无法更新 Dynamodb table,获取 ValidationException

Cant update Dynamo Db table , getting ValidationException

我需要仅使用分区键来更新我的发电机数据库 table。但是我得到了验证例外。 我创建了一个包含 3 个字段的 table。

  1. id(分区键)
  2. 名称(排序键)
  3. 年龄

然后我尝试仅使用 id 更新年龄字段。(尝试将年龄从 30 修改为 40)这是我的代码

var AWS = require("aws-sdk");

AWS.config.update({
    region: "us-east-1",

});

var params = {
    TableName: 'test',
    Key: { id: '100' },
    UpdateExpression: 'set #age = :age ',
    ConditionExpression: '#age = :testAge',
    ExpressionAttributeNames: { '#age': 'age' },
    ExpressionAttributeValues: { ':age': '40', ':testAge': '30' }
};

var docClient = new AWS.DynamoDB.DocumentClient();
docClient.update(params, function (err, data) {
    if (err) {
        console.log(err);
    }
    else {
        console.log(data);
    }
});

但是我遇到了这样的错误。

{ [ValidationException: The provided key element does not match the schema]
  message: 'The provided key element does not match the schema',
  code: 'ValidationException',
  time: Thu Nov 17 2016 22:38:01 GMT+0530 (IST),
  requestId: '34PNMFM6CEACQIRHTSV77OI0JRVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 0 }

出现错误后,我像这样修改了 params 变量

 var params = {
        TableName: 'test',
        Key: { id: '100',name: 'manaf' },
        UpdateExpression: 'set #age = :age ',
        ConditionExpression: '#age = :testAge',
        ExpressionAttributeNames: { '#age': 'age' },
        ExpressionAttributeValues: { ':age': '40', ':testAge': '30' }
    };

至此,更新成功。如何在不使用排序键的情况下更新 table?

目前,DynamoDB 更新 API 没有仅通过分区键更新项目的选项。也没有类似于 batchWriteItem 的 batchUpdateItem API。

因此,如果排序键不可用,则获取分区键的所有排序键并更新分区和排序键组合的每个项目。

For the primary key, you must provide all of the attributes. For example, with a simple primary key, you only need to provide a value for the partition key. For a composite primary key, you must provide values for both the partition key and the sort key.

示例代码:-

您可能需要为您的 table 更改它。下面的代码使用 "Movies" table,其中 "yearkey" 作为分区键,"title" 作为排序键。

下面的代码更新给定哈希键“2012”的 "createdate" 属性。

变量paramsUpdate是根据查询操作形成的。请根据您的要求相应地更新它(即 table 结构)。逻辑保持不变,您只需要相应地更改 table 名称和键值。

var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');

AWS.config.update({
    region : "us-west-2",
    endpoint : "http://localhost:8000",
    credentials : creds
});

var docClient = new AWS.DynamoDB.DocumentClient();

var hashKey = 2012;

var paramsQuery = {
    TableName : "Movies",
    KeyConditionExpression : 'yearkey = :hkey',
    ExpressionAttributeValues : {
        ':hkey' : hashKey

    }
};

function updateItem(paramsUpdate) {
    console.log("Updating the item...");
    docClient.update(paramsUpdate, function(err, data) {
        if (err) {
            console.error("Unable to update item. Error JSON:", JSON.stringify(
                    err, null, 2));
        } else {
            console.log("UpdateItem succeeded:", JSON.stringify(data));
        }
    });
}

docClient.query(paramsQuery, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log(data.Count);
        var itemIndex = 0;
        while (itemIndex < data.Count) {

            console.log('Hashkey to be updated ======>',
                     data.Items[itemIndex].yearkey,
                     ';Title to be updated ========>',
                     data.Items[itemIndex].title);
            var paramsUpdate = {
                TableName : "Movies",
                Key : {
                    "yearkey" : data.Items[itemIndex].yearkey,
                    "title" : data.Items[itemIndex].title
                },
                UpdateExpression : "set #createdate = :createdate",
                ExpressionAttributeNames : {
                    '#createdate' : 'createdate'
                },
                ExpressionAttributeValues : {
                    ':createdate' : '2016-11-17'
                },
                ReturnValues : 'UPDATED_NEW'
            };
            updateItem(paramsUpdate);
            itemIndex++;

        }
    }
});

在DynamoDB中,partition key + sort key被当作一个"composite primary key"来唯一标识一个item(相反,Dynamo也支持简单主键,只包含partition key)。所以你需要两者来更新一个项目。这就是您可以拥有两个具有相同分区键但不同排序键的项目的原因。因此,如果您只提供分区键,Dynamo 会混淆要更新的项目。

对于您当前的 table 配置,在给定分区键的情况下更新项目的唯一方法是使用仅分区键进行 查询 以获取所有项目, 和 filter 筛选出具有预期排序键的那个。然后使用分区键和排序键的组合更新此项。