Skip to content

Instantly share code, notes, and snippets.

@deborahc
Last active August 1, 2019 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deborahc/6a8139b7307d723a621594fba5bfc5e5 to your computer and use it in GitHub Desktop.
Save deborahc/6a8139b7307d723a621594fba5bfc5e5 to your computer and use it in GitHub Desktop.
Scaling a non-partitioned (fixed) container) with Azure Cosmos DB .NET V3 SDK (GA)
// Scale fixed container using partition key in .NET V3 SDK
public static async Task ScaleNonPartitionedContainer()
{
using (var cosmosClient = new CosmosClient(endpoint, key))
{
//Get reference to existing fixed container
var container = cosmosClient.GetDatabase(databaseId).GetContainer(containerId);
// Add item to container without partition key
var user = new User()
{
Id = "bob",
Status = "Learning Azure Cosmos DB!"
};
await container.CreateItemAsync<User>(user, PartitionKey.None);
// Now start taking advantage of partitioning! Create and add a new item with partition key value of user Id
var userWithPartitionKey = new User()
{
Id = "alice",
PartitionKey = "alice",
Status = "Partitioning all the things"
};
await container.CreateItemAsync<User>(userWithPartitionKey, new PartitionKey(userWithPartitionKey.PartitionKey));
// Scale throughtput beyond 10,000 RU/s limit of fixed containers
var throughputResponse = await container.ReplaceThroughputAsync(15000);
}
}
// Example of the User class
public class User
{
public User()
{
}
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "_partitionKey", NullValueHandling = NullValueHandling.Ignore)] // Enbales optional PartitionKey value
public string PartitionKey { get; set; }
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment