简体   繁体   English

在本地 DynamoDB 中创建表时出错

[英]Error creating table in local DynamoDB

I have downloaded local version for Amazon DynamoDB.我已经为 Amazon DynamoDB 下载了本地版本。 I am trying to create a table using shell.我正在尝试使用 shell 创建一个表。 When I run the code from shell it gives me an error:当我从 shell 运行代码时,它给了我一个错误:

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

Create code is:创建代码是:

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

});

How do I create a table in local DynamoDB?如何在本地 DynamoDB 中创建表? Do I need to create a DB first?我需要先创建一个数据库吗? I am asking this because I have always worked on SQL and this is the first time I am using NoSQL我问这个是因为我一直在研究 SQL,这是我第一次使用 NoSQL

No need to create database.无需创建数据库。 Just need to create table.只需要创建表。

Use the below configuration for local dynamodb.对本地 dynamodb 使用以下配置。 The endpoint URL is important.端点 URL 很重要。 The other attributes are dummy values (ie it can be any values).其他属性是虚拟值(即它可以是任何值)。

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

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

Also, no need to define all the attributes while creating the table.此外,无需在创建表时定义所有属性。 Only key attributes need to be defined.只需要定义关键属性。 Otherwise, you will get error.否则,你会得到错误。

Full code to create table (should be executed on http://localhost:8000/shell/):-创建表的完整代码(应在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));
    }
}); 
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

});

You need to also install aws-amplify cli locally as well before you can create a local DynamoDB table.在创建本地 DynamoDB 表之前,您还需要在本地安装 aws-amplify cli。

npm install -g @aws-amplify/cli

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS:如何强制停止创建 DynamoDB 表? - AWS: How to force stop creating DynamoDB table? 当我尝试在本地 DynamoDB 上启用“生存时间”时出现 UnrecognizedClientException 错误 - UnrecognizedClientException error when I try to enable "time to live" on local DynamoDB DynamoDB 在本地机器上创建表 - DynamoDB create tables in local machine DynamoDB 表上的 Stream - Stream on DynamoDB table docker 上的 Serverless offline 和 DynamoDB local - Serverless offline and DynamoDB local on docker DynamoDB 中的表设计 - Table design in DynamoDB 为什么在使用 dynamodb 和 s3 存储桶创建 lambda 函数时出现 IamRoleLambdaExecution 错误? - Why I am getting IamRoleLambdaExecution error when creating a lambda function with dynamodb and s3 bucket? 获取参数验证失败:使用 boto3 dynamodb.put_item 的 dynamodb 表的返回项中出现未知参数错误 - Getting Parameter validation failed: Unknown parameter error in returned item of dynamodb table with boto3 dynamodb.put_item 如何将 AWS-SDK DynamoDB 指向本地无服务器 DynamoDB - How to point AWS-SDK DynamoDB to a serverless DynamoDB local DynamoDB 可用于大型事件表吗? - DynamoDB usable for largeish event table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM