Last active
August 1, 2019 04:57
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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