amazon-dynamodb - 在本地 DynamoDB 中创建表时出错

标签 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

最佳答案

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

对本地 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));
    }
}); 

关于amazon-dynamodb - 在本地 DynamoDB 中创建表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658490/

相关文章:

node.js - 离线构建无服务器应用程序的最佳方法是什么?

conditional - 有条件更新的 DynamoDBContext

amazon-web-services - DynamoDBMapper 有条件保存对象列表

amazon-web-services - 由于命名约定,使用 DynamoDB AWS SDK [No Mapping for Hash Key] 时出错

node.js - dynamodb 本地 shell 不使用 docker 镜像列出表

c# - 异步方法中的 Await 与 Task.Result

javascript - DynamoDB Local 在关闭实例后删除数据

database - Cassandra,查询主键,跳过聚类列(timeuuid)

java - Cassandra 中的无效请求异常

sql - 构建社交网络类型应用程序的模式?