Dynamodb- Adding non key attributes
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));
}
});