typescript 批量写入dynamo时出错(AttributeValue设置了多个数据类型)sdk v3

sulc1iza  于 2023-02-25  发布在  TypeScript
关注(0)|答案(1)|浏览(192)

我尝试批量写入dynamo,但目前在Cloudwatch日志中遇到此错误

Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes

以批处理方式写入dynamo的逻辑如下

const writeThingsInBatch = async (things: Things[], accountId: string, key: string): Promise<void> => {
    if (!tableName) {
        throw new Error(DynamoError.InvalidTableName);
    }

    const putRequests: PutRequest[] = things.map((thing) => ({ Item: enhancedSearchFilterToItem(key, accountId, thing) }));
    const putRequestChunks: PutRequest[][] = chunk(putRequests, 25); //lodash

    for (const putRequestChunk of putRequestChunks) {
        try {
            console.log("Request chunk", JSON.stringify(putRequestChunk))
            const data = await dynamoClient.send(new BatchWriteCommand({ RequestItems: { tableName: putRequestChunk } }));
            console.log("data from batch", { data })
            if (data.UnprocessedItems) {
                console.warn("Some items have not been processed in BatchWriteCommand", { UnprocessedItems: data.UnprocessedItems });
                return;
            }
            return;
        } catch (error) {
            console.error("Error when Batch writing to dynamo", { error });
        }
    }
};

try之后,我有一个日志来检查我添加到Request.tableName中的区块的外观,从那里可以看到数据看起来是正确的

[
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010250",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010251",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010252",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010253",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010254",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010255",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010256",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010257",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
    {
        "Item": {
            "key": "d43ecafc-233e-35e2-a7c0-124e833123b3",
            "reference": "61498eeaa995ad0073bb8444#0010258",
            "code": "numberOfComments = 2",
            "allowed": true,
            "isSyncable": true,
            "syncIt": "1676972136978"
        }
    },
]

我已经阅读了一些帖子,到目前为止,人们的错误似乎不是我所做的,除非我错过了一些真正容易的。任何帮助将不胜感激。

6vl6ewon

6vl6ewon1#

发现了这个问题,如果有人福尔斯这个问题,解决方案是在RequestItems.tableName数组中包含的每个项中实际包含我们对dynamo PutRequest发出的请求类型。
更新以下代码

type BatchPutRequest = {
    PutRequest: PutRequest
}

const writeFiltersInBatch = async (filters: EnhancedSearchFilter[], accountId: string, clientKey: string): Promise<void> => {
    if (!esFilterTableName) {
        throw new Error(DynamoError.InvalidTableName);
    }

    const putRequests: BatchPutRequest[] = filters.map((filter) => ({ PutRequest: { Item: enhancedSearchFilterToItem(clientKey, accountId, filter) } }));
    const putRequestChunks: BatchPutRequest[][] = chunk(putRequests, 25);

    for (const putRequestChunk of putRequestChunks) {
        try {
            console.log("Request chunk", JSON.stringify(putRequestChunk))
            const data = await dynamoClient.send(new BatchWriteCommand({ RequestItems: { esFilterTableName: putRequestChunk } }));
            console.log("data from batch", { data })
            if (data.UnprocessedItems) {
                console.warn("Some items have not been processed in BatchWriteCommand", { UnprocessedItems: data.UnprocessedItems });
                return;
            }
            return;
        } catch (error) {
            console.error("Error when Batch writing to dynamo", { error });
        }
    }
};

相关问题