You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用BatchWriteCommand写入编组数据时遇ValidationException问题求助

DynamoDB BatchWriteCommand 触发ValidationException:The provided key element does not match the schema

在AWS Lambda中使用BatchWriteCommand将编组(marshall)后的数据存入AWS DynamoDB时,触发ValidationException,错误信息为:The provided key element does not match the schema。

相关代码

export interface MyEvent {
    eventId: number
    eventTime: number
}

export interface MyModel {
    category: string
    status: string
    details: MyEvent[]
}

const dbClient = new DynamoDB({ apiVersion: "2012-08-10", "ap-southeast-2" })
export const ddbDocClient = DynamoDBDocument.from(dbClient)

export async function save(myItems: MyModel[]): Promise<void> {
    const marshalledData = myItems.map(event => ({
        PutRequest: {
            Item: marshall({
                myPK: "111111",
                mySortKey: event.category,
                status: event.status,
                eventDetails: event.details
            })
        }
    }))
    const command = new BatchWriteCommand({
        RequestItems: {
            ["events"]: marshalledData
        }
    })
    try {
        await ddbDocClient.send(command)
    } catch (e) {
        throw Error(`Error ${(<Error>e).message} `)
    }
}

尝试过的操作

若仅对eventDetails单独编组(eventDetails: marshall(event.details)),数据可存入但格式异常,会变成:

[ { "M" : { "M" : { "M" ....

而预期的正确格式应为:

{
    L: [
      {
        M: {

更新:编组后的数据结构

编组后数据的JSON结构如下:

{
    category: { S: 'Accounts' },
    status: { S: 'Active' },
    details: {
        L: [
            {
                M: {
                    eventId: { N: '101' },
                    eventTime: { N: '1686369780' }
                }
            },
            {
                M: {
                    eventId: { N: '102' },
                    eventTime: { N: '1686371880' }
                }
            }
        ]
    }
}

恳请帮忙排查问题原因。

内容的提问来源于stack exchange,提问作者Spartacus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 22:30:18