Dynamo Bad Api例外

时间:2017-10-15 16:18:00

标签: amazon-web-services amazon-dynamodb

我目前有一张桌子:

{
  "AttributeDefinitions": [
    {
      "AttributeName": "soAssetId",
      "AttributeType": "N"
    },
    {
      "AttributeName": "timestamp",
      "AttributeType": "N"
    }
  ],
  "TableName": "odm-rates",
  "KeySchema": [
    {
      "AttributeName": "soAssetId",
      "KeyType": "HASH"
    },
    {
      "AttributeName": "timestamp",
      "KeyType": "RANGE"
    }
  ],
  "TableStatus": "ACTIVE",
  "CreationDateTime": "2017-10-15T15:24:14.470Z",
  "ProvisionedThroughput": {
    "LastIncreaseDateTime": "1970-01-01T00:00:00.000Z",
    "LastDecreaseDateTime": "1970-01-01T00:00:00.000Z",
    "NumberOfDecreasesToday": 0,
    "ReadCapacityUnits": 5,
    "WriteCapacityUnits": 5
  },
  "TableSizeBytes": 0,
  "ItemCount": 0,
  "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/odm-rates",
  "GlobalSecondaryIndexes": [
    {
      "IndexName": "rateRange",
      "KeySchema": [
        {
          "AttributeName": "soAssetId",
          "KeyType": "HASH"
        },
        {
          "AttributeName": "timestamp",
          "KeyType": "RANGE"
        }
      ],
      "Projection": {
        "ProjectionType": "ALL"
      },
      "IndexStatus": "ACTIVE",
      "ProvisionedThroughput": {
        "ReadCapacityUnits": 5,
        "WriteCapacityUnits": 5
      },
      "IndexSizeBytes": 0,
      "ItemCount": 0,
      "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/odm-rates/index/rateRange"
    }
  ]
}

当我试着写:

dynamo.batchWriteItem({
              RequestItems: {
                "odm-rates": [{},
                  {
                    PutRequest: {
                        Item: {
                            "soAssetId": { N: "143" },
                            "value": { S: "13423423" },
                            "bidValue": { S: "234234234" },
                            "askValue": { S: "5433434343" },
                            "timestamp": { N: "324234234" }
                        }
                    }
                  }
                ]
              }
            }, function(err, data) {
    console.log("LOG");
   if (err) console.log(err);
   else console.log(data);
});

我得到以下异常:

{ ValidationException: Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes
    at Request.extractError (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/protocol/json.js:48:27)
    at Request.callListeners (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at Request.emit (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /sites/nsfxpricer/node/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/sites/nsfxpricer/node/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
  message: 'Supplied AttributeValue has more than one datatypes set, must contain exactly one of the supported datatypes',
  code: 'ValidationException',
  time: 2017-10-15T16:10:16.201Z,
  requestId: '61bdb8cc-cb17-401a-b461-c22f2efd7454',
  statusCode: 400,
  retryable: false,
  retryDelay: 36.32793383670548 }

我尝试了200种不同的插入数据的方式,但实际的异常似乎与底层问题完全无关。官方API是简单的poo-poo吗?如何正确创建二级索引?

1 个答案:

答案 0 :(得分:1)

batchwriteitems的语法错误。您需要指定正在执行的操作类型以及批量写入。

请查看文档,

http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_BatchWriteItem.html

如,

 "RequestItems": {
        "Forum": [
            {
                "PutRequest": {
                    "Item": {
                        "Name": {
                            "S": "Amazon DynamoDB"
                        },
                        "Category": {
                            "S": "Amazon Web Services"
                        }
                    }
                }
            },
            {
                "PutRequest": {
                    "Item": {
                        "Name": {
                            "S": "Amazon RDS"
                        },
                        "Category": {
                            "S": "Amazon Web Services"
                        }
                    }
                }
            }
]}

希望它有所帮助。