无法更新Dynamo Db表,获取ValidationException [英] Cant update Dynamo Db table , getting ValidationException

查看:113
本文介绍了无法更新Dynamo Db表,获取ValidationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要仅使用分区键来更新dynamo db表。但是我得到了验证证明。
我创建了一个包含3个字段的表。

I need to update my dynamo db table by using only partition key. But i got validation exeption. I have created a table with 3 fields.


  1. id(分区键)

  2. 名称(排序键)

  3. age

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

Then i have triyed to update age field using only id.(tryied to modify age 30 to 40) this is my code

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

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

But i got error like this.

{ [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变量

After getting error, i modified my params variable like this

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

使用此操作,更新成功完成。如何使用不带排序键的表来更新表?

Using this, updation is successfully completed. How to update table using without sort key?

推荐答案

当前,DynamoDB更新API没有选项来更新项仅按分区键。没有类似于batchWriteItem的batchUpdateItem API。

Currently, the DynamoDB update API doesn't have an option to update the item by partition key only. There is no batchUpdateItem API similar to batchWriteItem as well.

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

So, if the sort key is not available, get all the sort keys of partition key and update each item for the partition and sort key combination.


对于主键,必须提供所有属性。对于带有简单主键的
示例,您只需为分区键提供一个值
。对于复合主键,必须同时为分区键和排序键提供
值。

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.

示例代码:-

您可能需要为表进行更改。以下代码使用 Movies表,其中 yearkey作为分区键, title作为排序键。

You may need to change it for your table. The below code uses "Movies" table which has "yearkey" as partition key and "title" as sort key.

下面的代码更新了 movies表的 createdate属性给定哈希键 2012。

The below code updates the "createdate" attribute for the given hash key "2012".

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

The variable paramsUpdate is formed based on the query operation. Please update it accordingly for your requirement (i.e. table structure). Logic remains same, you just need to change the table name and key values accordingly.

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++;

        }
    }
});

这篇关于无法更新Dynamo Db表,获取ValidationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