in dotnetv3/dynamodb/FromSQL/CreateIndexExample/CreateIndexExample/CreateIndex.cs [161:286]
public static async Task<UpdateTableResponse> AddIndexAsync(IAmazonDynamoDB client, string table, string indexname, string partitionkey, string partitionkeytype, string sortkey, string sortkeytype)
{
if (client is null)
{
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;
}