Amazon dynamodb DynamoDB表种子在cli中工作,但在AWS-SDK中不工作
我有一个表,其中包含25个以上的项,并编写了一个基本脚本,将它们分解为25个项的子数组,然后循环通过该子数组集合,在AWS DynamoDB客户端中运行批写项命令。我遇到的问题是返回的验证错误。当我通过aws cli运行相同的种子文件时,它会完美地为表种子。这让我觉得这和我的剧本有关。看到我丢了什么吗?提前谢谢Amazon dynamodb DynamoDB表种子在cli中工作,但在AWS-SDK中不工作,amazon-dynamodb,seed,amazon-dynamodb-local,Amazon Dynamodb,Seed,Amazon Dynamodb Local,我有一个表,其中包含25个以上的项,并编写了一个基本脚本,将它们分解为25个项的子数组,然后循环通过该子数组集合,在AWS DynamoDB客户端中运行批写项命令。我遇到的问题是返回的验证错误。当我通过aws cli运行相同的种子文件时,它会完美地为表种子。这让我觉得这和我的剧本有关。看到我丢了什么吗?提前谢谢 var { DynamoDB } = require('aws-sdk'); var db = new DynamoDB.DocumentClient({ region: 'loca
var { DynamoDB } = require('aws-sdk');
var db = new DynamoDB.DocumentClient({
region: 'localhost',
endpoint: 'http://localhost:8000',
});
const allItems = require('./allItems.json');
const tableName = 'some-table-name';
console.log({ tableName, allItems });
var batches = [];
var currentBatch = [];
var count = 0;
for (let i = 0; i < allItems.length; i++) {
//push item to the current batch
count++;
currentBatch.push(allItems[i]);
if (count === 25) {
batches.push(currentBatch);
currentBatch = [];
}
}
//if there are still items left in the curr batch, add to the collection of batches
if (currentBatch.length > 0 && currentBatch.length !== 25) {
batches.push(currentBatch);
}
var completedRequests = 0;
var errors = false;
//request handler for DynamoDB
function requestHandler(err, data) {
console.log('In the request handler...');
return function (err, data) {
completedRequests++;
errors = errors ? true : err;
//log error
if (errors) {
console.error('Request caused a DB error.');
console.error('ERROR: ' + err);
console.error(JSON.stringify(err, null, 2));
} else {
var res = {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
},
body: JSON.stringify(data),
isBase64Encoded: false,
};
console.log(`Success: returned ${data}`);
return res;
}
if (completedRequests == batches.length) {
return errors;
}
};
}
//Make request
var params;
for (let j = 0; j < batches.length; j++) {
//items go in params.RequestedItems.id array
//format for the items is {PutRequest : {Item: ITEM_OBJECT}}
params = '{"RequestItems": {"' + tableName + '": []}}';
params = JSON.parse(params);
params.RequestItems[tableName] = batches[j];
console.log('before db.batchWriteItem: ', params);
try {
//send to db
db.batchWrite(params, requestHandler(params));
} catch{
console.error(err)
}
}
您正在nodejs代码中使用DocumentClient。这将自动将DynamoDB使用的数据格式转换为更易于使用的格式 e、 g 将成为
{
"id": "A string value"
}
CLI不执行此数据转换。
您可以使用常规的DynamoDB客户端在Nodejs中不执行此转换。e、 g.
const db=new Dynamodb()
谢谢你的回答。我最终使用exec函数直接写入cli,而不是使用documentClient
{
"id": {
"S": "A string value"
}
}
{
"id": "A string value"
}