0

我有一个包含超过 25 个项目的表,并编写了一个基本脚本将它们分成 25 个项目的子数组,然后循环通过该子数组集合以在 AWS DynamoDB 客户端中运行批量写入项目命令。我得到的问题是返回的验证错误。当我通过 aws-cli 运行相同的种子文件时,它会完美地为表格播种。这让我觉得这与我的剧本有关。看到我遗漏的东西了吗?提前致谢!

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)
    }
}

这是格式化的请求对象和错误:

before db.batchWriteItem:  
{ RequestItems:
   { 'some-table-name': [ [Object], [Object], [Object], [Object] ] } 
}
In the request handler...
Request caused a DB error.
ERROR: ValidationException: Invalid attribute value type
{
  "message": "Invalid attribute value type",
  "code": "ValidationException",
  "time": "2020-08-04T10:51:13.751Z",
  "requestId": "dd49628c-6ee9-4275-9349-6edca29636fd",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 47.94198279972915
}
4

1 回答 1

2

您在 nodejs 代码中使用 DocumentClient。这将自动将 DynamoDB 使用的数据格式转换为更易于使用的格式。

例如

  {
     "id": {
       "S": "A string value"
     }
  }

会成为

  {
     "id": "A string value"
  }

CLI 不执行此数据转换。您可以使用常规 DynamoDB 客户端不在 Nodejs 中执行此转换。例如const db = new Dynamodb()

于 2020-08-04T12:52:20.780 回答