Ejemplo n.º 1
0
        public override bool EnsureCreated([NotNull] IModel model)
        {
            Check.NotNull(model, "model");

            var created = false;
            foreach (var type in model.EntityTypes)
            {
                var request = new CreateTableRequest(new AtsTable(type.TableName()));
                created |= _connection.ExecuteRequest(request);
            }
            return created;
        }
Ejemplo n.º 2
0
 public bool CreateTableAndReadyForUse(CreateTableRequest request)
 {
     var status = client.CreateTable(request).CreateTableResult.TableDescription.TableStatus;
     var count = 0;
     while (!status.Equals("ACTIVE", StringComparison.InvariantCultureIgnoreCase) && count < UpdateStatusRepeatCount)
     {
         Thread.Sleep(UpdateStatusRepeatTimeOut);
         status = client.DescribeTable(new DescribeTableRequest { TableName = request.TableName }).DescribeTableResult.Table.TableStatus;
         count++;
     }
     return (status.Equals("ACTIVE", StringComparison.InvariantCultureIgnoreCase));
 }
        private ExecutionContext CreateExecutionContext(EndpointDiscoveryTestClient client, AmazonDynamoDBConfig config, bool required, SortedDictionary <string, string> identifiers)
        {
            var request = new CreateTableRequest();
            var options = new InvokeOptions();

            options.RequestMarshaller           = new CreateTableRequestMarshaller();
            options.EndpointDiscoveryMarshaller = new TestEndpointDiscoveryMarshaller(required, identifiers);
            client.SetOptionsEndpointOperation(options);
            var credentials = new ImmutableCredentials(AWS_ACCESS_KEY_ID, "test", "test");

            var executionContext = new ExecutionContext(
                new RequestContext(true, new NullSigner())
            {
                ClientConfig         = config,
                Request              = options.RequestMarshaller.Marshall(request),
                ImmutableCredentials = credentials,
                OriginalRequest      = request,
                Options              = options
            },
                new ResponseContext()
                );

            return(executionContext);
        }
Ejemplo n.º 4
0
        private async Task CreateEventTable()
        {
            var slugIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_slug",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_slug",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_time",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.KEYS_ONLY
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var processedIndex = new GlobalSecondaryIndex
            {
                IndexName = "ix_not_processed",
                KeySchema = new List <KeySchemaElement>
                {
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "not_processed",
                            KeyType       = "HASH" //Partition key
                        }
                    },
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "event_time",
                            KeyType       = "RANGE" //Sort key
                        }
                    }
                },
                Projection = new Projection
                {
                    ProjectionType = ProjectionType.KEYS_ONLY
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            var createRequest = new CreateTableRequest($"{_tablePrefix}-{DynamoPersistenceProvider.EVENT_TABLE}", new List <KeySchemaElement>
            {
                new KeySchemaElement("id", KeyType.HASH)
            })
            {
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition("id", ScalarAttributeType.S),
                    new AttributeDefinition("not_processed", ScalarAttributeType.N),
                    new AttributeDefinition("event_slug", ScalarAttributeType.S),
                    new AttributeDefinition("event_time", ScalarAttributeType.N)
                },
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    slugIndex,
                    processedIndex
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                }
            };

            await CreateTable(createRequest);
        }
Ejemplo n.º 5
0
        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 void PopulateRequestFromSchemaObject(TableSchema schemaObject, CreateTableRequest request)
        {
            if (!schemaObject.HasDefinedKeys)
            {
                ThrowArgumentError("Key schema not defined on input object.", Schema);
            }
            if (!schemaObject.HasDefinedAttributes)
            {
                ThrowArgumentError("Attribute schema containing keys not defined on input object.", Schema);
            }

            request.KeySchema            = schemaObject.KeySchema;
            request.AttributeDefinitions = schemaObject.AttributeSchema;

            // optional
            if (schemaObject.HasDefinedLocalSecondaryIndices)
            {
                // reconstruct each lsi so that the schema builder cmdlets can opt to not
                // set the primary hash key element, thus enabling re-use on multiple
                // table creation pipelines
                var requestIndices = new List <LocalSecondaryIndex>();
                foreach (var definedLSI in schemaObject.LocalSecondaryIndexSchema)
                {
                    var lsi = new LocalSecondaryIndex
                    {
                        IndexName = definedLSI.IndexName,
                        KeySchema = new List <KeySchemaElement>()
                    };
                    var hashKey = definedLSI.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                    if (hashKey == null)
                    {
                        hashKey = schemaObject.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                        if (hashKey == null)
                        {
                            throw new ArgumentException(string.Format("Unable to determine primary hash key from supplied schema to use in local secondary index {0}", definedLSI.IndexName));
                        }

                        // DynamoDB requires the hash key be first in the collection
                        lsi.KeySchema.Add(hashKey);
                        lsi.KeySchema.AddRange(definedLSI.KeySchema);
                    }
                    else
                    {
                        // primary hash already set, possibly from earlier setup so just re-use
                        lsi.KeySchema.AddRange(definedLSI.KeySchema);
                    }

                    if (definedLSI.Projection != null)
                    {
                        lsi.Projection = new Projection
                        {
                            ProjectionType   = definedLSI.Projection.ProjectionType,
                            NonKeyAttributes = definedLSI.Projection.NonKeyAttributes
                        };
                    }

                    requestIndices.Add(lsi);
                }

                request.LocalSecondaryIndexes = requestIndices;
            }

            // optional
            if (schemaObject.HasDefinedGlobalSecondaryIndices)
            {
                // reconstruct each lsi so that the schema builder cmdlets can opt to not
                // set the primary hash key element, thus enabling re-use on multiple
                // table creation pipelines
                var requestIndices = new List <GlobalSecondaryIndex>();
                foreach (var definedGSI in schemaObject.GlobalSecondaryIndexSchema)
                {
                    var gsi = new GlobalSecondaryIndex
                    {
                        IndexName             = definedGSI.IndexName,
                        KeySchema             = new List <KeySchemaElement>(),
                        ProvisionedThroughput = new ProvisionedThroughput
                        {
                            ReadCapacityUnits  = definedGSI.ProvisionedThroughput.ReadCapacityUnits,
                            WriteCapacityUnits = definedGSI.ProvisionedThroughput.WriteCapacityUnits
                        }
                    };

                    var hashKey = definedGSI.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                    if (hashKey == null)
                    {
                        hashKey = schemaObject.KeySchema.FirstOrDefault(k => k.KeyType.Equals(KeyType.HASH));
                        if (hashKey == null)
                        {
                            throw new ArgumentException(string.Format("Unable to determine primary hash key from supplied schema to use in global secondary index {0}", definedGSI.IndexName));
                        }

                        // DynamoDB requires the hash key be first in the collection
                        gsi.KeySchema.Add(hashKey);
                        gsi.KeySchema.AddRange(definedGSI.KeySchema);
                    }
                    else
                    {
                        // primary hash already set, possibly from earlier setup so just re-use
                        gsi.KeySchema.AddRange(definedGSI.KeySchema);
                    }

                    if (definedGSI.Projection != null)
                    {
                        gsi.Projection = new Projection
                        {
                            ProjectionType   = definedGSI.Projection.ProjectionType,
                            NonKeyAttributes = definedGSI.Projection.NonKeyAttributes
                        };
                    }

                    requestIndices.Add(gsi);
                }

                request.GlobalSecondaryIndexes = requestIndices;
            }
        }
 public async Task<CreateTableResponse> CreateTableAsync(string tableName, MetadataPreference? metadataPreference = null)
 {
     var request = new CreateTableRequest(_account, tableName, metadataPreference);
     var response = await request.ExecuteAsync();
     return response.Payload;
 }
        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);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Create a table async.
 /// </summary>
 /// <param name="request">The create table request.</param>
 /// <returns>The async task.</returns>
 public async Task <CreateTableResponse> CreateTableAsync(CreateTableRequest request)
 {
     return(await _client.CreateTableAsync(request));
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Build a table from a create table request
        /// We filter out any attributes that are not B, S, or N because DynamoDB does not currently support
        /// creation of these types via the API. As these are not used for hash or range keys they are not
        /// required to store items in DynamoDB
        /// </summary>
        /// <param name="createTableRequest">The request to build tables from</param>
        /// <param name="ct"></param>
        /// <returns>The response to table creation</returns>
        public async Task <CreateTableResponse> Build(CreateTableRequest createTableRequest, CancellationToken ct = default(CancellationToken))
        {
            var modifiedTableRequest = RemoveNonSchemaAttributes(createTableRequest);

            return(await _client.CreateTableAsync(modifiedTableRequest, ct));
        }
Ejemplo n.º 11
0
        private void CreateLSITable()
        {
            #region CreateTable LSI 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 local secondary indexes:
            //  Table has two indexes, one on "Year" and the other on "Setting"
            List <LocalSecondaryIndex> indexes = new List <LocalSecondaryIndex>
            {
                new LocalSecondaryIndex
                {
                    IndexName = "YearsIndex",
                    KeySchema = new List <KeySchemaElement>
                    {
                        // Hash key must match table hash key
                        new KeySchemaElement {
                            AttributeName = "Author", KeyType = "HASH"
                        },
                        // Secondary index on "Year" attribute
                        new KeySchemaElement {
                            AttributeName = "Year", KeyType = "RANGE"
                        }
                    },
                    // Projection type is set to ALL, all attributes returned for this index
                    Projection = new Projection
                    {
                        ProjectionType = "ALL"
                    }
                },
                new LocalSecondaryIndex
                {
                    IndexName = "SettingsIndex",
                    KeySchema = new List <KeySchemaElement>
                    {
                        // Hash key must match table hash key
                        new KeySchemaElement {
                            AttributeName = "Author", KeyType = "HASH"
                        },
                        // Secondary index on "Setting" attribute
                        new KeySchemaElement {
                            AttributeName = "Setting", KeyType = "RANGE"
                        }
                    },
                    // Projection type is set to INCLUDE, the specified attributes + keys are returned
                    Projection = new Projection
                    {
                        ProjectionType   = "INCLUDE",
                        NonKeyAttributes = new List <string>
                        {
                            "Pages", "Genres"
                        }
                    }
                }
            };

            // Define key attributes:
            //  The key attributes "Author" and "Title" are string types.
            //  The local secondary index attributes are "Year" (numerical) and "Setting" (string).
            List <AttributeDefinition> definitions = new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "Author", AttributeType = "S"
                },
                new AttributeDefinition
                {
                    AttributeName = "Title", AttributeType = "S"
                },
                new AttributeDefinition
                {
                    AttributeName = "Year", AttributeType = "N"
                },
                new AttributeDefinition
                {
                    AttributeName = "Setting", 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,
                LocalSecondaryIndexes = indexes
            };

            // 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
        }
