5

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

2 Answers 2

9

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!

Sign up to request clarification or add additional context in comments.

Comments

0

Short answer: You can't.

You can't add non-key attributes while creating the table. You have to create the table first, then add non-key attributes using dynamodb.putItem() or dynamodb.batchWriteItem().

Example:

var params = {
  TableName: 'Movies',
  Item: {
    'year': {N: '1994'},
    'title': {S: 'Pulp Fiction'},
    'description': {S: 'Two hitmen with a penchant for philosophical discussions.'},
  }
}

dynamodb.putItem(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.