Exemple #1
0
        public void DataPlaneSamples()
        {
            {
                #region CreateTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define table schema:
                //  Table has a hash-key "Author" and a range-key "Title"
                List <KeySchemaElement> schema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Author", KeyType = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Title", KeyType = "RANGE"
                    }
                };

                // Define key attributes:
                //  The key attributes "Author" and "Title" are string types
                List <AttributeDefinition> definitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Author", AttributeType = "S"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "Title", AttributeType = "S"
                    }
                };

                // Define table throughput:
                //  Table has capacity of 20 reads and 50 writes
                ProvisionedThroughput throughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 20,
                    WriteCapacityUnits = 50
                };

                // Configure the CreateTable request
                CreateTableRequest request = new CreateTableRequest
                {
                    TableName             = "SampleTable",
                    KeySchema             = schema,
                    ProvisionedThroughput = throughput,
                    AttributeDefinitions  = definitions
                };

                // View new table properties
                TableDescription tableDescription = client.CreateTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Creation time: {0}", tableDescription.CreationDateTime);
                Console.WriteLine("Item count: {0}", tableDescription.ItemCount);
                Console.WriteLine("Table size (bytes): {0}", tableDescription.TableSizeBytes);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);

                // List table key schema
                List <KeySchemaElement> tableSchema = tableDescription.KeySchema;
                for (int i = 0; i < tableSchema.Count; i++)
                {
                    KeySchemaElement element = tableSchema[i];
                    Console.WriteLine("Key: Name = {0}, KeyType = {1}",
                                      element.AttributeName, element.KeyType);
                }

                // List attribute definitions
                List <AttributeDefinition> attributeDefinitions = tableDescription.AttributeDefinitions;
                for (int i = 0; i < attributeDefinitions.Count; i++)
                {
                    AttributeDefinition definition = attributeDefinitions[i];
                    Console.WriteLine("Attribute: Name = {0}, Type = {1}",
                                      definition.AttributeName, definition.AttributeType);
                }

                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            {
                #region DescribeTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Create DescribeTable request
                DescribeTableRequest request = new DescribeTableRequest
                {
                    TableName = "SampleTable"
                };

                // Issue DescribeTable request and retrieve the table description
                TableDescription tableDescription = client.DescribeTable(request).Table;

                // View new table properties
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Creation time: {0}", tableDescription.CreationDateTime);
                Console.WriteLine("Item count: {0}", tableDescription.ItemCount);
                Console.WriteLine("Table size (bytes): {0}", tableDescription.TableSizeBytes);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);
                // List table key schema
                List <KeySchemaElement> tableSchema = tableDescription.KeySchema;
                for (int i = 0; i < tableSchema.Count; i++)
                {
                    KeySchemaElement element = tableSchema[i];
                    Console.WriteLine("Key: Name = {0}, KeyType = {1}",
                                      element.AttributeName, element.KeyType);
                }

                // List attribute definitions
                List <AttributeDefinition> attributeDefinitions = tableDescription.AttributeDefinitions;
                for (int i = 0; i < attributeDefinitions.Count; i++)
                {
                    AttributeDefinition definition = attributeDefinitions[i];
                    Console.WriteLine("Attribute: Name = {0}, Type = {1}",
                                      definition.AttributeName, definition.AttributeType);
                }
                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            {
                #region ListTables Paging Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                string startTableName = null;
                do
                {
                    // Configure ListTables request with the marker value
                    ListTablesRequest request = new ListTablesRequest
                    {
                        ExclusiveStartTableName = startTableName,
                    };

                    // Issue call
                    ListTablesResult result = client.ListTables(request);

                    // List retrieved tables
                    List <string> tables = result.TableNames;
                    Console.WriteLine("Retrieved tables: {0}",
                                      string.Join(", ", tables));

                    // Update marker value from the result
                    startTableName = result.LastEvaluatedTableName;
                } while (!string.IsNullOrEmpty(startTableName)); // Test marker value

                #endregion
            }

            {
                #region ListTables NonPaging Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Issue call
                ListTablesResult result = client.ListTables();

                // List retrieved tables
                List <string> tables = result.TableNames;
                Console.WriteLine("Retrieved tables: {0}",
                                  string.Join(", ", tables));

                #endregion
            }

            TableUtils.WaitUntilTableActive("SampleTable", TestClient);

            {
                #region UpdateTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Define new table throughput:
                //  Table will now have capacity of 40 reads and 50 writes
                ProvisionedThroughput throughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 40,
                    WriteCapacityUnits = 50
                };

                // Compose the UpdateTable request
                UpdateTableRequest request = new UpdateTableRequest
                {
                    TableName             = "SampleTable",
                    ProvisionedThroughput = throughput
                };

                // View new table properties
                TableDescription tableDescription = client.UpdateTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Throughput: Reads = {0}, Writes = {1}",
                                  tableDescription.ProvisionedThroughput.ReadCapacityUnits,
                                  tableDescription.ProvisionedThroughput.WriteCapacityUnits);

                #endregion
            }

            TableUtils.WaitUntilTableActive("SampleTable", TestClient);

            {
                #region DeleteTable Sample

                // Create a client
                AmazonDynamoDBClient client = new AmazonDynamoDBClient();

                // Configure the DeleteTable request
                DeleteTableRequest request = new DeleteTableRequest
                {
                    TableName = "SampleTable"
                };

                // Issue DeleteTable request and retrieve the table description
                TableDescription tableDescription = client.DeleteTable(request).TableDescription;
                Console.WriteLine("Table name: {0}", tableDescription.TableName);
                Console.WriteLine("Table status: {0}", tableDescription.TableStatus);

                #endregion
            }
        }
Exemple #2
0
        private void CreateTableImpl()
        {
            var request = new CreateTableRequest
            {
                TableName = TableName,
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement("ErrorId", KeyType.HASH)
                },
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition("Application", ScalarAttributeType.S),
                    new AttributeDefinition("ErrorId", ScalarAttributeType.S),
                    new AttributeDefinition("TimeUtc", ScalarAttributeType.S)
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    new GlobalSecondaryIndex
                    {
                        IndexName = "Application-TimeUtc-index",
                        KeySchema = new List <KeySchemaElement>
                        {
                            new KeySchemaElement("Application", KeyType.HASH),
                            new KeySchemaElement("TimeUtc", KeyType.RANGE)
                        },
                        ProvisionedThroughput = new ProvisionedThroughput(ReadCapacityUnits, WriteCapacityUnits),
                        Projection            = new Projection {
                            ProjectionType = ProjectionType.ALL
                        }
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput(ReadCapacityUnits, WriteCapacityUnits),
                StreamSpecification   = new StreamSpecification
                {
                    StreamEnabled  = StreamEnabled,
                    StreamViewType = StreamViewType.NEW_IMAGE
                }
            };

            var result = _client.CreateTable(request);

            //try to wait for it to be created (up to 2 minutes)
            if (result.TableDescription.TableStatus != TableStatus.ACTIVE)
            {
                for (var i = 0; i < 60; i++)
                {
                    try
                    {
                        var describe = _client.DescribeTable(new DescribeTableRequest(TableName));
                        if (describe.Table.TableStatus == TableStatus.CREATING)
                        {
                            Thread.Sleep(TimeSpan.FromSeconds(2));
                            continue;
                        }
                        break;
                    }
                    catch (ResourceNotFoundException)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(2));
                    }
                }
            }


            if (_client.DescribeTable(new DescribeTableRequest(TableName)).Table?.TableStatus != TableStatus.ACTIVE)
            {
                throw new ResourceNotFoundException("Could not create table " + TableName);
            }
        }
        private static void CreateTable()
        {
            var createTableRequest =
                new CreateTableRequest()
            {
                TableName             = tableName,
                ProvisionedThroughput =
                    new ProvisionedThroughput()
                {
                    ReadCapacityUnits  = (long)1,
                    WriteCapacityUnits = (long)1
                }
            };

            var attributeDefinitions = new List <AttributeDefinition>()
            {
                // Attribute definitions for table primary key
                { new AttributeDefinition()
                  {
                      AttributeName = "CustomerId", AttributeType = "S"
                  } },
                { new AttributeDefinition()
                  {
                      AttributeName = "OrderId", AttributeType = "N"
                  } },
                // Attribute definitions for index primary key
                { new AttributeDefinition()
                  {
                      AttributeName = "OrderCreationDate", AttributeType = "N"
                  } },
                { new AttributeDefinition()
                  {
                      AttributeName = "IsOpen", AttributeType = "N"
                  } }
            };

            createTableRequest.AttributeDefinitions = attributeDefinitions;

            // Key schema for table
            var tableKeySchema = new List <KeySchemaElement>()
            {
                { new KeySchemaElement()
                  {
                      AttributeName = "CustomerId", KeyType = "HASH"
                  } },                                              //Partition key
                { new KeySchemaElement()
                  {
                      AttributeName = "OrderId", KeyType = "RANGE"
                  } }                                            //Sort key
            };

            createTableRequest.KeySchema = tableKeySchema;

            var localSecondaryIndexes = new List <LocalSecondaryIndex>();

            // OrderCreationDateIndex
            LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex()
            {
                IndexName = "OrderCreationDateIndex"
            };

            // Key schema for OrderCreationDateIndex
            var indexKeySchema = new List <KeySchemaElement>()
            {
                { new KeySchemaElement()
                  {
                      AttributeName = "CustomerId", KeyType = "HASH"
                  } },                                                //Partition key
                { new KeySchemaElement()
                  {
                      AttributeName = "OrderCreationDate", KeyType = "RANGE"
                  } }                                                        //Sort key
            };

            orderCreationDateIndex.KeySchema = indexKeySchema;

            // Projection (with list of projected attributes) for
            // OrderCreationDateIndex
            var projection = new Projection()
            {
                ProjectionType = "INCLUDE"
            };

            var nonKeyAttributes = new List <string>()
            {
                "ProductCategory",
                "ProductName"
            };

            projection.NonKeyAttributes = nonKeyAttributes;

            orderCreationDateIndex.Projection = projection;

            localSecondaryIndexes.Add(orderCreationDateIndex);

            // IsOpenIndex
            LocalSecondaryIndex isOpenIndex
                = new LocalSecondaryIndex()
                {
                IndexName = "IsOpenIndex"
                };

            // Key schema for IsOpenIndex
            indexKeySchema = new List <KeySchemaElement>()
            {
                { new KeySchemaElement()
                  {
                      AttributeName = "CustomerId", KeyType = "HASH"
                  } },                                                //Partition key
                { new KeySchemaElement()
                  {
                      AttributeName = "IsOpen", KeyType = "RANGE"
                  } }                                             //Sort key
            };

            // Projection (all attributes) for IsOpenIndex
            projection = new Projection()
            {
                ProjectionType = "ALL"
            };

            isOpenIndex.KeySchema  = indexKeySchema;
            isOpenIndex.Projection = projection;

            localSecondaryIndexes.Add(isOpenIndex);

            // Add index definitions to CreateTable request
            createTableRequest.LocalSecondaryIndexes = localSecondaryIndexes;

            Console.WriteLine("Creating table " + tableName + "...");
            client.CreateTable(createTableRequest);
            WaitUntilTableReady(tableName);
        }
        /// <summary>
        /// Creates all samples defined in SampleTables map
        /// </summary>
        /// <param name="client"></param>
        public static void CreateSampleTables(AmazonDynamoDBClient client)
        {
            Console.WriteLine("Getting list of tables");
            List <string> currentTables = client.ListTables().TableNames;

            Console.WriteLine("Number of tables: " + currentTables.Count);

            bool tablesAdded = false;

            if (!currentTables.Contains("Actors"))
            {
                Console.WriteLine("Table Actors does not exist, creating");
                client.CreateTable(new CreateTableRequest
                {
                    TableName             = "Actors",
                    ProvisionedThroughput = new ProvisionedThroughput {
                        ReadCapacityUnits = 3, WriteCapacityUnits = 1
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "Name",
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition {
                            AttributeName = "Name", AttributeType = ScalarAttributeType.S
                        }
                    }
                });
                tablesAdded = true;
            }

            if (!currentTables.Contains("Movies"))
            {
                Console.WriteLine("Table Movies does not exist, creating");
                client.CreateTable(new CreateTableRequest
                {
                    TableName             = "Movies",
                    ProvisionedThroughput = new ProvisionedThroughput {
                        ReadCapacityUnits = 3, WriteCapacityUnits = 1
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "Title",
                            KeyType       = KeyType.HASH
                        },
                        new KeySchemaElement
                        {
                            AttributeName = "Released",
                            KeyType       = KeyType.RANGE
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition {
                            AttributeName = "Title", AttributeType = ScalarAttributeType.S
                        },
                        new AttributeDefinition {
                            AttributeName = "Released", AttributeType = ScalarAttributeType.S
                        }
                    }
                });
                tablesAdded = true;
            }

            if (tablesAdded)
            {
                bool allActive;
                do
                {
                    allActive = true;
                    Console.WriteLine("While tables are still being created, sleeping for 5 seconds...");
                    Thread.Sleep(TimeSpan.FromSeconds(5));

                    foreach (var tableName in SAMPLE_TABLE_NAMES)
                    {
                        TableStatus tableStatus = GetTableStatus(client, tableName);
                        if (!object.Equals(tableStatus, TableStatus.ACTIVE))
                        {
                            allActive = false;
                        }
                    }
                } while (!allActive);
            }

            Console.WriteLine("All sample tables created");
        }
Exemple #5
0
        public void Can_put_item_with_null_value()
        {
            var db = new AmazonDynamoDBClient("keyId", "key", new AmazonDynamoDBConfig
            {
                ServiceURL = "http://localhost:8000",
            });

            try
            {
                db.DeleteTable(new DeleteTableRequest
                {
                    TableName = "Test",
                });

                Thread.Sleep(1000);
            }
            catch (Exception) { /*ignore*/ }

            db.CreateTable(new CreateTableRequest
            {
                TableName = "Test",
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = "HASH"
                    }
                },
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = "N",
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 10,
                    WriteCapacityUnits = 5,
                }
            });

            Thread.Sleep(1000);

            db.PutItem(new PutItemRequest
            {
                TableName = "Test",
                Item      = new Dictionary <string, AttributeValue>
                {
                    { "Id", new AttributeValue {
                          N = "1"
                      } },
                    { "Name", new AttributeValue {
                          S = "Foo"
                      } },
                    { "Empty", new AttributeValue {
                          NULL = true
                      } },
                }
            });

            var response = db.GetItem(new GetItemRequest
            {
                TableName      = "Test",
                ConsistentRead = true,
                Key            = new Dictionary <string, AttributeValue> {
                    { "Id", new AttributeValue {
                          N = "1"
                      } }
                }
            });

            Assert.That(response.IsItemSet);
            Assert.That(response.Item["Id"].N, Is.EqualTo("1"));
            Assert.That(response.Item["Name"].S, Is.EqualTo("Foo"));
            Assert.That(response.Item["Empty"].NULL);
        }
        public static void Main(string[] args)
        {
            // First, set up a DynamoDB client for DynamoDB Local
            AmazonDynamoDBConfig ddbConfig = new AmazonDynamoDBConfig();

            ddbConfig.ServiceURL = "http://localhost:8000";
            AmazonDynamoDBClient client;

            try
            {
                client = new AmazonDynamoDBClient(ddbConfig);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: failed to create a DynamoDB client; " + ex.Message);
                PauseForDebugWindow();
                return;
            }

            // Build a 'CreateTableRequest' for the new table
            CreateTableRequest createRequest = new CreateTableRequest
            {
                TableName            = "Movies",
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "year",
                        AttributeType = "N"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "title",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "year",
                        KeyType       = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "title",
                        KeyType       = "RANGE"
                    }
                },
            };

            // Provisioned-throughput settings are required even though
            // the local test version of DynamoDB ignores them
            createRequest.ProvisionedThroughput = new ProvisionedThroughput(1, 1);

            // Using the DynamoDB client, make a synchronous CreateTable request
            CreateTableResponse createResponse;

            try
            {
                createResponse = client.CreateTable(createRequest);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: failed to create the new table; " + ex.Message);
                PauseForDebugWindow();
                return;
            }

            // Report the status of the new table...
            Console.WriteLine("\n\n Created the \"Movies\" table successfully!\n    Status of the new table: '{0}'", createResponse.TableDescription.TableStatus);
        }
        private static void CreateTable()
        {
            // Attribute definitions
            var attributeDefinitions = new List <AttributeDefinition>()
            {
                { new AttributeDefinition {
                      AttributeName = "IssueId", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "Title", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "CreateDate", AttributeType = "S"
                  } },
                { new AttributeDefinition {
                      AttributeName = "DueDate", AttributeType = "S"
                  } }
            };

            // Key schema for table
            var tableKeySchema = new List <KeySchemaElement>()
            {
                {
                    new KeySchemaElement {
                        AttributeName = "IssueId",
                        KeyType       = "HASH" //Partition key
                    }
                },
                {
                    new KeySchemaElement {
                        AttributeName = "Title",
                        KeyType       = "RANGE" //Sort key
                    }
                }
            };

            // Initial provisioned throughput settings for the indexes
            var ptIndex = new ProvisionedThroughput
            {
                ReadCapacityUnits  = 1L,
                WriteCapacityUnits = 1L
            };

            // CreateDateIndex
            var createDateIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "CreateDateIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "CreateDate", KeyType = "HASH" //Partition key
                    },
                    new KeySchemaElement {
                        AttributeName = "IssueId", KeyType = "RANGE" //Sort key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType   = "INCLUDE",
                    NonKeyAttributes =
                    {
                        "Description", "Status"
                    }
                }
            };

            // TitleIndex
            var titleIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "TitleIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "Title", KeyType = "HASH" //Partition key
                    },
                    new KeySchemaElement {
                        AttributeName = "IssueId", KeyType = "RANGE" //Sort key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = "KEYS_ONLY"
                }
            };

            // DueDateIndex
            var dueDateIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "DueDateIndex",
                ProvisionedThroughput = ptIndex,
                KeySchema             =
                {
                    new KeySchemaElement {
                        AttributeName = "DueDate",
                        KeyType       = "HASH" //Partition key
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = "ALL"
                }
            };



            var createTableRequest = new CreateTableRequest
            {
                TableName             = tableName,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = (long)1,
                    WriteCapacityUnits = (long)1
                },
                AttributeDefinitions   = attributeDefinitions,
                KeySchema              = tableKeySchema,
                GlobalSecondaryIndexes =
                {
                    createDateIndex, titleIndex, dueDateIndex
                }
            };

            Console.WriteLine("Creating table " + tableName + "...");
            client.CreateTable(createTableRequest);

            WaitUntilTableReady(tableName);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            try
            {
                string AccessKey = " *** Enter Access Key Here *** ";
                string SecretKey = " *** Enter Secret Key Here *** ";
                Console.WriteLine("Enter Table Name ");
                string tableName = Console.ReadLine();
                var    client    = new AmazonDynamoDBClient(AccessKey, SecretKey, Amazon.RegionEndpoint.APSouth1);
                Console.WriteLine("Getting list of tables");
                List <string> currentTables = client.ListTables().TableNames;
                int           count         = currentTables.Count;
                for (int i = 0; i < count; i++)
                {
                    Console.WriteLine(currentTables[i]);
                }

                Console.WriteLine("Enter Hash Key ");
                string HashKey = Console.ReadLine();
                Console.WriteLine("Enter Hash Key Type [N -> number, S -> String, B -> Binary]");
                string HashKeyType = Console.ReadLine();

                Console.WriteLine("Enter Range Key");
                string RangeKey = Console.ReadLine();
                Console.WriteLine("Enter Range Key Type [N -> number, S -> String, B -> Binary]");
                string RangeKeyType = Console.ReadLine();

                Console.WriteLine("Enter Read Capacity Unit");
                int ReadCapacity = Convert.ToInt32(Console.ReadLine());

                Console.WriteLine("Enter Write Capacity Unit");
                int WriteCapacity = Convert.ToInt32(Console.ReadLine());

                if (!currentTables.Contains(tableName))
                {
                    var request = new CreateTableRequest
                    {
                        TableName            = tableName,
                        AttributeDefinitions = new List <AttributeDefinition>
                        {
                            new AttributeDefinition
                            {
                                AttributeName = HashKey,
                                AttributeType = HashKeyType
                            },
                            new AttributeDefinition
                            {
                                AttributeName = RangeKey,
                                AttributeType = RangeKeyType
                            }
                        },
                        KeySchema = new List <KeySchemaElement>
                        {
                            new KeySchemaElement
                            {
                                AttributeName = HashKey,
                                KeyType       = "HASH"
                            },
                            new KeySchemaElement
                            {
                                AttributeName = RangeKey,
                                KeyType       = "RANGE"
                            }
                        },
                        ProvisionedThroughput = new ProvisionedThroughput
                        {
                            ReadCapacityUnits  = ReadCapacity,
                            WriteCapacityUnits = WriteCapacity
                        },
                    };
                    var response = client.CreateTable(request);
                    Console.WriteLine("Table created with request ID: " + response.ResponseMetadata.RequestId);
                }
                else
                {
                    Console.WriteLine("Table Already Exists");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR Message: " + e.Message);
            }
            Console.ReadLine();
        }
