0

I am trying to create a table in DynamoDB and post that, list out all the existing tables. The code I used is

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Amazon;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.Model;
using Amazon.Runtime;

namespace DynamoDBTester
{
class Program
{
    private static AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    private static string tableName = "DummyTable";
    static void Main(string[] args)
    {
       // try
        //{
            CreateDummyTable();
           // ListMyTables();

            Console.WriteLine("To continue, press Enter");
            Console.ReadLine();
        //}
        //catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
        //catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
        //catch (Exception e) { Console.WriteLine(e.Message); }
    }

    private static void CreateDummyTable()
    {
        Console.WriteLine("\n*** Creating DummyTable ***");
        var request = new CreateTableRequest
        {
            AttributeDefinitions = new List<AttributeDefinition>()
        {
            new AttributeDefinition
            {
                AttributeName = "Id",
                AttributeType = "N"
            }
            ,
            new AttributeDefinition
            {
                AttributeName = "DateTime",
                AttributeType = "S"
            }
            ,
            new AttributeDefinition
            {
                AttributeName = "Temperature",
                AttributeType = "N"
            }
        },
            KeySchema = new List<KeySchemaElement>
            {
                new KeySchemaElement
            {
                AttributeName = "Id",
                KeyType = "HASH" //Partition key
            },
                new KeySchemaElement
            {
                AttributeName = "DateTime",
                KeyType = "RANGE" //Partition key
            },
                new KeySchemaElement
            {
                AttributeName = "Temperature",
                KeyType = "RANGE" //Partition key
            }

        },
            ProvisionedThroughput = new ProvisionedThroughput
            {
                ReadCapacityUnits = 5,
                WriteCapacityUnits = 6
            },
            TableName = tableName
        };
        var response = client.CreateTable(request);
        var tableDescription = response.TableDescription;
        Console.WriteLine("{1}: {0} \t ReadsPerSec: {2} \t WritesPerSec: {3}",
                  tableDescription.TableStatus,
                  tableDescription.TableName,
                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

        string status = tableDescription.TableStatus;
        Console.WriteLine(tableName + " - " + status);

        WaitUntilTableReady(tableName);
    }
    private static void WaitUntilTableReady(string tableName)
    {
        string status = null;
        // Let us wait until table is created. Call DescribeTable.
        do
        {
            System.Threading.Thread.Sleep(5000); // Wait 5 seconds.
            try
            {
                var res = client.DescribeTable(new DescribeTableRequest
                {
                    TableName = tableName
                });

                Console.WriteLine("Table name: {0}, status: {1}",
                          res.Table.TableName,
                          res.Table.TableStatus);
                status = res.Table.TableStatus;
            }
            catch (ResourceNotFoundException)
            {
                // DescribeTable is eventually consistent. So you might
                // get resource not found. So we handle the potential exception.
            }
        } while (status != "ACTIVE");
    }
    private static void ListMyTables()
    {
        Console.WriteLine("\n*** listing tables ***");
        string lastTableNameEvaluated = null;
        do
        {
            var request = new ListTablesRequest
            {
                Limit = 2,
                ExclusiveStartTableName = lastTableNameEvaluated
            };

            var response = client.ListTables(request);
            foreach (string name in response.TableNames)
                Console.WriteLine(name);

            lastTableNameEvaluated = response.LastEvaluatedTableName;
        } while (lastTableNameEvaluated != null);
    }



}
}

But I am getting and error as

Additional information: 1 validation error detected: Value '[com.amazonaws.dynamodb.v20120810.KeySchemaElement@21c24a, com.amazonaws.dynamodb.v20120810.KeySchemaElement@7357d4d9, com.amazonaws.dynamodb.v20120810.KeySchemaElement@7b38ae72]' at 'keySchema' failed to satisfy constraint: Member must have length less than or equal to 2

My table name is DummyTable

It should have 3 columns:

1.Id

2.DateTime

3.Temperature

where Id is the PrimaryKey

2
  • It says that key schema can not have more than 2 items. At max you can have one partition key and one sortkey as part of key schema. Commented May 30, 2017 at 11:35
  • I tried removing the Temperature attribute from the KeySchema. Then I'm getting an error as Additional information: One or more parameter values were invalid: Number of attributes in KeySchema does not exactly match number of attributes defined in AttributeDefinitions Commented May 30, 2017 at 11:49

3 Answers 3

3

While creating table , you have to keep only columns which will have hash or range schemaattribute.

for additional columns you dont need to mention while creating the table. while inserting an item you can dynamically append any number of columns to the record and it will be saved against the specified hash/range attribute.

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

Comments

0

Problem:-

1) Only one attribute can be defined as RANGE key. You have two attributes DateTime and Temperature defined as RANGE key

Solution:-

If you need two different RANGE keys, you can use Local Secondary Index (LSI). A table can have 5 LSI.

LSI

Comments

0

While creating the Table you only need to specify the Primary key of that table (KeySchema) either

a.simple primary key (only partition key) or

b.complex primary key(partition +sort key)

In your case its complex primary key and you only need to mention partition key and sort key (only schema required for dynamo db )

No need to mention the additional attribute (additional column name while creating the table) .

Code change needed is to remove the addition attribute definition and schema

var request = new CreateTableRequest
{
    AttributeDefinitions = new List<AttributeDefinition>()
    {
        new AttributeDefinition
        {
            AttributeName = "Id",
            AttributeType = "N"
        },
        new AttributeDefinition
        {
            AttributeName = "DateTime",
            AttributeType = "S"
        }
    },

    KeySchema = new List<KeySchemaElement>
    {
        new KeySchemaElement
        {
            AttributeName = "Id",
            KeyType = "HASH" //Partition key
        },
        new KeySchemaElement
        {
            AttributeName = "DateTime",
            KeyType = "RANGE" //Range key
        }
    }

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.