【问题标题】:Dynamodb- Adding non key attributesDynamodb - 添加非关键属性
【发布时间】:2016-07-01 18:16:28
【问题描述】:

我想将数据插入本地 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):键模式中的属性数必须与属性定义中定义的属性数匹配。

【问题讨论】:

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


    【解决方案1】:

    您可以在创建表时仅使用 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));
        }
    });
    

    【讨论】:

    • 但是为什么在创建表的时候可以选择定义属性呢?
    【解决方案2】:

    对于 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'}
        }
    )
    

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 2021-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多