Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDB Validation Exception - Key element does not match the schema

I'm trying to get an item from my DynamoDB but get the following error

ValidationException: The provided key element does not match the schema

The create item piece of the code works. But no the Get item.

Table Info:
Table Name: movieTable
Primary Partition Key: itemID
Primary Sort Key: sortKey

Here's the code for the create and update:

        var fbUserId;
        var params;
        var keyText;
        var attText;
        var valText;
        var dynamodb = null;
        var docClient = null;
        var appId = '405140756489952'; //from facebook
        var roleArn = 'arn:aws:iam::042765862882:role/Verzosa'; //from AWS IAM
        var resultData = null;

        document.getElementById('putThis').onclick = function () {
            dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
            docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
            keyText = document.getElementById("keyValue").value;
            attText = document.getElementById("attributeText").value;
            valText = document.getElementById("valueText").value;
            console.log("Key Value: ", keyText);
            console.log("Attribute: ", attText);
            console.log("Value: ", valText);
            params = {
                TableName: 'movieTable',
                Item: {
                    itemID: keyText,
                    sortKey: valText
                }
            };
            docClient.put(params, function(err, data){
                          if (err) console.log(err);
                          else
                          {
                          resultData = data;
                          console.log(resultData);

                          }
                          })


        };

    document.getElementById('getThis').onclick = function () {
        dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
        docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
        keyText = document.getElementById("keyValue").value;
        attText = document.getElementById("attributeText").value;
        console.log("Key Value: ", keyText);
        console.log("Attribute: ", attText);
        params = {
            TableName: 'movieTable',
            Key: {
                itemID: keyText,
            },
            ProjectionExpression: "#a",
            ExpressionAttributeNames: {
                '#a': attText
            }
        };
        docClient.get(params, function (err, data)
                      {
                      if (err)
                      {
                      console.log(err, err.stack);
                      }
                      else
                      {
                      console.log("success, logging data: ");
                      console.log(data);//shows keys
                      console.log("attribute 1 is " + data.Item.sortKey)
                      //var output = data.Item.attribute1;
                      l = document.getElementById("output");
                      l.innerHTML = data.Item.sortKey;
                      }
                      })
    };

Any help would be appreciated.

like image 440
Aldwin Verzosa Avatar asked Feb 11 '17 05:02

Aldwin Verzosa


People also ask

What is DynamoDB key schema?

A key schema specifies the attributes that make up the primary key of a table, or the key attributes of an index. A KeySchemaElement represents exactly one attribute of the primary key. For example, a simple primary key would be represented by one KeySchemaElement (for the partition key).

Does DynamoDB need schema?

A relational database management system (RDBMS) requires you to define the table's schema when you create it. In contrast, DynamoDB tables are schemaless—other than the primary key, you do not need to define any extra attributes or data types when you create a table.

Does DynamoDB primary key need to be unique?

The primary key uniquely identifies each item in the table, so that no two items can have the same key. DynamoDB supports two different kinds of primary keys: Partition key – A simple primary key, composed of one attribute known as the partition key.

What is Hashkey and Rangekey in DynamoDB?

DynamoDB supports two types of primary keys, a Hash Key and a Hash and Range Key. A Hash Key consists of a single attribute that uniquely identifies an item. A Hash and Range Key consists of two attributes that together, uniquely identify an item.


1 Answers

You are getting this error because when using AWS.DynamoDB.DocumentClient.get method, you must specify both hash and sort key of an item. But you have only hash key specified (itemId), and sort key is missing.

Here is how your get params should look like:

  ...
  params = {
      TableName: 'movieTable',
      Key: {
          itemID: keyText,
          sortKey: valText   // <--- sort key added 
      },
      ProjectionExpression: "#a",
      ExpressionAttributeNames: {
          '#a': attText
      }
  };
  docClient.get(params, function (err, data) {
  ...

If you'd like to get a record with a hash key only, without specifying its sort key, you should use query method instead of get:

  ...
  params = {
      TableName: 'movieTable',
      KeyConditionExpression: '#itemID = :itemID',
      ProjectionExpression: "#a",
      ExpressionAttributeNames: {
          '#a': attText,
          '#itemID': 'itemID'
      },
      ExpressionAttributeValues: {
          ':itemID': keyText
      }
  };

  dynamodbDoc.query(params, function(err, data) {
  ...

Be aware that while get method always returns 1 or no records, query can possibly return multiple records, so you would have to revisit your current implementation of get callback (e.g. instead of accessing data.Item you should use data.Items array, see query method docs)

like image 109
xtx Avatar answered Nov 14 '22 23:11

xtx