Exemple #9
0
        private void CreateTableIfNotExists()
        {
            var listTablesRequest = new ListTablesRequest {
                ExclusiveStartTableName = TableName.Substring(0, 6)
            };

            try
            {
                var listTables = _client.ListTables(listTablesRequest);

                if (listTables.TableNames.Contains(TableName))
                {
                    _tableExists = true;
                }
                else
                {
                    var request = new CreateTableRequest()
                    {
                        TableName            = TableName,
                        AttributeDefinitions = new List <AttributeDefinition>()
                        {
                            new AttributeDefinition()
                            {
                                AttributeName = "Id",
                                AttributeType = "S"
                            }
                        },
                        KeySchema = new List <KeySchemaElement>()
                        {
                            new KeySchemaElement()
                            {
                                AttributeName = "Id",
                                KeyType       = "HASH"
                            }
                        },
                        ProvisionedThroughput = new ProvisionedThroughput()
                        {
                            ReadCapacityUnits  = _utilities.DynamoReadCapacityUnits,
                            WriteCapacityUnits = _utilities.DynamoWriteCapacityUnits
                        }
                    };

                    var response = _client.CreateTable(request);

                    if (response.HttpStatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception("Could not create dynamodb table to store checkpoints");
                    }

                    //wait while table is created
                    var tableStatus = response.TableDescription.TableStatus;

                    while (tableStatus != TableStatus.ACTIVE)
                    {
                        var checkTableStatus = _client.DescribeTable(new DescribeTableRequest(TableName));

                        tableStatus = checkTableStatus.Table.TableStatus;

                        Thread.Sleep(500);
                    }

                    Log.Information("The DynamoDB checkpoint table created.");

                    _tableExists = true;
                }
            }
            catch (Exception e)
            {
                Log.Debug(e, "Could not connect to DynamoDB");

                throw;
            }
        }
