在本地DynamoDB

时间:2017-04-27 12:55:33

标签: amazon-dynamodb dynamo-local nosql

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

"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

3 个答案:

答案 0 :(得分: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));
    }
}); 

答案 1 :(得分:0)

var params = {
TableName: 'student',
KeySchema: [ 
    { 
        AttributeName: 'sid',
        KeyType: 'HASH',
    },
],
AttributeDefinitions: [ 
    {
        AttributeName: 'sid',
        AttributeType: 'N', 
    },
],
ProvisionedThroughput: { 
    ReadCapacityUnits: 10, 
    WriteCapacityUnits: 10, 
},
};
dynamodb.createTable(params, function(err, data) {
    if (err) ppJson(err); // an error occurred
    else ppJson(data); // successful response

});

答案 2 :(得分:0)

在创建本地DynamoDB表之前,还需要在本地安装aws-amplify cli。

npm install -g @aws-amplify/cli