private Amazon.DynamoDBv2.Model.CreateTableResponse CallAWSServiceOperation(IAmazonDynamoDB client, Amazon.DynamoDBv2.Model.CreateTableRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon DynamoDB", "CreateTable"); try { #if DESKTOP return(client.CreateTable(request)); #elif CORECLR return(client.CreateTableAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
public static void CreateBooksTable(string tableName) { try { DynamoDbClient.CreateTable( new CreateTableRequest { TableName = tableName, AttributeDefinitions = new List<AttributeDefinition> { new AttributeDefinition { AttributeName = "Name", AttributeType = "S" }, new AttributeDefinition { AttributeName = "PublishYear", AttributeType = "N" } }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 5, WriteCapacityUnits = 5 }, KeySchema = new List<KeySchemaElement> { new KeySchemaElement { AttributeName = "Name", KeyType = "HASH" }, new KeySchemaElement { AttributeName = "PublishYear", KeyType = "RANGE" } } }); Logger.DebugFormat("Created table {0}", tableName); } catch { Logger.DebugFormat("Table already existed {0}", tableName); } }
public static void ConfirmTableExistence(string tableName, IAmazonDynamoDB client, List <KeySchemaElement> tableSchema, List <AttributeDefinition> attributeDefinitions, int reads, int writes) { Console.WriteLine("Confirming table " + tableName + " existence"); string tableStatus = null; tableStatus = WaitUntilTableStable(tableName, client, tableStatus); if (string.IsNullOrEmpty(tableStatus)) { Console.WriteLine("Creating table " + tableName); var tableDescription = client.CreateTable(new CreateTableRequest { TableName = tableName, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = reads, WriteCapacityUnits = writes }, KeySchema = tableSchema, AttributeDefinitions = attributeDefinitions }).TableDescription; WaitUntilTableCreated(tableName, client); } else { Console.WriteLine("Not creating table " + tableName + ", status is " + tableStatus); } }
private static async Task CheckCreateTable( this IAmazonDynamoDB dynamoDB, string tableName, string partitionKey, string sortKey, int readCapacityUnits, int writeCapacityUnits ) { var existingTable = await dynamoDB.IsTableCreated(tableName, true); if (existingTable.Created) { CheckTableKeys(existingTable.TableDesc, partitionKey, sortKey); } else { var res = await dynamoDB.CreateTable(tableName, partitionKey, sortKey, readCapacityUnits, writeCapacityUnits); if (res.TableStatus != "ACTIVE") { await Task.Delay(2000); await dynamoDB.IsTableCreated(tableName, false); } } }
public void Create_Table_with_AWSSDK() { db.DeleteTable <Todo>(); var request = new CreateTableRequest { TableName = "Todo", KeySchema = new List <KeySchemaElement> { new KeySchemaElement("Id", KeyType.HASH), }, AttributeDefinitions = new List <AttributeDefinition> { new AttributeDefinition("Id", ScalarAttributeType.N), }, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = 10, WriteCapacityUnits = 5, } }; awsDb.CreateTable(request); while (true) { try { var descResponse = awsDb.DescribeTable(new DescribeTableRequest("Todo")); if (descResponse.Table.TableStatus == DynamoStatus.Active) { break; } Thread.Sleep(TimeSpan.FromSeconds(2)); } catch (ResourceNotFoundException) { // DescribeTable is eventually consistent. So you might get resource not found. } } var listResponse = awsDb.ListTables(new ListTablesRequest()); var tableNames = listResponse.TableNames; tableNames.PrintDump(); }
public static void ConfirmTableExistence(string tableName, IAmazonDynamoDB client, List<KeySchemaElement> tableSchema, List<AttributeDefinition> attributeDefinitions, int reads, int writes) { Console.WriteLine("Confirming table " + tableName + " existence"); string tableStatus = null; tableStatus = WaitUntilTableStable(tableName, client, tableStatus); if (string.IsNullOrEmpty(tableStatus)) { Console.WriteLine("Creating table " + tableName); var tableDescription = client.CreateTable(new CreateTableRequest { TableName = tableName, ProvisionedThroughput = new ProvisionedThroughput { ReadCapacityUnits = reads, WriteCapacityUnits = writes }, KeySchema = tableSchema, AttributeDefinitions = attributeDefinitions }).TableDescription; WaitUntilTableCreated(tableName, client); } else { Console.WriteLine("Not creating table " + tableName + ", status is " + tableStatus); } }
private void CreateTable() { var request = new CreateTableRequest { AttributeDefinitions = _config.AttributeDefinitions, KeySchema = _config.KeySchema, ProvisionedThroughput = _config.ProvisionedThroughput, TableName = _config.TableName }; try { var response = _client.CreateTable(request); WaitUntilTableReady(_config.TableName); } catch (AmazonDynamoDBException ex) { if (!_expectedErrorCodesForConcurrentCalls.Contains(ex.ErrorCode)) { throw; } } }
public void CreateNewTable(Type entity, Type entityBaseType) { if (dynamoService == null || entity == null || !entityBaseType.IsAssignableFrom(entity)) { return; } try { DynamoDBTableAttribute attr = entity.GetCustomAttribute <DynamoDBTableAttribute>(); if (attr == null) { return; } string tableName = attr.TableName.ToLower(); if (CheckTableExists(tableName)) { ConsoleHelper.Write("\nTable "); ConsoleHelper.Write(tableName.ToUpper(), ConsoleColor.Yellow); ConsoleHelper.Write(string.Format(" is active in dynamoDB - {0}", dynamoService.DynamoServiceUrl)); return; } dynamoService.RestartDynamoDBContext(); using (IAmazonDynamoDB client = dynamoService.DynamoClient) { CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.TableName = tableName; createTableRequest.ProvisionedThroughput = new ProvisionedThroughput() { ReadCapacityUnits = 1, WriteCapacityUnits = 1 }; List <KeySchemaElement> keySchemas = new List <KeySchemaElement>(); List <AttributeDefinition> attributeDefinitions = new List <AttributeDefinition>(); Attribute hashAttr = null; Attribute rangeAttr = null; foreach (PropertyInfo property in entity.GetProperties()) { hashAttr = property.GetCustomAttribute(typeof(DynamoDBHashKeyAttribute), false); rangeAttr = property.GetCustomAttribute(typeof(DynamoDBRangeKeyAttribute), false); if (hashAttr != null) { keySchemas.Add(new KeySchemaElement(property.Name, KeyType.HASH)); attributeDefinitions.Add(new AttributeDefinition(property.Name, TypeToScalarAttributeType(property.GetType()))); } else if (rangeAttr != null) { keySchemas.Add(new KeySchemaElement(property.Name, KeyType.RANGE)); attributeDefinitions.Add(new AttributeDefinition(property.Name, TypeToScalarAttributeType(property.GetType()))); } } createTableRequest.KeySchema = keySchemas; createTableRequest.AttributeDefinitions = attributeDefinitions; CreateTableResponse createTableResponse = client.CreateTable(createTableRequest); TableDescription tableDescription = createTableResponse.TableDescription; ConsoleHelper.Write("\n\nTable "); ConsoleHelper.Write(tableName.ToUpper(), ConsoleColor.Yellow); ConsoleHelper.Write(" creation command sent to Amazon..."); string tableStatus = tableDescription.TableStatus.Value.ToLower(); while (!ACTIVE_STATUS.Equals(tableStatus)) { ConsoleHelper.Write("\nTable "); ConsoleHelper.Write(tableName.ToUpper(), ConsoleColor.Yellow); ConsoleHelper.WriteLine(" is not yet active, waiting..."); //Waiting for the table to be created in amazon or locally, check again each x milliseconds Thread.Sleep(2000); DescribeTableRequest describeTableRequest = new DescribeTableRequest(tableName); DescribeTableResponse describeTableResponse = client.DescribeTable(describeTableRequest); tableDescription = describeTableResponse.Table; tableStatus = tableDescription.TableStatus.Value.ToLower(); ConsoleHelper.Write("\nLatest status of table "); ConsoleHelper.Write(string.Format("{0}: ", tableName.ToUpper()), ConsoleColor.Yellow); ConsoleHelper.Write(tableStatus.ToUpper(), ConsoleColor.Green); } ConsoleHelper.Write("\nCreation complete for table "); ConsoleHelper.Write(tableName.ToUpper(), ConsoleColor.Yellow); ConsoleHelper.Write(", final status: "); ConsoleHelper.Write(tableStatus.ToUpper(), ConsoleColor.Green); } } catch (AmazonDynamoDBException exception) { ConsoleHelper.Write(string.Format("\nException while creating new dynamoDB table: {0}.", exception.Message), ConsoleColor.Red); ConsoleHelper.Write(string.Format(" Error code: {0}, error type: {1}", exception.ErrorCode, exception.ErrorType), ConsoleColor.Red); } }