Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamodb- Adding non key attributes

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.

like image 669
Anshul Verma Avatar asked Jul 01 '16 18:07

Anshul Verma


People also ask

What is a non key attribute?

Non-key attributes are attributes that are not part of any key. Generally, most attributes are simply descriptive, and fall into this category. Determining key and non-key attributes is an important modeling exercise, one that requires careful consideration.

Does DynamoDB accept null values?

Dynamodb can't accept a key with an explicitly NULL value.

How do I add a foreign key in DynamoDB?

DynamoDB does not have foreign keys. It is a NoSQL database that doesn't support that kind of relational data. There is no data integrity maintained between tables, so this sort of behavior is not built in. If you want to have this sort of behavior, you will have to model your data storage in DynamoDB a different way.

What are projected attributes DynamoDB?

Attribute projections. A projection is the set of attributes that is copied from a table into a secondary index. The partition key and sort key of the table are always projected into the index; you can project other attributes to support your application's query requirements.


1 Answers

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));
    }
});
like image 118
notionquest Avatar answered Sep 23 '22 02:09

notionquest