Exemple #10
0
        private bool CreateDatabase()
        {
            {
                var createTableRequest =
                    new CreateTableRequest()
                {
                    TableName             = tablename,
                    ProvisionedThroughput =
                        new ProvisionedThroughput()
                    {
                        ReadCapacityUnits  = (long)1,
                        WriteCapacityUnits = (long)1
                    }
                };

                var attributeDefinitions = new List <AttributeDefinition>()
                {
                    // Attribute definitions for table primary key
                    { new AttributeDefinition()
                      {
                          AttributeName = "Index", AttributeType = "N"
                      } },
                    { new AttributeDefinition()
                      {
                          AttributeName = "PreviousHash", AttributeType = "S"
                      } },
                    // Attribute definitions for index primary key
                    { new AttributeDefinition()
                      {
                          AttributeName = "Timestamp", AttributeType = "S"
                      } },
                    { new AttributeDefinition()
                      {
                          AttributeName = "Hash", AttributeType = "S"
                      } },
                    { new AttributeDefinition()
                      {
                          AttributeName = "Data", AttributeType = "S"
                      } }
                };

                createTableRequest.AttributeDefinitions = attributeDefinitions;

                // Key schema for table
                var tableKeySchema = new List <KeySchemaElement>()
                {
                    { new KeySchemaElement()
                      {
                          AttributeName = "Hash", KeyType = "HASH"
                      } },                                                              //Partition key
                    { new KeySchemaElement()
                      {
                          AttributeName = "Index", KeyType = "RANGE"
                      } }                                                            //Sort key
                };

                createTableRequest.KeySchema = tableKeySchema;

                var localSecondaryIndexes = new List <LocalSecondaryIndex>();

                // TimestampIndex
                LocalSecondaryIndex TimestampIndex = new LocalSecondaryIndex()
                {
                    IndexName = "TimestampIndex"
                };

                // Key schema for TimestampIndex
                var indexKeySchema = new List <KeySchemaElement>()
                {
                    { new KeySchemaElement()
                      {
                          AttributeName = "Hash", KeyType = "HASH"
                      } },                                                                //Partition key
                    { new KeySchemaElement()
                      {
                          AttributeName = "Timestamp", KeyType = "RANGE"
                      } }                                                                        //Sort key
                };

                TimestampIndex.KeySchema = indexKeySchema;

                // Projection (with list of projected attributes) for
                // TimestampIndex
                var projection = new Projection()
                {
                    ProjectionType = "INCLUDE"
                };

                var nonKeyAttributes = new List <string>()
                {
                    "Data"
                };
                projection.NonKeyAttributes = nonKeyAttributes;

                TimestampIndex.Projection = projection;

                localSecondaryIndexes.Add(TimestampIndex);

                // HashIndex
                LocalSecondaryIndex HashIndex
                    = new LocalSecondaryIndex()
                    {
                    IndexName = "HashIndex"
                    };

                // Key schema for HashIndex
                indexKeySchema = new List <KeySchemaElement>()
                {
                    { new KeySchemaElement()
                      {
                          AttributeName = "Hash", KeyType = "HASH"
                      } },                                                                //Partition key
                    { new KeySchemaElement()
                      {
                          AttributeName = "PreviousHash", KeyType = "RANGE"
                      } }                                                             //Sort key
                };

                // Projection (all attributes) for HashIndex
                projection = new Projection()
                {
                    ProjectionType = "ALL"
                };

                HashIndex.KeySchema  = indexKeySchema;
                HashIndex.Projection = projection;

                localSecondaryIndexes.Add(HashIndex);

                // DataIndex
                LocalSecondaryIndex DataIndex
                    = new LocalSecondaryIndex()
                    {
                    IndexName = "DataIndex"
                    };

                // Key schema for DataIndex
                indexKeySchema = new List <KeySchemaElement>()
                {
                    { new KeySchemaElement()
                      {
                          AttributeName = "Hash", KeyType = "HASH"
                      } },                                                                //Partition key
                    { new KeySchemaElement()
                      {
                          AttributeName = "Data", KeyType = "RANGE"
                      } }                                                             //Sort key
                };

                // Projection (all attributes) for DataIndex
                projection = new Projection()
                {
                    ProjectionType = "ALL"
                };

                DataIndex.KeySchema  = indexKeySchema;
                DataIndex.Projection = projection;

                localSecondaryIndexes.Add(DataIndex);
                // Add index definitions to CreateTable request
                createTableRequest.LocalSecondaryIndexes = localSecondaryIndexes;

                Console.WriteLine("Creating table " + tablename + "...");
                client.CreateTable(createTableRequest);
                //WaitUntilTableReady(tablename);
            }
            WaitUntilTableReady(tablename);

            Table.TryLoadTable(client, tablename, out blockchain);
            CreateGenesisBlock();

            return(true);
        }
        public void Dbservice()
        {
            try
            {
                AmazonDynamoDBConfig clientConfig = new AmazonDynamoDBConfig();
                // Set the endpoint URL
                clientConfig.ServiceURL = "http://*****:*****@test.com",
                    HomePhone = "000000000",
                    WorkPhone = "00000000000",
                    //    Products = new Dictionary<string, string>
                    //   {
                    // "test", new
                    //   }
                };

                //DbContext.Save<DemoTest>(data);
                var loginTab = DbContext.Load <Login>("*****@*****.**");

                var rs       = DbContext.Query <Organizations>("ythy", QueryOperator.GreaterThan, 0);
                var Qrequest = new QueryRequest
                {
                    TableName = "DemoTest",
                    KeyConditionExpression    = "Title = :v_Title and ReleaseYear = :v_ReleaseYear",
                    ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                        { ":v_Title", new AttributeValue {
                              S = "t1"
                          } },
                        { ":v_ReleaseYear", new AttributeValue {
                              N = "51"
                          } }
                    },

                    // Optional parameter.
                    // ProjectionExpression = "Id, ReplyDateTime, PostedBy",
                    // Optional parameter.
                    ConsistentRead = true
                };
                // var Qresponse = DbContext.Query<DemoTest>("ythy", QueryOperator.Equal, 53);
                //foreach (Dictionary<string, AttributeValue> item in Qresponse.ToList())
                //{
                //}
                // var response = DbContext.Load<Organizations>(new Organizations() { ReleaseYear = 50, Title = "ythy" });

                // DbContext.Save<DemoTest>(data);//, new DynamoDBOperationConfig() { OverrideTableName = "DemoTest", SkipVersionCheck = true });
                // TestCRUDOperations(DbContext, client);
                //TestCRUDOperations(context);
                Console.WriteLine("To continue, press Enter");
            }
            catch (AmazonDynamoDBException e) { Console.WriteLine(e.Message); }
            catch (AmazonServiceException e) { Console.WriteLine(e.Message); }
            catch (Exception e) { Console.WriteLine(e.Message); }
        }
        private void CreateMemoriesTable(AmazonDynamoDBClient client)
        {
            var provisionedThroughput = new ProvisionedThroughput(1, 1);

            var attributeDefinition = new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "Id",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "BabyId",
                    AttributeType = ScalarAttributeType.S
                },
                new AttributeDefinition
                {
                    AttributeName = "Description",
                    AttributeType = ScalarAttributeType.S
                }
            };

            var keySchema = new List <KeySchemaElement>()
            {
                new KeySchemaElement
                {
                    AttributeName = "Id",
                    KeyType       = KeyType.HASH
                }
            };

            var babyIdIndex = new GlobalSecondaryIndex()
            {
                IndexName             = "BabyIdIndex",
                ProvisionedThroughput = provisionedThroughput,
                Projection            = new Projection {
                    ProjectionType = ProjectionType.ALL
                },
                KeySchema =
                {
                    new KeySchemaElement
                    {
                        AttributeName = "BabyId",
                        KeyType       = KeyType.HASH
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "Description",
                        KeyType       = KeyType.RANGE
                    }
                }
            };


            // Build a 'CreateTableRequest' for the new table

            var createRequest = new CreateTableRequest
            {
                TableName              = BabyMemoryConstants.MemoriesTableName,
                AttributeDefinitions   = attributeDefinition,
                ProvisionedThroughput  = provisionedThroughput,
                KeySchema              = keySchema,
                GlobalSecondaryIndexes = { babyIdIndex }
            };

            try
            {
                var createResponse = client.CreateTable(createRequest);
                Console.WriteLine("\n Table Created " + createResponse.TableDescription);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n Error: failed to create the new table; " + ex.Message);
            }
        }