Dynamodb- Adding non key attributes

Anshul Verma picture Anshul Verma · Jul 1, 2016 · Viewed 9.2k times · Source

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.

Answer

notionquest picture notionquest · Jul 1, 2016

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