Dynamodb- Adding non key attributes
Asked Answered
R

2

17

I want to insert data into dynamodb local. However, I have only one key attribute and multiple non-key attributes.

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

I have multiple such values. This is one record, an example of what I want to insert. Following is what I am trying:

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
    }
)

I get the following error:

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.

Rumba answered 1/7, 2016 at 18:16 Comment(0)
C
22

You can just create the table with HASH and RANGE key attributes alone while creating the table. DynamoDB doesn't expect to define all the other attributes as DynamoDB is a key-value pair table. Please try the below code. You should be able to create the table.

While inserting an item, you can include any attributes as per your requirement.

Create Table :-

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));
    }
});

Create Item:-

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));
    }
});
Calamity answered 1/7, 2016 at 21:31 Comment(1)
But why there is an option to define attributes when creating a table at all?Maine
R
1

For Boto3 Users in Python:

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'}
    }
)
Rockhampton answered 16/5, 2019 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.