2017-04-27 80 views
1

我已经为Amazon DynamoDB下载了本地版本。我正在尝试使用shell创建一个表。当我从shell中运行代码它给我一个错误:在本地DynamoDB中创建表时出错

"message":"The security token included in the request is invalid." 
"code":"UnrecognizedClientException" 
"time":"2017-04-27T12:50:35.880Z" 
"statusCode":400 
"retryable":false 

创建代码:

var dynamodb = new AWS.DynamoDB(); 
var params = { 
    "AttributeDefinitions": [ 
     { 
      "AttributeName": "UserId", 
      "AttributeType": "N" 
     }, 
     { 
      "AttributeName": "FirstName", 
      "AttributeType": "S" 
     }, 
     { 
      "AttributeName": "LastName", 
      "AttributeType": "S" 
     }, 
     { 
      "AttributeName": "CellPhoneNumber", 
      "AttributeType": "N" 
     } 
    ], 
    "TableName": "Users", 
    "KeySchema": [ 
     { 
      "AttributeName": "UserId", 
      "KeyType": "HASH" 
     }, 
     { 
      "AttributeName": "CellPhoneNumber", 
      "KeyType": "RANGE" 
     } 
    ], 
    "LocalSecondaryIndexes": [ 
     { 
      "IndexName": "UserIndex", 
      "KeySchema": [ 
       { 
        "AttributeName": "UserId", 
        "KeyType": "HASH" 
       }, 
       { 
        "AttributeName": "CellPhoneNumber", 
        "KeyType": "RANGE" 
       } 
      ], 
      "Projection": { 
       "ProjectionType": "KEYS_ONLY" 
      } 
     } 
    ], 
    "ProvisionedThroughput": { 
     "ReadCapacityUnits": 5, 
     "WriteCapacityUnits": 5 
    } 
} 

dynamodb.createTable(params, function(err, data) { 
    if (err) ppJson(err); // an error occurred 
    else ppJson(data); // successful response 

}); 

如何创建本地DynamoDB表?我是否需要先创建一个数据库?我问这是因为我一直在SQL上工作,这是我第一次使用NoSQL

回答

0

无需创建数据库。只需要创建表格。

对本地dynamodb使用下面的配置。端点URL很重要。其他属性是虚拟值(即可以是任何值)。

var creds = new AWS.Credentials('akid', 'secret', 'session'); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000", 
    credentials : creds 
}); 

此外,创建表时不需要定义所有属性。只有关键属性需要定义。否则,你会得到错误。

完整的代码来创建表(应http://localhost:8000/shell/):-

var dynamodb = new AWS.DynamoDB({ 
    region: 'us-east-1', 
    endpoint: "http://localhost:8000" 
}); 
var tableName = "Movies"; 

var params = { 
    "AttributeDefinitions": [ 
     { 
      "AttributeName": "UserId", 
      "AttributeType": "N" 
     }, 
     { 
      "AttributeName": "CellPhoneNumber", 
      "AttributeType": "N" 
     } 
    ], 
    "TableName": "PBUsers", 
    "KeySchema": [ 
     { 
      "AttributeName": "UserId", 
      "KeyType": "HASH" 
     }, 
     { 
      "AttributeName": "CellPhoneNumber", 
      "KeyType": "RANGE" 
     } 
    ], 
    "LocalSecondaryIndexes": [ 
     { 
      "IndexName": "UserIndex", 
      "KeySchema": [ 
       { 
        "AttributeName": "UserId", 
        "KeyType": "HASH" 
       }, 
       { 
        "AttributeName": "CellPhoneNumber", 
        "KeyType": "RANGE" 
       } 
      ], 
      "Projection": { 
       "ProjectionType": "KEYS_ONLY" 
      } 
     } 
    ], 
    "ProvisionedThroughput": { 
     "ReadCapacityUnits": 5, 
     "WriteCapacityUnits": 5 
    } 
} 

dynamodb.createTable(params, function(err, data) { 
    if (err) { 
     if (err.code === "ResourceInUseException" && err.message === "Cannot create preexisting table") { 
      console.log("message ====>" + err.message); 
     } else { 
      console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2)); 
     } 

    } else { 
     console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2)); 
    } 
}); 
+0

当我执行在外壳上面的代码(HTTP执行://本地主机:8000 /壳/),我得到这个错误:“ ReferenceError:require未定义“ – User2682

+0

更新了脚本。 – notionquest