最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon dynamodb - DyanmoDB ValidationException batchWrite - Stack Overflow

programmeradmin1浏览0评论

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.

Share Improve this question asked Nov 19, 2024 at 5:04 Abraham EsparzaAbraham Esparza 134 bronze badges 3
  • Do 2 things and I'll provide an answer. 1. Show how you create your DynamoDB client. 2. Show the output of describe table, or a screenshot of your tables schema (keys) from the console. – Leeroy Hannigan Commented Nov 19, 2024 at 5:18
  • Hi @LeeroyHannigan, here is how i define my DynamoDB client: 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:25
  • @LeeroyHannigan I don't have any data in my table at the moment, I started a fresh table to populate with this script. The primary key is id and there is not a sort key. – Abraham Esparza Commented Nov 19, 2024 at 5:27
Add a comment  | 

1 Answer 1

Reset to default 0

You'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/

发布评论

评论列表(0)

  1. 暂无评论