Dynamodb- 添加非键属性

16

我想将数据插入到本地 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
    }
)

我遇到了以下错误:

调用 CreateTable 操作时发生 ValidationException 错误: 主键架构中属性数量必须与属性定义中定义的属性数量相匹配。

2个回答

22

在创建表格时,您只需使用HASH和RANGE键属性即可。因为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));
    }
});

5
为什么在创建表时有定义属性的选项呢? - Damian

0

针对Python中的Boto3用户:

ddb_client.create_table(
    TableName=DDB_TABLE_NAME,
    AttributeDefinitions=[
        {
            'AttributeName': 'id',
            'AttributeType': 'S'
        },
        {
            'AttributeName': 'latitude',
            'AttributeType': 'S'
        },
    ],
    KeySchema=[
        {
            'AttributeName': 'id',
            'KeyType': 'HASH'
        },
        {
            'AttributeName': 'latitude',
            'KeyType': 'RANGE'
        }
    ],
    BillingMode='<your_value>'
)


response = ddb_client.put_item(
    TableName=DDB_TABLE_NAME,
    Item={
        'id': {'S': '1'},
        'latitude': {'S': '33.498'},
        'longitude': {'S': '32.332'},
        'category': {'S': 'Spa'},
        'name': {'S': 'Studio'}
    }
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接