. Advertisement .
..3..
. Advertisement .
..4..
If you have ever found the problem with DynamoDB, it is a common error because DynamoDB is considered as a hosted database for NoSQL that AWS (Amazon Web Services) offers. DynamoDB offers reliable performance; a managed experience; and a small, simple API.
DynamoDB is also a particularly great fit for these use cases as applications with big data amounts and accurate latency requirement; Serverless applications applying AWS Lambda; and simple and known data sets.
If you know the useful information of DynamoDB, you are able to learn how to deal with their errors such as the “ValidationException: The provided key element does not match the schema” issue. Refer to this article to explore it!
When does it occur?
You have designed a DynamoDB table “Movie” table wby the instruction from
Aws.amazon in Javascript with the feature as follow:
var tableAttrs = { TableName : "Movies", KeySchema: [ { AttributeName: "year", KeyType: "HASH"}, { AttributeName: "title", KeyType: "RANGE" } ], AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N" }, { AttributeName: "title", AttributeType: "S" } ], ProvisionedThroughput: { ReadCapacityUnits: 5, WriteCapacityUnits: 5 } };
The command of batchGet is below that you would like to add:
var params = { "RequestItems" : { "Movies": { "Keys" : [ {year : { "N" : "1000" } }, {year : { "N" : "1001" } } ] } } }
You operate it with as:
let db = new DynamoDB.DocumentClient({ apiVersion: '2012-08-10' }) let result = db.batchGet(params).promise();
So, the error is appearing in the console with the following message:
ValidationException: The provided key element does not match the schema
The table below is considered the screenshot displaying the keys of table and you are confused that the added year is a key component doesn’t fit the schema:
......... ADVERTISEMENT .........
..8..
Solutions on fixing this error
To solve this problem, we will re-read the instruction in the BatchGetItem that it guides us to correct it in. For further information, you can search here
So, you have to define the entire basic key like a combination between sort and partition key. It is being used the same with the GetItem too:
const AWS = require("aws-sdk"); AWS.config.update({ region: "us-east-1" }); var documentClient = new AWS.DynamoDB.DocumentClient(); documentClient.batchGet( { RequestItems: { Movies: { Keys: [ { year: 1000, title: "one" }, { year: 1000, title: "two" }, ], }, }, }, function (err, data) { console.log("err", err, "data", JSON.stringify(data)); } );
On the other hand, you are able to use query to get on records with Partition Key. Here is the command for the query shown:
const AWS = require("aws-sdk"); AWS.config.update({ region: "us-east-1" }); var documentClient = new AWS.DynamoDB.DocumentClient(); documentClient.query( { TableName: "Movies", KeyConditionExpression: "#year = :yearValue ", ExpressionAttributeValues: { ":yearValue": 1000, }, ExpressionAttributeNames: { "#year": "year", }, }, function (err, data) { if (err) console.error(err); else console.log("dynamodb query succeeded:", JSON.stringify(data, null, 2)); } );
Conclusion
Hopefully after this article you have gained new knowledge to solve the problem when encountering “ValidationException: The provided key element does not match the schema” by using the command of DynamoDB batchGet. If there are any challenges or have any questions for us, please contact us immediately for answers. Thank you!
Leave a comment