'c# to create table in Dynamo DB

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



Solution 1:[1]

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.

Solution 2:[2]

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

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Viswanathan Annamalai
Solution 2 Dhanesh H S
Solution 3 Ruben Bartelink