public async Task AddSecondaryIndexAsync() { var attributeDefinitions = new List <AttributeDefinition> { new AttributeDefinition { AttributeName = "Id", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "UserId", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "AccountId", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "UniqueMediaName", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "UniqueMediaKey", AttributeType = ScalarAttributeType.S }, new AttributeDefinition { AttributeName = "DateAdded", AttributeType = ScalarAttributeType.S }, }; var request = new CreateGlobalSecondaryIndexAction { IndexName = "UserId-DateAdded-index", KeySchema = new List <KeySchemaElement> { new KeySchemaElement { AttributeName = "UserId", KeyType = KeyType.HASH }, new KeySchemaElement { AttributeName = "DateAdded", KeyType = KeyType.RANGE } }, Projection = new Projection { ProjectionType = ProjectionType.ALL }, ProvisionedThroughput = new ProvisionedThroughput(1, 1), }; UpdateTableRequest updateTableRequest = new UpdateTableRequest { AttributeDefinitions = attributeDefinitions, GlobalSecondaryIndexUpdates = new List <GlobalSecondaryIndexUpdate> { new GlobalSecondaryIndexUpdate { Create = request } }, TableName = _tablePrefix + "MediaItem", }; var response = await _client.UpdateTableAsync(updateTableRequest); if (response.HttpStatusCode != HttpStatusCode.OK) { Trace.WriteLine("Not updated!!"); } }
public static async Task <UpdateTableResponse> AddIndexAsync(IAmazonDynamoDB client, string table, string indexname, string partitionkey, string partitionkeytype, string sortkey, string sortkeytype) { if (null == client) { throw new ArgumentNullException("client parameter is null"); } if (string.IsNullOrEmpty(table)) { throw new ArgumentNullException("table parameter is null"); } if (string.IsNullOrEmpty(indexname)) { throw new ArgumentNullException("indexname parameter is null"); } if (string.IsNullOrEmpty(partitionkey)) { throw new ArgumentNullException("partitionkey parameter is null"); } if (string.IsNullOrEmpty(sortkey)) { throw new ArgumentNullException("sortkey parameter is null"); } ProvisionedThroughput pt = new ProvisionedThroughput { ReadCapacityUnits = 10L, WriteCapacityUnits = 5L }; KeySchemaElement kse1 = new KeySchemaElement { AttributeName = partitionkey, KeyType = "HASH" }; KeySchemaElement kse2 = new KeySchemaElement { AttributeName = sortkey, KeyType = "RANGE" }; List <KeySchemaElement> kses = new List <KeySchemaElement> { kse1, kse2 }; Projection p = new Projection { ProjectionType = "ALL" }; var newIndex = new CreateGlobalSecondaryIndexAction() { IndexName = indexname, ProvisionedThroughput = pt, KeySchema = kses, Projection = p }; GlobalSecondaryIndexUpdate update = new GlobalSecondaryIndexUpdate { Create = newIndex }; List <GlobalSecondaryIndexUpdate> updates = new List <GlobalSecondaryIndexUpdate> { update }; AttributeDefinition ad1; if (partitionkeytype == "string") { ad1 = new AttributeDefinition { AttributeName = partitionkey, AttributeType = "S" }; } else { ad1 = new AttributeDefinition { AttributeName = partitionkey, AttributeType = "N" }; } AttributeDefinition ad2; if (sortkeytype == "string") { ad2 = new AttributeDefinition { AttributeName = sortkey, AttributeType = "S" }; } else { ad2 = new AttributeDefinition { AttributeName = sortkey, AttributeType = "N" }; } UpdateTableRequest request = new UpdateTableRequest { TableName = table, AttributeDefinitions = { ad1, ad2 }, GlobalSecondaryIndexUpdates = updates }; var response = await client.UpdateTableAsync(request); return(response); }