amazon-web-services - Dynamodb- 添加非关键属性

标签 amazon-web-services amazon-dynamodb dynamo-local

我想将数据插入到 dynamodb 本地。但是,我只有一个关键属性和多个非关键属性。

{
    'id':'99876983ydbhdu3739',
    'category':'Spa',
    'latitude':'33.498',
    'longitude':'32.332',
    'name':'Studio'
}  

我有多个这样的值。这是一个记录,我想插入的一个例子。以下是我正在尝试的内容:
table = dynamodb.create_table(
    TableName='Trial',
    KeySchema=[
        {
            'AttributeName': 'facebook_id',
            'KeyType': 'HASH'  #Sort key
        },
        {
            'AttributeName': 'latitude',
            'KeyType': 'RANGE'  #Sort key
        },

    ],
    AttributeDefinitions=[
         {
            'AttributeName':'id',
            'AttributeType':'S'
        },
        {
            'AttributeName': 'category',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'latitude',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'longitude',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'name',
            'AttributeType':'S'
        }

    ],
    ProvisionedThroughput={
        'ReadCapacityUnits': 10,
        'WriteCapacityUnits': 10
    }
)

我收到以下错误:

An error occurred (ValidationException) when calling the CreateTable operation: The number of attributes in key schema must match the number of attributesdefined in attribute definitions.

最佳答案

您可以在创建表时单独使用 HASH 和 RANGE 键属性创建表。 DynamoDB 不希望定义所有其他属性,因为 DynamoDB 是一个键值对表。请尝试以下代码。您应该能够创建表。

插入项目时,您可以根据需要包含任何属性。

创建表:-

var AWS = require("aws-sdk");

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

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Trail",
    KeySchema : [ {
        AttributeName : "facebook_id",
        KeyType : "HASH"
    }, //Partition key
    {
        AttributeName : "latitude",
        KeyType : "RANGE"
    } //Sort key
    ],
    AttributeDefinitions : [ {
        AttributeName : "facebook_id",
        AttributeType : "N"
    }, {
        AttributeName : "latitude",
        AttributeType : "S"
    } ],
    ProvisionedThroughput : {
        ReadCapacityUnits : 10,
        WriteCapacityUnits : 10
    }
};

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 AWS = require("aws-sdk");

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

var docClient = new AWS.DynamoDB.DocumentClient();

var table = "Trail";

var params = {
    TableName : table,
    Item : {
        "facebook_id" : 1,
        "latitude" : 'lat',
        "longitude" : 'long',
        "name" : 'facebook',
        "category" : 'social_media'
    }
};

console.log("Adding a new item...");
docClient.put(params, function(err, data) {
    if (err) {
        console.error("Unable to add item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("Added item:", JSON.stringify(data, null, 2));
    }
});

关于amazon-web-services - Dynamodb- 添加非关键属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38151687/

相关文章:

amazon-web-services - 有没有办法从子网列表中选择在 CloudFormation 中启动 EC2 实例?

node.js - 如何使用 AWS S3 托管静态 Express/nodeJS 页面?

ruby - AWS ruby​​ sdk v2 - dynamodb 查询

amazon-web-services - DynamoDB 映射器 : update only not-null properties

node.js - 为 DynamoDB 项目创建唯一 ID

java - 如何通过 Java 中的 Executor Framework 在 DynamoDb 中获得最佳批量插入率?

node.js - 分离网络服务器和处理服务器

amazon-web-services - 已断开 没有可用的受支持的身份验证方法(服务器发送公钥) 发送公钥 gssapi key 和麦克风

scala - DynamoDB PutItemOutcome#getPutItemResult() 返回空对象

amazon-dynamodb - 错误 InvalidParameterType : Expected params. 项目 ['pid'] 成为 DynamoDB 中的结构