DynamoDB Cookbook
上QQ阅读APP看书,第一时间看更新

Creating a table using the AWS SDK for .Net

Now, let's understand how to create a DynamoDB table using the AWS SDK for .Net.

Getting ready

You can use an IDE, such as Visual Studio to code these recipes. You can refer to the AWS documentation on how to set up your workstation at http://aws.amazon.com/sdk-for-net/.

How to do it…

Let's start with creating a table called productTableNet:

  1. Instantiate the CreateTable request specifying AttributeDefinition and KeySchema. We will create the table having both the HASH and RANGE Keys. We will set the provisioned read and write capacity units to be one:
    AmazonDynamoDBClient client = new AmazonDynamoDBClient();
    string tableName = "productTableNet";
    var request=new CreateTableRequest{
      AttributeDefinitions=newList<AttributeDefinition>(){
        newAttributeDefinition{
          AttributeName="id", AttributeType="N"
        },
        newAttributeDefinition{
          AttributeName="type", AttributeType="S"
        }
      },
      KeySchema=newList<KeySchemaElement>{
        newKeySchemaElement{
          AttributeName="id", KeyType="HASH"
        },
        newKeySchemaElement{
          AttributeName="type", KeyType="RANGE"
        }
      },
      ProvisionedThroughput=newProvisionedThroughput{
        ReadCapacityUnits=1, WriteCapacityUnits=1
      },
      TableName=tableName
    };
  2. Now, initiate the DynamoDB Client and invoke the createTable method to actually create the table on DynamoDB:
    var response = client.CreateTable(request);
  3. Now, you can go to the DynamoDB console to confirm whether the table is created.

How it works…

Once we invoke these methods, the internal DynamoDB APIs are called to create the table with a specified feature on DynamoDB.