我有一个JSON,其中包含以下格式的对象数组。如何使用Lambda函数(NodeJS)在dynamoDB中添加它。假设我的JSON位于s3存储桶名称“ databucket”中,文件名为data.json。创建数据库表时,我已使用“ userId”作为键。预先感谢
{
"records": [
{
"first-name": "Mark",
"last-name": "rob",
"userId": "fvdvfd"
},
{
"first-name": "Alan",
"last-name": "thomas",
"userId": "wee"
},
{
"first-name": "Joan",
"last-name": "Young",
"userId": "wee"
}
]
}
答案 0 :(得分:1)
第二,您应该创建dynamodb参数以提供给dynamodb客户端PUT方法。由于它是一个对象数组,因此您必须遍历该数组并放置每个对象,否则可以按如下方式在dynamodb中使用batchWrite方法,
var AWS = require('aws-sdk');
var insertBatchRecords = function(callback) {
var dynamodb = new AWS.DynamoDB();
var bulkInsertParams = {
RequestItems: {
"Records": [
{
PutRequest: {
Item: {
"first-name": "Mark",
"last-name": "rob",
"userId": "fvdvfd"
}
}},
{
PutRequest: {
Item: {
"first-name": "Alan",
"last-name": "thomas",
"userId": "wee"
}
}}]
}
};
dynamodb.batchWriteItem(bulkInsertParams, callback);
}