Ejemplo n.º 12
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
            }
        }
Ejemplo n.º 13
0
        public void Test6GlobalSeconaryIndex()
        {
            var tableName = "GameHistory";

            DeleteTable(tableName);

            var gsiSeaonalKeyPlayer = new GlobalSecondaryIndex {
                IndexName             = "SeasonalKeyPlayerIndex",
                ProvisionedThroughput = new ProvisionedThroughput {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 1
                },
                Projection = new Projection {
                    ProjectionType = "ALL"
                },
                KeySchema = new List <KeySchemaElement> {
                    { new KeySchemaElement {
                          AttributeName = "SeasonId", KeyType = "HASH"
                      } },
                    { new KeySchemaElement {
                          AttributeName = "KeyPlayerId", KeyType = "RANGE"
                      } }
                }
            };

            var createTableRequest = new CreateTableRequest {
                TableName = tableName,
                KeySchema = new List <KeySchemaElement> {
                    new KeySchemaElement("GameId", KeyType.HASH),
                    new KeySchemaElement("GameType", KeyType.RANGE)
                },
                AttributeDefinitions = new List <AttributeDefinition> {
                    new AttributeDefinition("GameId", ScalarAttributeType.N),
                    new AttributeDefinition("GameType", ScalarAttributeType.N),
                    new AttributeDefinition("SeasonId", ScalarAttributeType.N),
                    new AttributeDefinition("KeyPlayerId", ScalarAttributeType.N)
                },
                ProvisionedThroughput  = new ProvisionedThroughput(1, 1),
                GlobalSecondaryIndexes = { gsiSeaonalKeyPlayer }
            };

            var response = Client.CreateTable(createTableRequest);

            Assert.AreEqual(response.HttpStatusCode, HttpStatusCode.OK);

            var queryRequest = new QueryRequest {
                TableName = tableName,
                IndexName = gsiSeaonalKeyPlayer.IndexName,
                KeyConditionExpression    = "SeasonId = :season_id and KeyPlayerId = :player_id",
                ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                    { ":season_id", new AttributeValue {
                          N = "1"
                      } },
                    { ":player_id", new AttributeValue {
                          N = "1234"
                      } }
                },
                ScanIndexForward = true
            };

            var queryResponse = Client.Query(queryRequest);
        }
        private Table CreateTable()
        {
            CreateTableRequest createRequest = new CreateTableRequest()
                .WithTableName(this._tableName)
                .WithKeySchema(new KeySchema()
                    .WithHashKeyElement(new KeySchemaElement()
                        .WithAttributeName(ATTRIBUTE_SESSION_ID)
                        .WithAttributeType("S")))
                .WithProvisionedThroughput(new ProvisionedThroughput()
                    .WithReadCapacityUnits(this._initialReadUnits)
                    .WithWriteCapacityUnits(this._initialWriteUnits));
            createRequest.BeforeRequestEvent += this.UserAgentRequestEventHandler;

            CreateTableResponse response = this._ddbClient.CreateTable(createRequest);

            DescribeTableRequest descRequest = new DescribeTableRequest()
                .WithTableName(this._tableName);
            descRequest.BeforeRequestEvent += this.UserAgentRequestEventHandler;

            // Wait till table is active
            bool isActive = false;
            while (!isActive)
            {
                Thread.Sleep(DESCRIBE_INTERVAL);
                DescribeTableResponse descResponse = this._ddbClient.DescribeTable(descRequest);
                string tableStatus = descResponse.DescribeTableResult.Table.TableStatus;

                if (string.Equals(tableStatus, ACTIVE_STATUS, StringComparison.InvariantCultureIgnoreCase))
                    isActive = true;
            }

            Table table = Table.LoadTable(this._ddbClient, this._tableName, Table.DynamoDBConsumer.SessionStateProvider);
            return table;
        }
