【问题标题】:unable to create table in dynamodb-local - aws无法在 dynamodb-local 中创建表 - aws
【发布时间】:2015-08-04 22:03:42
【问题描述】:

我正在使用 dynamoDB-local 和 nodejs 代码。

我有以下代码:

var aws = require("aws-sdk")
aws.config.update({"accessKeyId": "aaa",
                   "secretAccessKey": "bbb",
                   "region": "us-east-1"})

var awsdb = new aws.DynamoDB({ endpoint: new aws.Endpoint("http://localhost:8000") });

awsdb.createTable({
  TableName: 'myTbl',
  AttributeDefinitions: [
    { AttributeName: 'aaa', AttributeType: 'S' },
  ],
  KeySchema:[
    { AttributeName: 'aaa', KeyType: 'HASH' }
  ]
}, function() { 
    awsdb.listTables(function(err, data) {
      console.log(data)
  });
});

但它没有创建表。我在日志中收到{ TableNames: [] }。 err 为空。

【问题讨论】:

  • createTable 将返回 { TableNames: [] } 如果您的代码中已设置:::Aws.config[:stub_responses] = true(这是 Ruby,但您明白了)

标签: node.js amazon-web-services amazon-dynamodb


【解决方案1】:

您似乎在 CreateTable 请求中缺少必需的 ProvisionedThroughput 参数。所以发生的事情是 CreateTable 返回一个验证错误并且 ListTables 成功执行而没有返回任何表(代码中的“err”变量似乎是针对 ListTables 调用的)

例如以下对我有用

var aws = require("aws-sdk")
aws.config.update({"accessKeyId": "aaa",
  "secretAccessKey": "bbb",
  "region": "us-east-1"})
var awsdb = new aws.DynamoDB({ endpoint: new aws.Endpoint("http://localhost:8000") });

awsdb.createTable({
  TableName: 'myTbl',
  AttributeDefinitions: [
       { AttributeName: 'aaa', AttributeType: 'S' },
       ],
  KeySchema:[
       { AttributeName: 'aaa', KeyType: 'HASH' }
  ],
  ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
}, function(err, data) {
  if (err) 
    console.log(err, err.stack); // an error occurred
  else {
    awsdb.listTables(function(err, data) {
      console.log(data)
    });
  }
});

【讨论】:

  • 谢谢!我之前已经尝试过ProvisionedThroughput。但是我把KeyType 作为字符串 - S - 所以它不起作用,这就是我删除它的原因。之后我将其更改为HASH,但没有返回它:)
【解决方案2】:

发出 createTable 后,您必须等到表被有效创建。创建表后,它将出现在您的 listTables 调用中。您可以使用 describeTable 调用等待。

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html#createTable-property

CreateTable 是一个异步操作。收到 CreateTable 请求后,DynamoDB 会立即返回一个 TableStatus 为 CREATING 的响应。

您可以使用 DescribeTable API 来检查表格状态。

【讨论】:

  • 只是想注意 DynamoDB Local 会立即创建表,因此如果您不使用实际服务,您可以不用这样做。(当然,建议检查 TableStatus如果您打算最终使用真正的 DynamoDB,则 ACTIVE :) )
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-08
  • 2018-01-03
  • 1970-01-01
相关资源
最近更新 更多