admin管理员组文章数量:1431784
I am attempting to batchWrite items to my users
table, but I am receiving a ValidationException. I can't identify why The provided key element does not match the schema
. I created a new table, and the partition key is id
.
batchWrite function
const batchWriteUserData = (data) => {
const input = {
RequestItems: {
"users": data.map((item) => ({
PutRequest: {
Item: {
id: { S: item.id },
name: { S: item.name },
username: { S: item.username },
email: { S: item.email },
password: { S: item.password },
blogs: {
L: item.blogs.map((blog) => ({
M: {
id: { S: blog.id.toString() },
title: { S: blog.title },
content: { S: blog.content },
},
})),
},
},
},
})),
},
}
console.log(JSON.stringify(input, null, 2))
dynamoDB.batchWrite(input, (err, data) => {
if (err) console.error('Error:', err);
else return 'Sucess';
});
}
functions that create the data
const generateBlogData = () => {
const paragraphAmount = Math.floor((Math.random() * 5) + 1);
const titleAmount = Math.floor((Math.random() * 10) + 1);
const blogCount = Math.floor(Math.random() * 9 + 1);
const userBlogs = [];
for (let i = 0; i < blogCount; i++) {
const blogTitle = lorem.generateWords(titleAmount);
const blogContent = lorem.generateParagraphs(paragraphAmount);
const blogData = {
id: i + 1,
title: blogTitle,
content: blogContent,
};
userBlogs.push(blogData);
};
return userBlogs;
};
const generateUserData = () => {
let maxUsers = 100;
let count = 0;
let userData = []
while (maxUsers > 0) {
const prefix = lorem.generateWords(1);
const suffix = lorem.generateWords(1);
const loremPassword = lorem.generateWords(1).concat(Math.floor(Math.random() * 900) + 100);
const data = {
id: generateUniqueId(),
name: generateUniqueName(),
username: generateUniqueUsername(),
email: `${prefix}@${suffix}`,
password: loremPassword
};
data['blogs'] = generateBlogData();
if (count < 25) {
userData.push(data);
}
else if (count === 25) {
batchWriteUserData(userData);
}
count++;
maxUsers--;
}
};
Can someone identify why this is happening? I appreciate any feedback/perspective.
I've tried writing only the user data, even down to just the id to determine if the issues was the blogs data, I also tried storing the blog.id
without .toString()
and with the type as N
but the issue persists.
I am attempting to batchWrite items to my users
table, but I am receiving a ValidationException. I can't identify why The provided key element does not match the schema
. I created a new table, and the partition key is id
.
batchWrite function
const batchWriteUserData = (data) => {
const input = {
RequestItems: {
"users": data.map((item) => ({
PutRequest: {
Item: {
id: { S: item.id },
name: { S: item.name },
username: { S: item.username },
email: { S: item.email },
password: { S: item.password },
blogs: {
L: item.blogs.map((blog) => ({
M: {
id: { S: blog.id.toString() },
title: { S: blog.title },
content: { S: blog.content },
},
})),
},
},
},
})),
},
}
console.log(JSON.stringify(input, null, 2))
dynamoDB.batchWrite(input, (err, data) => {
if (err) console.error('Error:', err);
else return 'Sucess';
});
}
functions that create the data
const generateBlogData = () => {
const paragraphAmount = Math.floor((Math.random() * 5) + 1);
const titleAmount = Math.floor((Math.random() * 10) + 1);
const blogCount = Math.floor(Math.random() * 9 + 1);
const userBlogs = [];
for (let i = 0; i < blogCount; i++) {
const blogTitle = lorem.generateWords(titleAmount);
const blogContent = lorem.generateParagraphs(paragraphAmount);
const blogData = {
id: i + 1,
title: blogTitle,
content: blogContent,
};
userBlogs.push(blogData);
};
return userBlogs;
};
const generateUserData = () => {
let maxUsers = 100;
let count = 0;
let userData = []
while (maxUsers > 0) {
const prefix = lorem.generateWords(1);
const suffix = lorem.generateWords(1);
const loremPassword = lorem.generateWords(1).concat(Math.floor(Math.random() * 900) + 100);
const data = {
id: generateUniqueId(),
name: generateUniqueName(),
username: generateUniqueUsername(),
email: `${prefix}@${suffix}`,
password: loremPassword
};
data['blogs'] = generateBlogData();
if (count < 25) {
userData.push(data);
}
else if (count === 25) {
batchWriteUserData(userData);
}
count++;
maxUsers--;
}
};
Can someone identify why this is happening? I appreciate any feedback/perspective.
I've tried writing only the user data, even down to just the id to determine if the issues was the blogs data, I also tried storing the blog.id
without .toString()
and with the type as N
but the issue persists.
1 Answer
Reset to default 0You're using the document client which takes native JSON not DDB JSON which you use on your request.
const input = {
RequestItems: {
"users": data.map((item) => ({
PutRequest: {
Item: {
id: item.id ,
name: item.name ,
username: item.username ,
email: item.email ,
password: item.password ,
blogs:
item.blogs.map((blog) => ({
id: blog.id.toString() ,
title: blog.title ,
content: blog.content ,
})),
},
},
})),
},
}
Learn more here: https://aws.amazon/blogs/database/exploring-amazon-dynamodb-sdk-clients/
本文标签: amazon dynamodbDyanmoDB ValidationException batchWriteStack Overflow
版权声明:本文标题:amazon dynamodb - DyanmoDB ValidationException batchWrite - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745581122a2664628.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
import AWS from 'aws-sdk'; AWS.config.update({ accessKeyId: '', secretAccessKey: '', region: '', }); const dynamoDB = new AWS.DynamoDB.DocumentClient(); export default dynamoDB;
– Abraham Esparza Commented Nov 19, 2024 at 5:25id
and there is not a sort key. – Abraham Esparza Commented Nov 19, 2024 at 5:27