Ejemplo n.º 15
0
 public CreateTableResponse CreateTable(CreateTableRequest request)
 {
     return client.CreateTable(request);
 }
        public void TestAPIWithParameter(string apiName)
        {
            var tableName                  = TestContext.tableName;
            var pkSchema                   = TestContext.pkSchema;
            var reservedThroughput         = TestContext.reservedThroughput;
            var primaryKey                 = TestContext.primaryKey;
            var attribute                  = TestContext.attribute;
            var condition                  = TestContext.condition;
            var startPrimaryKey            = TestContext.startPrimaryKey;
            var endPrimaryKey              = TestContext.endPrimaryKey;
            var putRowConsumed             = TestContext.putRowConsumed;
            var getRowConsumed             = TestContext.getRowConsumed;
            var updateRowConsumed          = TestContext.updateRowConsumed;
            var deleteRowConsumed          = TestContext.deleteRowConsumed;
            var getRangeConsumed           = TestContext.getRangeConsumed;
            var updateOfAttributeForPut    = TestContext.updateOfAttributeForPut;
            var updateOfAttributeForDelete = TestContext.updateOfAttributeForDelete;
            var columnsToGet               = TestContext.columnsToGet;
            var limit     = TestContext.limit;
            var direction = TestContext.direction;

            var tableMeta = new TableMeta(tableName, pkSchema);

            switch (apiName)
            {
            case "CreateTable":
                var request0 = new CreateTableRequest(tableMeta, reservedThroughput);
                OTSClient.CreateTable(request0);
                return;

            case "ListTable":
                var request1  = new ListTableRequest();
                var response1 = OTSClient.ListTable(request1);
                Assert.AreEqual(new List <string>()
                {
                    tableName
                }, response1.TableNames);
                return;

            case "UpdateTable":
                var request2  = new UpdateTableRequest(tableName, reservedThroughput);
                var response2 = OTSClient.UpdateTable(request2);

                if (reservedThroughput.Read.HasValue && reservedThroughput.Write.HasValue)
                {
                    AssertCapacityUnit(
                        reservedThroughput,
                        response2.ReservedThroughputDetails.CapacityUnit);
                }
                Assert.IsTrue(response2.ReservedThroughputDetails.LastDecreaseTime >= 0);
                Assert.IsTrue(response2.ReservedThroughputDetails.LastIncreaseTime >= 0);
                Assert.IsTrue(response2.ReservedThroughputDetails.NumberOfDecreasesToday >= 0);
                return;

            case "DeleteTable":
                var request3 = new DeleteTableRequest(tableName);
                OTSClient.DeleteTable(request3);

                var request31  = new ListTableRequest();
                var response31 = OTSClient.ListTable(request31);
                Assert.AreEqual(new List <string>()
                {
                }, response31.TableNames);
                return;

            case "DescribeTable":
                var request4  = new DescribeTableRequest(tableName);
                var response4 = OTSClient.DescribeTable(request4);
                Assert.AreEqual(tableName, response4.TableMeta.TableName);
                AssertPrimaryKeySchema(pkSchema, response4.TableMeta.PrimaryKeySchema);
                AssertCapacityUnit(reservedThroughput, response4.ReservedThroughputDetails.CapacityUnit);
                Assert.IsTrue(response4.ReservedThroughputDetails.LastDecreaseTime >= 0);
                Assert.IsTrue(response4.ReservedThroughputDetails.LastIncreaseTime >= 0);
                Assert.IsTrue(response4.ReservedThroughputDetails.NumberOfDecreasesToday >= 0);
                return;

            case "PutRow":
                var request5  = new PutRowRequest(tableName, condition, primaryKey, attribute);
                var response5 = OTSClient.PutRow(request5);
                AssertCapacityUnit(putRowConsumed, response5.ConsumedCapacityUnit);
                return;

            case "GetRow":
                var request6  = new GetRowRequest(tableName, primaryKey, columnsToGet);
                var response6 = OTSClient.GetRow(request6);
                AssertPrimaryKey(primaryKey, response6.PrimaryKey, columnsToGet);
                AssertAttribute(attribute, response6.Attribute, columnsToGet);
                AssertCapacityUnit(getRowConsumed, response6.ConsumedCapacityUnit);
                return;

            case "DeleteRow":
                var request7  = new DeleteRowRequest(tableName, condition, primaryKey);
                var response7 = OTSClient.DeleteRow(request7);
                AssertCapacityUnit(deleteRowConsumed, response7.ConsumedCapacityUnit);

                var request71  = new GetRowRequest(tableName, primaryKey);
                var response71 = OTSClient.GetRow(request71);
                AssertPrimaryKey(new PrimaryKey(), response71.PrimaryKey);
                AssertAttribute(new AttributeColumns(), response71.Attribute);
                return;

            case "UpdateRow_Put":
                var request8  = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForPut);
                var response8 = OTSClient.UpdateRow(request8);
                AssertCapacityUnit(updateRowConsumed, response8.ConsumedCapacityUnit);

                var request81  = new GetRowRequest(tableName, primaryKey);
                var response81 = OTSClient.GetRow(request81);
                AssertPrimaryKey(primaryKey, response81.PrimaryKey);
                AssertAttribute(attribute, response81.Attribute);
                AssertCapacityUnit(getRowConsumed, response81.ConsumedCapacityUnit);

                return;

            case "UpdateRow_Delete":
                var request9  = new UpdateRowRequest(tableName, condition, primaryKey, updateOfAttributeForDelete);
                var response9 = OTSClient.UpdateRow(request9);
                AssertCapacityUnit(deleteRowConsumed, response9.ConsumedCapacityUnit);

                var request91  = new GetRowRequest(tableName, primaryKey);
                var response91 = OTSClient.GetRow(request91);
                // Don't assert primary key
                AssertAttribute(new AttributeColumns(), response91.Attribute);
                return;

            case "BatchGetRow":
                var request11 = new BatchGetRowRequest();
                request11.Add(tableName, new List <PrimaryKey>()
                {
                    primaryKey
                }, columnsToGet);
                var response11 = OTSClient.BatchGetRow(request11);
                Assert.AreEqual(1, response11.RowDataGroupByTable.Count);
                Assert.IsTrue(response11.RowDataGroupByTable.ContainsKey(tableName));
                Assert.AreEqual(1, response11.RowDataGroupByTable[tableName].Count);

                if (!response11.RowDataGroupByTable[tableName][0].IsOK)
                {
                    throw new OTSServerException(apiName, HttpStatusCode.OK,
                                                 response11.RowDataGroupByTable[tableName][0].ErrorCode,
                                                 response11.RowDataGroupByTable[tableName][0].ErrorMessage);
                }
                AssertPrimaryKey(primaryKey, response11.RowDataGroupByTable[tableName][0].PrimaryKey);
                AssertAttribute(attribute, response11.RowDataGroupByTable[tableName][0].Attribute);
                AssertCapacityUnit(getRowConsumed, response11.RowDataGroupByTable[tableName][0].Consumed);
                return;

            case "BatchWriteRow_Put":
                var request12  = new BatchWriteRowRequest();
                var rowChanges = new RowChanges();
                rowChanges.AddPut(condition, primaryKey, attribute);
                request12.Add(tableName, rowChanges);
                var response12 = OTSClient.BatchWriteRow(request12);
                Assert.AreEqual(1, response12.TableRespones.Count);
                Assert.IsTrue(response12.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(1, response12.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(0, response12.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(0, response12.TableRespones[tableName].DeleteResponses.Count);
                if (response12.TableRespones[tableName].PutResponses[0].IsOK)
                {
                    AssertCapacityUnit(putRowConsumed,
                                       response12.TableRespones[tableName].PutResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response12.TableRespones[tableName].PutResponses[0].ErrorCode,
                                                 response12.TableRespones[tableName].PutResponses[0].ErrorMessage);
                }


                var request121  = new GetRowRequest(tableName, primaryKey);
                var response121 = OTSClient.GetRow(request121);
                AssertPrimaryKey(primaryKey, response121.PrimaryKey);
                AssertAttribute(attribute, response121.Attribute);
                AssertCapacityUnit(getRowConsumed, response121.ConsumedCapacityUnit);
                return;

            case "BatchWriteRow_Update":
                var request13   = new BatchWriteRowRequest();
                var rowChanges2 = new RowChanges();
                rowChanges2.AddUpdate(condition, primaryKey, updateOfAttributeForPut);
                request13.Add(tableName, rowChanges2);
                var response13 = OTSClient.BatchWriteRow(request13);
                Assert.AreEqual(1, response13.TableRespones.Count);
                Assert.IsTrue(response13.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(0, response13.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(1, response13.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(0, response13.TableRespones[tableName].DeleteResponses.Count);
                if (response13.TableRespones[tableName].UpdateResponses[0].IsOK)
                {
                    AssertCapacityUnit(updateRowConsumed,
                                       response13.TableRespones[tableName].UpdateResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response13.TableRespones[tableName].UpdateResponses[0].ErrorCode,
                                                 response13.TableRespones[tableName].UpdateResponses[0].ErrorMessage);
                }

                var request131  = new GetRowRequest(tableName, primaryKey);
                var response131 = OTSClient.GetRow(request131);
                AssertPrimaryKey(primaryKey, response131.PrimaryKey);
                AssertAttribute(attribute, response131.Attribute);
                AssertCapacityUnit(getRowConsumed, response131.ConsumedCapacityUnit);
                return;

            case "BatchWriteRow_Delete":
                var request14   = new BatchWriteRowRequest();
                var rowChanges3 = new RowChanges();
                rowChanges3.AddDelete(condition, primaryKey);
                request14.Add(tableName, rowChanges3);
                var response14 = OTSClient.BatchWriteRow(request14);
                Assert.AreEqual(1, response14.TableRespones.Count);
                Assert.IsTrue(response14.TableRespones.ContainsKey(tableName));
                Assert.AreEqual(0, response14.TableRespones[tableName].PutResponses.Count);
                Assert.AreEqual(0, response14.TableRespones[tableName].UpdateResponses.Count);
                Assert.AreEqual(1, response14.TableRespones[tableName].DeleteResponses.Count);

                if (response14.TableRespones[tableName].DeleteResponses[0].IsOK)
                {
                    AssertCapacityUnit(deleteRowConsumed,
                                       response14.TableRespones[tableName].DeleteResponses[0].Consumed);
                }
                else
                {
                    throw new OTSServerException("/BatchWriteRow", HttpStatusCode.OK,
                                                 response14.TableRespones[tableName].DeleteResponses[0].ErrorCode,
                                                 response14.TableRespones[tableName].DeleteResponses[0].ErrorMessage);
                }
                var request141  = new GetRowRequest(tableName, primaryKey);
                var response141 = OTSClient.GetRow(request141);
                AssertPrimaryKey(new PrimaryKey(), response141.PrimaryKey);
                AssertAttribute(new AttributeColumns(), response141.Attribute);
                return;

            case "GetRange":
                var request15 = new GetRangeRequest(tableName, direction,
                                                    startPrimaryKey, endPrimaryKey,
                                                    columnsToGet, limit);
                var response15 = OTSClient.GetRange(request15);
                Assert.AreEqual(1, response15.RowDataList.Count);
                Assert.AreEqual(null, response15.NextPrimaryKey);
                AssertCapacityUnit(getRangeConsumed, response15.ConsumedCapacityUnit);
                AssertPrimaryKey(primaryKey, response15.RowDataList[0].PrimaryKey, columnsToGet);
                AssertAttribute(attribute, response15.RowDataList[0].Attribute, columnsToGet);
                return;

            default:
                throw new Exception(String.Format("invalid api name: {0}", apiName));
            }
        }
Ejemplo n.º 17
0
        private async Task CreateTable()
        {
            _logger.LogInformation("Creating Table");

            try
            {
                var request = new CreateTableRequest
                {
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "id",
                            AttributeType = ScalarAttributeType.S
                        },
                        new AttributeDefinition
                        {
                            AttributeName = "csvType",
                            AttributeType = ScalarAttributeType.S
                        },
                        new AttributeDefinition
                        {
                            AttributeName = "csvLocation",
                            AttributeType = ScalarAttributeType.S
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "id",
                            KeyType       = KeyType.HASH
                        },
                        new KeySchemaElement
                        {
                            AttributeName = "csvType",
                            KeyType       = KeyType.RANGE
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 5,
                        WriteCapacityUnits = 5
                    },
                    TableName = _config.TableName,
                    GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                    {
                        new GlobalSecondaryIndex
                        {
                            IndexName = "latest-result",
                            KeySchema = new List <KeySchemaElement>
                            {
                                new KeySchemaElement("csvLocation", KeyType.HASH),
                                new KeySchemaElement("csvType", KeyType.RANGE)
                            },
                            Projection = new Projection {
                                ProjectionType = ProjectionType.ALL
                            },
                            ProvisionedThroughput = new ProvisionedThroughput(5, 5)
                        }
                    }
                };

                var response = await _dynamoDb.CreateTableAsync(request);
                await WaitUntilTableReady(_config.TableName);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Create table error");
            }
        }
Ejemplo n.º 18
0
        public async Task <string> CreateTableAsync(
            Type contextType, Type entityType, string tableName)
        {
            InputValidator.ThrowIfAnyNullOrWhitespace(entityType, tableName);

            var dynamoDbTableAttribute = entityType.GetCustomAttribute <DynamoDBTableAttribute>(true);

            tableName = dynamoDbTableAttribute?.TableName ?? tableName;
            var hashKeyMember = entityType
                                .GetProperties()
                                .SingleOrDefault(pi => pi.GetCustomAttributes()
                                                 .Any(attr => attr.GetType() == typeof(DynamoDBHashKeyAttribute)));
            var hashKeyMemberAttribute = entityType
                                         .GetProperty(hashKeyMember?.Name ?? string.Empty)
                                         ?.GetCustomAttribute <DynamoDBHashKeyAttribute>(true);
            var hashKeyMemberType = entityType
                                    .GetProperty(hashKeyMember?.Name ?? string.Empty)
                                    ?.PropertyType;
            var entityConfigRequired = hashKeyMember == null;
            var entityConfig         = this.entityConfigurationProvider
                                       .TryGetEntityConfiguration(contextType, entityType);

            if (entityConfigRequired && entityConfig == null)
            {
                throw new DynamoContextConfigurationException(string.Format(
                                                                  ExceptionMessage.EntityConfigurationNotFound, entityType.FullName));
            }

            var hashKeyMemberName = entityConfig?.HashKeyMemberName
                                    ?? hashKeyMemberAttribute.AttributeName
                                    ?? hashKeyMember.Name;
            var hashKeyMemberAttributeType = new ScalarAttributeType(
                hashKeyMemberType != null
                ? Constants.AttributeTypesMap[hashKeyMemberType]
                : Constants.AttributeTypesMap[entityConfig.HashKeyMemberType]);
            var readCapacityUnits = entityConfig?.ReadCapacityUnits
                                    ?? Constants.DefaultReadCapacityUnits;
            var writeCapacityUnits = entityConfig?.WriteCapacityUnits
                                     ?? Constants.DefaultWriteCapacityUnits;
            var gsisConfiguration = (entityConfig?.Indexes?.Count ?? 0) > 0
                ? entityConfig.Indexes
                : this.indexConfigurationFactory.CreateIndexConfigByAttributes(entityType);

            var request = new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = this.attributeDefinitionFactory.CreateAttributeDefinitions(
                    hashKeyMemberName, hashKeyMemberAttributeType, gsisConfiguration).ToList(),
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement(hashKeyMemberName, KeyType.HASH)
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = readCapacityUnits,
                    WriteCapacityUnits = writeCapacityUnits
                },
                GlobalSecondaryIndexes = gsisConfiguration.Count() == 0
                ? null
                : this.indexFactory.CreateRequestIndexes(gsisConfiguration).ToList()
            };

            try
            {
                var response = await this.client.CreateTableAsync(request);

                if (!response.HttpStatusCode.IsSuccessful())
                {
                    throw new CreateTableFailedException(
                              response.ResponseMetadata.Metadata.JoinByNewLine());
                }

                return(request.TableName);
            }
            catch (Exception ex)
            {
                throw new CreateTableFailedException("Failed to create a table.", ex);
            }
        }
Ejemplo n.º 19
0
        public async Task TestSourceFunctionAsyncRSMapIssue()
        {
            // Invoke the lambda function and confirm the string was upper cased.
            AmazonDynamoDBClient    client = new AmazonDynamoDBClient(RegionEndpoint.USEast1);
            List <KeySchemaElement> schema = new List <KeySchemaElement>
            {
                new KeySchemaElement
                {
                    AttributeName = "guid", KeyType = "HASH"
                },
                new KeySchemaElement
                {
                    AttributeName = "Data", KeyType = "RANGE"
                }
            };

            // Define key attributes:
            //  The key attributes "Author" and "Title" are string types
            List <AttributeDefinition> definitions = new List <AttributeDefinition>
            {
                new AttributeDefinition
                {
                    AttributeName = "guid", AttributeType = "S"
                },
                new AttributeDefinition
                {
                    AttributeName = "Data", 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             = "MDSSourceDynamoDBTestDat1",
                KeySchema             = schema,
                ProvisionedThroughput = throughput,
                AttributeDefinitions  = definitions
            };


            // View new table properties
            var tableDescription = await client.CreateTableAsync(request);

            Console.WriteLine("Table name: {0}", tableDescription.TableDescription.TableName);
            Console.WriteLine("Creation time: {0}", tableDescription.TableDescription.CreationDateTime);
            Console.WriteLine("Item count: {0}", tableDescription.TableDescription.ItemCount);
            Console.WriteLine("Table size (bytes): {0}", tableDescription.TableDescription.TableSizeBytes);
            Console.WriteLine("Table status: {0}", tableDescription.TableDescription.TableStatus);
            //dbList.Add()

            Table  Catolog = Table.LoadTable(client, tableDescription.TableDescription.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 = await client.DescribeTableAsync(new DescribeTableRequest
                    {
                        TableName = "MDSSourceDynamoDBTestDat1"
                    });

                    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");
            Console.WriteLine("\n*** listing tables ***");

            IAmazonS3 s3Client              = new AmazonS3Client(RegionEndpoint.USEast1);
            string    inputData             = "#BUSINESS_DATE|CURRENCY|CREDIT_TYPE|RATINGS|TODAY|DAY_1|DAYS_7|DAYS_30|DAYS_90|DAYS_365|YEARS_2|YEARS_3|LOAD_DATE\n20190828|USD|CORPS|AAA|17.506131099768|17.499652220154|17.689977433049|17.612596564917|17.531741482981|16.721663421274|22.812501511208|28.591981044712|08/29/2019";
            var       destinationBucketName = "spgmi-dest-buck-test";
            var       bucketName            = "spgi-mds-data-dev-test2".ToLower();
            var       key = "MDR_EQUITY_PDR_ZSCORE_INCR_20191217032542.txt";

            // Create a bucket an object to setup a test data.
            await s3Client.PutBucketAsync(destinationBucketName);

            try
            {
                await s3Client.PutObjectAsync(new PutObjectRequest
                {
                    BucketName  = bucketName,
                    Key         = key,
                    ContentBody = inputData
                });

                // Setup the S3 event object that S3 notifications would create with the fields used by the Lambda function.
                var s3Event = new S3Event
                {
                    Records = new List <S3EventNotification.S3EventNotificationRecord>
                    {
                        new S3EventNotification.S3EventNotificationRecord
                        {
                            S3 = new S3EventNotification.S3Entity
                            {
                                Bucket = new S3EventNotification.S3BucketEntity {
                                    Name = bucketName
                                },
                                Object = new S3EventNotification.S3ObjectEntity {
                                    Key = key
                                }
                            }
                        }
                    }
                };
                var context = new TestLambdaContext();
                // Invoke the lambda function and confirm the content type was returned.
                var function    = new Function(s3Client);
                var contentType = await function.FunctionHandler(s3Event, context);

                Assert.Equal("text/plain", contentType);
            }
            catch (Exception ex)
            {
            }
        }
        private Table CreateTable()
        {
            CreateTableRequest createRequest = new CreateTableRequest
            {
                TableName = this._tableName,
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = ATTRIBUTE_SESSION_ID, KeyType = "HASH"
                    }
                },
                AttributeDefinitions = new List <AttributeDefinition>
                {
                    new AttributeDefinition
                    {
                        AttributeName = ATTRIBUTE_SESSION_ID, AttributeType = "S"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = this._initialReadUnits,
                    WriteCapacityUnits = this._initialWriteUnits
                }
            };

            CreateTableResponse response = this._ddbClient.CreateTable(createRequest);

            DescribeTableRequest descRequest = new DescribeTableRequest
            {
                TableName = this._tableName
            };

            // Wait till table is active
            bool isActive = false;

            while (!isActive)
            {
                Thread.Sleep(DESCRIBE_INTERVAL);
                DescribeTableResponse descResponse = this._ddbClient.DescribeTable(descRequest);
                string tableStatus = descResponse.Table.TableStatus;

                if (string.Equals(tableStatus, ACTIVE_STATUS, StringComparison.InvariantCultureIgnoreCase))
                {
                    isActive = true;
                }
            }

            if (!string.IsNullOrEmpty(this._ttlAttributeName))
            {
                this._ddbClient.UpdateTimeToLive(new UpdateTimeToLiveRequest
                {
                    TableName = this._tableName,
                    TimeToLiveSpecification = new TimeToLiveSpecification
                    {
                        AttributeName = this._ttlAttributeName,
                        Enabled       = true
                    }
                });
            }

            var   tableConfig = CreateTableConfig();
            Table table       = Table.LoadTable(this._ddbClient, tableConfig);

            return(table);
        }
Ejemplo n.º 21
0
 /// <summary>
 /// Create a table async.
 /// </summary>
 /// <param name="request">The create table request.</param>
 /// <param name="cancellationToken">The cancellation token.</param>
 /// <returns>The async task.</returns>
 public async Task <CreateTableResponse> CreateTableAsync(CreateTableRequest request, CancellationToken cancellationToken)
 {
     return(await _client.CreateTableAsync(request, cancellationToken));
 }
Ejemplo n.º 22
0
 public void CreateTable(AmazonDynamoDBClient client)
 {
     var postTableCreateRequest = new CreateTableRequest
     {
         AttributeDefinitions = new List <AttributeDefinition>
         {
             new AttributeDefinition
             {
                 AttributeName = nameof(MovieEntity.DirectorId),
                 AttributeType = ScalarAttributeType.S
             },
             new AttributeDefinition
             {
                 AttributeName = nameof(MovieEntity.CreateDate),
                 AttributeType = ScalarAttributeType.S
             },
             new AttributeDefinition()
             {
                 AttributeName = nameof(MovieEntity.MovieId),
                 AttributeType = ScalarAttributeType.S
             }
         },
         TableName = "Movies",
         KeySchema = new List <KeySchemaElement>()
         {
             new KeySchemaElement()
             {
                 AttributeName = nameof(MovieEntity.DirectorId),
                 KeyType       = KeyType.HASH
             },
             new KeySchemaElement()
             {
                 AttributeName = nameof(MovieEntity.CreateDate),
                 KeyType       = KeyType.RANGE
             }
         },
         GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
         {
             new GlobalSecondaryIndex
             {
                 Projection = new Projection
                 {
                     ProjectionType = ProjectionType.ALL
                 },
                 IndexName = Constants.MoiveTableMovieIdGsi,
                 KeySchema = new List <KeySchemaElement>
                 {
                     new KeySchemaElement
                     {
                         AttributeName = nameof(MovieEntity.MovieId),
                         KeyType       = KeyType.HASH
                     }
                 },
                 ProvisionedThroughput = new ProvisionedThroughput
                 {
                     ReadCapacityUnits  = 5,
                     WriteCapacityUnits = 5
                 }
             }
         },
         ProvisionedThroughput = new ProvisionedThroughput
         {
             ReadCapacityUnits  = 5,
             WriteCapacityUnits = 6
         },
     };
     CreateTableResponse result = client.CreateTableAsync(postTableCreateRequest).GetAwaiter().GetResult();
 }
 public CreateTableResponse CreateTable(string tableName, MetadataPreference? metadataPreference = null)
 {
     var request = new CreateTableRequest(_account, tableName, metadataPreference);
     var response = request.Execute();
     return response.Payload;
 }
 void CreateTableForType_(Type type)
 {
     _table = new CreateTableRequestBuilder()
              .BuildFrom(type, "test_");
 }
Ejemplo n.º 25
0
        // Creates tables and it's key schema elements
        public static void CreateTables()
        {
            // Create Widget table if not exist
            List <string> currentTables = _client.ListTables().TableNames;

            if (!currentTables.Contains(_tableNameWidget))
            {
                // Create table request
                var request = new CreateTableRequest
                {
                    TableName = _tableNameWidget,
                    KeySchema = new List <KeySchemaElement>()
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "Id",
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>()
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "Id",
                            AttributeType = ScalarAttributeType.S
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 10,
                        WriteCapacityUnits = 5
                    }
                };

                // Create the table
                var response = _client.CreateTable(request);
            }

            // Create WidgetType table if not exist
            if (!currentTables.Contains(_tableNameWidgetType))
            {
                // Create table request
                var request = new CreateTableRequest
                {
                    TableName = _tableNameWidgetType,
                    KeySchema = new List <KeySchemaElement>()
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "Id",
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>()
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "Id",
                            AttributeType = ScalarAttributeType.S
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 10,
                        WriteCapacityUnits = 5
                    }
                };

                // Create the table
                var response = _client.CreateTable(request);
            }
        }
Ejemplo n.º 26
0
        public static async Task CreateTableIfNotExistsAndWaitForTableToBecomeAvailable(this IAmazonDynamoDB client, CreateTableRequest createTableRequest)
        {
            TableDescription table = null;

            try
            {
                DescribeTableRequest descRequest = new DescribeTableRequest()
                {
                    TableName = createTableRequest.TableName
                };
                table = (await client.DescribeTableAsync(descRequest)).Table;
            }
            catch (AmazonServiceException ase)
            {
                if (!ase.ErrorCode.Equals("ResourceNotFoundException", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw ase;
                }
            }

            // We are all done if it exists already
            if (table != null && table.TableStatus == TableStatus.ACTIVE)
            {
                return;
            }

            if (table == null)
            {
                table = (await client.CreateTableAsync(createTableRequest)).TableDescription;
            }

            if (table.TableStatus != TableStatus.ACTIVE)
            {
                await client.WaitForTableToBecomeAvailable(createTableRequest.TableName);
            }
        }
Ejemplo n.º 27
0
        public AmazonAnnotationPackageProvider()
        {
            var accessKeyId     = ConfigurationManager.AppSettings["accessKeyId"];
            var secretAccessKey = ConfigurationManager.AppSettings["secretAccessKey"];

            this._bucketName       = ConfigurationManager.AppSettings["bucketName"]?.ToLower();
            this._extractionFolder = ConfigurationManager.AppSettings["extractionFolder"];

            this._dbTableName = ConfigurationManager.AppSettings["dbTableName"];

            var s3ServiceUrl       = ConfigurationManager.AppSettings["s3ServiceUrl"];
            var dynamoDbServiceUrl = ConfigurationManager.AppSettings["dynamoDbServiceUrl"];

            if (string.IsNullOrEmpty(accessKeyId))
            {
                throw new ConfigurationErrorsException("The accessKeyId has not been configured. Please set it in the App.config");
            }

            if (string.IsNullOrEmpty(secretAccessKey))
            {
                throw new ConfigurationErrorsException("The secretAccessKey has not been configured. Please set it in the App.config");
            }

            if (string.IsNullOrEmpty(this._bucketName))
            {
                throw new ConfigurationErrorsException("The bucketName has not been configured. Please set it in the App.config");
            }

            if (string.IsNullOrEmpty(s3ServiceUrl) || string.IsNullOrEmpty(dynamoDbServiceUrl))
            {
                this._s3Client       = new AmazonS3Client(accessKeyId, secretAccessKey, RegionEndpoint.USEast1);
                this._dynamoDbClient = new AmazonDynamoDBClient(accessKeyId, secretAccessKey, RegionEndpoint.USEast1);
            }
            else
            {
                var s3Config = new AmazonS3Config
                {
                    ServiceURL     = s3ServiceUrl,
                    ForcePathStyle = true
                };
                var dbConfig = new AmazonDynamoDBConfig
                {
                    ServiceURL = dynamoDbServiceUrl
                };

                this._s3Client       = new AmazonS3Client(accessKeyId, secretAccessKey, s3Config);
                this._dynamoDbClient = new AmazonDynamoDBClient(accessKeyId, secretAccessKey, dbConfig);

                if (!this._s3Client.DoesS3BucketExist(this._bucketName))
                {
                    this._s3Client.PutBucket(this._bucketName);
                }

                var createTableRequest = new CreateTableRequest
                {
                    TableName              = this._dbTableName,
                    AttributeDefinitions   = new List <AttributeDefinition>(),
                    KeySchema              = new List <KeySchemaElement>(),
                    GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>(),
                    LocalSecondaryIndexes  = new List <LocalSecondaryIndex>(),
                    ProvisionedThroughput  = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 1,
                        WriteCapacityUnits = 1
                    }
                };
                createTableRequest.KeySchema = new[]
                {
                    new KeySchemaElement
                    {
                        AttributeName = "Id",
                        KeyType       = KeyType.HASH,
                    },
                }.ToList();

                createTableRequest.AttributeDefinitions = new[]
                {
                    new AttributeDefinition
                    {
                        AttributeName = "Id",
                        AttributeType = ScalarAttributeType.S,
                    }
                }.ToList();

                var tokenSource = new CancellationTokenSource();
                var token       = tokenSource.Token;

                tokenSource.CancelAfter(10000);

                var tables = this._dynamoDbClient.ListTablesAsync(token).GetAwaiter().GetResult();
                if (!tables.TableNames.Contains(this._dbTableName))
                {
                    this._dynamoDbClient.CreateTableAsync(createTableRequest).GetAwaiter().GetResult();
                }
            }

            this._packagesToDownload = new Queue <AnnotationPackage>();
        }
Ejemplo n.º 28
0
        private async Task TryCreateTables()
        {
            try
            {
                await _dynamo.DeleteTableAsync(new DeleteTableRequest { TableName = "Animals" });

                var response = await _dynamo.DescribeTableAsync(new DescribeTableRequest
                {
                    TableName = "Animals"
                });
            }
            catch (ResourceNotFoundException)
            {
                var request = new CreateTableRequest
                {
                    TableName            = "Animals",
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition
                        {
                            AttributeName = "Id",
                            AttributeType = "N"
                        },
                        new AttributeDefinition
                        {
                            AttributeName = "Genus",
                            AttributeType = "S"
                        }
                    },
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = "Id",
                            KeyType       = "HASH"
                        },
                        new KeySchemaElement
                        {
                            AttributeName = "Genus",
                            KeyType       = "RANGE"
                        },
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 10,
                        WriteCapacityUnits = 5
                    },
                    GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                    {
                        new GlobalSecondaryIndex
                        {
                            IndexName = "Animals_ByGenus",
                            KeySchema = new List <KeySchemaElement>
                            {
                                new KeySchemaElement
                                {
                                    AttributeName = "Genus",
                                    KeyType       = "HASH"
                                }
                            },
                            Projection = new Projection
                            {
                                ProjectionType = ProjectionType.ALL
                            },
                            ProvisionedThroughput = new ProvisionedThroughput
                            {
                                ReadCapacityUnits  = 10,
                                WriteCapacityUnits = 5
                            }
                        }
                    }
                };

                await _dynamo.CreateTableAsync(request);

                var items = new[]
                {
                    new Dictionary <string, AttributeValue>
                    {
                        { "Id", new AttributeValue {
                              N = "1"
                          } },
                        { "Genus", new AttributeValue {
                              S = "Microcarbo"
                          } },
                        { "Family", new AttributeValue {
                              S = "Phalacrocoracidae"
                          } },
                        { "Order", new AttributeValue {
                              S = "Suliformes"
                          } },
                        { "Class", new AttributeValue {
                              S = "Aves"
                          } },
                        { "CommonNames", new AttributeValue {
                              SS = new List <string> {
                                  "Little cormorant", "Javanese Cormorant"
                              }
                          } },
                        { "ScientificName", new AttributeValue {
                              S = "Microcarbo niger"
                          } },
                    }
                };

                foreach (var item in items)
                {
                    await _dynamo.PutItemAsync("Animals", item);
                }
            }
        }
