I am using dynamoDB local. I want to create a table with 6 attributes, only one of them is key
. How do I do that? Specify the key attribute in keySchema
and all attributes in AttributeDefinitions
?
var params = {
TableName : "Movies",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
{ AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
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));
}
});
Are you receiving the following error?
One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions
This is because your AttributeDefinitions
contains an attribute that is not defined in the KeySchema
. If you're only going to use a HASH
key, and not going to need a RANGE
key, you can remove the title
attribute from the AttributeDefinitions
.
DynamoDB
is schemaless so you don't need to include any non-key attribute definitions in AttributeDefinitions
. You can add any additional attribute(s) when you put an item in your table (have to include Partition/Sort keys).
The following code will create a table with only a HASH (Partition) key
:
var dynamodb = new AWS_SDK.DynamoDB();
var params = {
TableName : "MyNewTable",
KeySchema: [
{ AttributeName: "year", KeyType: "HASH"}, //Partition key
//{ AttributeName: "title", KeyType: "RANGE"}, //Sort key
],
AttributeDefinitions: [
{ AttributeName: "year", AttributeType: "N" },
// { AttributeName: "title", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
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));
}
For more info, you can refer to the AWS SDK documentation for the createTable
function on the DynamoDB
service.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With