2017-04-27 79 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