Ejemplo n.º 29
0
 public static CreateTableResponse CreateTable(this IAmazonDynamoDB client, CreateTableRequest request)
 {
     return(client.CreateTableAsync(request).GetResult());
 }
        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);
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Create table if it doesn't exist.
        /// Sample code from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LowLevelDotNetTableOperationsExample.html
        /// </summary>
        /// <param name="client"></param>
        /// <param name="tabelName"></param>
        /// <param name="primaryKey"></param>
        /// <param name="sortKey"></param>
        /// <returns></returns>
        internal static async Task <string> SetupTable(AmazonDynamoDBClient client, string tabelName, string primaryKey, string sortKey = null)
        {
            Console.WriteLine("\n*** Creating table ***");
            var request = new CreateTableRequest
            {
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = primaryKey,
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement
                    {
                        AttributeName = primaryKey,
                        KeyType       = "HASH" //Partition key
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 5,
                    WriteCapacityUnits = 6
                },
                TableName = tabelName
            };

            if (!string.IsNullOrWhiteSpace(sortKey))
            {
                request.AttributeDefinitions.Add(new AttributeDefinition()
                {
                    AttributeName = sortKey,
                    AttributeType = "S"
                });
                request.KeySchema.Add(new KeySchemaElement()
                {
                    AttributeName = sortKey,
                    KeyType       = "RANGE" // Sort Key
                });
            }
            try
            {
                var response = await client.CreateTableAsync(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(tabelName + " - " + status);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(tabelName);
        }
Ejemplo n.º 32
0
 /// <summary>
 /// Build a table from a create table request
 /// </summary>
 /// <param name="createTableRequest">The request to build tables from</param>
 /// <param name="ct"></param>
 /// <returns>The response to table creation</returns>
 public async Task <CreateTableResponse> Build(CreateTableRequest createTableRequest, CancellationToken ct = default(CancellationToken))
 {
     return(await _client.CreateTableAsync(createTableRequest, ct));
 }
Ejemplo n.º 33
0
        private async Task CreateTable(string tableName, List <KeySchemaElement> keys, List <AttributeDefinition> attributes, List <GlobalSecondaryIndex> secondaryIndexes = null, string ttlAttributeName = null)
        {
            var useProvisionedThroughput = readCapacityUnits > 0 && writeCapacityUnits > 0;
            var request = new CreateTableRequest
            {
                TableName             = tableName,
                AttributeDefinitions  = attributes,
                KeySchema             = keys,
                BillingMode           = useProvisionedThroughput ? BillingMode.PROVISIONED : BillingMode.PAY_PER_REQUEST,
                ProvisionedThroughput = useProvisionedThroughput ? new ProvisionedThroughput
                {
                    ReadCapacityUnits  = readCapacityUnits,
                    WriteCapacityUnits = writeCapacityUnits
                } : null
            };

            if (secondaryIndexes != null && secondaryIndexes.Count > 0)
            {
                if (useProvisionedThroughput)
                {
                    var indexThroughput = new ProvisionedThroughput {
                        ReadCapacityUnits = readCapacityUnits, WriteCapacityUnits = writeCapacityUnits
                    };
                    secondaryIndexes.ForEach(i =>
                    {
                        i.ProvisionedThroughput = indexThroughput;
                    });
                }

                request.GlobalSecondaryIndexes = secondaryIndexes;
            }

            try
            {
                var response = await ddbClient.CreateTableAsync(request);

                TableDescription description = null;
                do
                {
                    description = await GetTableDescription(tableName);

                    await Task.Delay(2000);
                } while (description.TableStatus == TableStatus.CREATING);

                if (!string.IsNullOrEmpty(ttlAttributeName))
                {
                    await ddbClient.UpdateTimeToLiveAsync(new UpdateTimeToLiveRequest
                    {
                        TableName = tableName,
                        TimeToLiveSpecification = new TimeToLiveSpecification {
                            AttributeName = ttlAttributeName, Enabled = true
                        }
                    });
                }
                if (description.TableStatus != TableStatus.ACTIVE)
                {
                    throw new InvalidOperationException($"Failure creating table {tableName}");
                }
            }
            catch (Exception exc)
            {
                Logger.Error(ErrorCode.StorageProviderBase, $"Could not create table {tableName}", exc);
                throw;
            }
        }
Ejemplo n.º 34
0
        public async Task CreateTable()
        {
            var request = new CreateTableRequest
            {
                AttributeDefinitions = new List <AttributeDefinition>()
                {
                    new AttributeDefinition
                    {
                        AttributeName = "UserId",
                        AttributeType = "N"
                    },
                    new AttributeDefinition
                    {
                        AttributeName = "MovieName",
                        AttributeType = "S"
                    }
                },
                KeySchema = new List <KeySchemaElement>()
                {
                    new KeySchemaElement
                    {
                        AttributeName = "UserId",
                        KeyType       = "HASH"
                    },
                    new KeySchemaElement
                    {
                        AttributeName = "MovieName",
                        KeyType       = "RANGE"
                    }
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = 1,
                    WriteCapacityUnits = 1
                },
                TableName = "MovieRank",
                GlobalSecondaryIndexes = new List <GlobalSecondaryIndex>
                {
                    new GlobalSecondaryIndex {
                        IndexName = "MovieName-index",
                        KeySchema = new List <KeySchemaElement>()
                        {
                            new KeySchemaElement
                            {
                                AttributeName = "MovieName",
                                KeyType       = "HASH"
                            }
                        },
                        ProvisionedThroughput = new ProvisionedThroughput
                        {
                            ReadCapacityUnits  = 1,
                            WriteCapacityUnits = 1
                        },
                        Projection = new Projection
                        {
                            ProjectionType = "ALL"
                        }
                    }
                }
            };
            var response = await DynamoDBClient.CreateTableAsync(request);

            await WaitUntilTableActive(request.TableName);
        }
Ejemplo n.º 35
0
 /*--------------------------------------------------------------------------
  *                       CreatingTable_async
  *--------------------------------------------------------------------------*/
 public async Task CreatingTable_async(CreateTableRequest request)
 {
     Task <bool> newTbl = CreateNewTable_async(request);
     await       newTbl;
 }
Ejemplo n.º 36
0
        protected virtual CreateTableRequest ToCreateTableRequest(DynamoMetadataType table)
        {
            var props = table.Type.GetSerializableProperties();

            if (props.Length == 0)
            {
                throw new NotSupportedException($"{table.Name} does not have any serializable properties");
            }

            var keySchema = new List <KeySchemaElement> {
                new KeySchemaElement(table.HashKey.Name, KeyType.HASH),
            };
            var attrDefinitions = new List <AttributeDefinition> {
                new AttributeDefinition(table.HashKey.Name, table.HashKey.DbType),
            };

            if (table.RangeKey != null)
            {
                keySchema.Add(new KeySchemaElement(table.RangeKey.Name, KeyType.RANGE));
                attrDefinitions.Add(new AttributeDefinition(table.RangeKey.Name, table.RangeKey.DbType));
            }

            var to = new CreateTableRequest
            {
                TableName             = table.Name,
                KeySchema             = keySchema,
                AttributeDefinitions  = attrDefinitions,
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = table.ReadCapacityUnits ?? ReadCapacityUnits,
                    WriteCapacityUnits = table.WriteCapacityUnits ?? WriteCapacityUnits,
                }
            };

            if (!table.LocalIndexes.IsEmpty())
            {
                to.LocalSecondaryIndexes = table.LocalIndexes.Map(x => new LocalSecondaryIndex
                {
                    IndexName  = x.Name,
                    KeySchema  = x.ToKeySchemas(),
                    Projection = new Projection
                    {
                        ProjectionType   = x.ProjectionType,
                        NonKeyAttributes = x.ProjectedFields.Safe().ToList(),
                    },
                });

                table.LocalIndexes.Each(x =>
                {
                    if (x.RangeKey != null && attrDefinitions.All(a => a.AttributeName != x.RangeKey.Name))
                    {
                        attrDefinitions.Add(new AttributeDefinition(x.RangeKey.Name, x.RangeKey.DbType));
                    }
                });
            }
            if (!table.GlobalIndexes.IsEmpty())
            {
                to.GlobalSecondaryIndexes = table.GlobalIndexes.Map(x => new GlobalSecondaryIndex
                {
                    IndexName  = x.Name,
                    KeySchema  = x.ToKeySchemas(),
                    Projection = new Projection
                    {
                        ProjectionType   = x.ProjectionType,
                        NonKeyAttributes = x.ProjectedFields.Safe().ToList(),
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = x.ReadCapacityUnits ?? ReadCapacityUnits,
                        WriteCapacityUnits = x.WriteCapacityUnits ?? WriteCapacityUnits,
                    }
                });

                table.GlobalIndexes.Each(x =>
                {
                    if (x.HashKey != null && attrDefinitions.All(a => a.AttributeName != x.HashKey.Name))
                    {
                        attrDefinitions.Add(new AttributeDefinition(x.HashKey.Name, x.HashKey.DbType));
                    }
                    if (x.RangeKey != null && attrDefinitions.All(a => a.AttributeName != x.RangeKey.Name))
                    {
                        attrDefinitions.Add(new AttributeDefinition(x.RangeKey.Name, x.RangeKey.DbType));
                    }
                });
            }


            return(to);
        }