使用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
相关产品推荐
相关产品推荐

